How was the install and playing with command line? Do you have any questions or concepts that you'd like to discuss?
quitand then press
Try out some calculator functions
3 + 4 2 * 4 6 - 2 4 / 2 4 /* 2 => SyntaxError: (irb):10:syntax error, unexpected *
Other Ruby Math Operators
11 % 5 => 1 2 ** 3 => 8
irb> hi NameError: undefined local variable or method `hi' for main:Object
# Everything after this pound sign will not be evaluated. # These are called comments and I will use them to annotate code age = 50 days_in_year = 365 days_alive = age * days_in_year => 18250
# variables age = 50 _age = 50 _my_age = 50 # constants PI = 3.14159 # literals 3 'hi'
# create and assign in one line age = 0 # access the value in age age # replace value in age age = 40
# good occupation = "software developer" # bad occupation_i_always_dreamed_of_having_in_seattle = "software developer" o = "software developer" 1st_occupation = "software developer"
# error name # defined, initalize, create name = "KC" defined?(name)
Numbers | Strings | Symbols | Booleans
Regular Expressions | Arrays | Ranges | Hashes
irb> 1.class => Fixnum irb> "Hello".class => String irb> 1.0022.class => Float irb> [1,2,3].class => Array
Numeric data comes in two types: Integers and Floats
Integers are either Fixnum or Bignum. They do not have decimals.
Floats have at least one number to the left of a decimal point.
7/8 7.0/8.0 3.14.to_s 3.14.to_i 1 + "2" 1 + "2".to_i (1 + 2) * 3 1 + (2 * 3)
How many seconds are in an hour?
How many minutes are in a week?
How many years old are you if you've been alive 1 million seconds?
Strings are characters inside double or single quotes.
a = 'Hello ' b = "World" c = a + b c.reverse => "dlroW olleH"
a = "Spam " # Here multiplying a by 4 concatenates (links) four strings together b = a * 4 =>"Spam Spam Spam Spam "
"Heather".upcase "Heather".downcase "heather".capitalize "Hello".reverse "Heather".length "Heather Moore".swapcase "".empty?
What is the reverse of your name?
How many characters long is your name?
Can you repeat the word hello 100 times?
What is the 5th character of my name?
irb> puts "hello" hello => nil irb> "2".to_i => 2 irb> 2.to_s => "2" irb> "2" / 5 NoMethodError: undefined method `/' for "2":String
Put the code below in a file and save it as name.rb
Run your program from the command line:
getsto ask a user for their age and then tell them how old they are in dog years.
getsmethod returns a string. To do math on it, convert it to an integer with the
#1 dog year = 7 human years user_age = gets.to_i
Update your code to return a message in all capital letters.
# YOU ARE 5 YEARS OLD IN DOG YEARS
Update your code to return a message in all capital letters, reversed with a count of the characters returned in parens.
# YOU ARE 5 YEARS OLD IN DOG YEARS (32 characters)
Practice: Write a command line program that asks the user for the year they were born, then calculates their age in years, days, and seconds. Tell the user how old they are in these different formats. (Note: you'll be using
puts in this program, along with some math)
Temperature conversion! Make a program that asks the user for the temperature in Fahrenheit and print out the temperature in Celsius and Kelvins.
More info: Read more on methods and some basic methods already given to you Chapter 5 of Learn to Program
@gdidayton | #GDIDAY3
We are done with class 1!
We have done a lot, I know you have questions so ask them!