- Assign the value
6to the variablenumber_six
number_six = 6
- What is the datatype of
"Matz"?
String
- What do the following expressions evaluate to:
"Cool" + "Cool" + "Cool"
"CoolCoolCool"
t = "Troy" a = "Abed" "#{t} and #{a} in the Morning"
"Troy and Abed in the Morning"
10 * 3
30
10 ** 3
1000
10 % 3
1
10 / 3
3
0.7 + 0.1
0.7999999999999999
- Can you explain why the expressions all give the same result?
"Ronnie " + "Pickering""Ronnie ".+("Pickering")"Ronnie ".send(:+, "Pickering")
The + operator is used as a string method in the first example, so thanks to Ruby syntactical sugar we can write it as:
"Ronnie " + "Pickering"
but it can also be used with the normal method syntax as in the second example:
"Ronnie ".+("Pickering")
In the third example, the send method is passed the + method as a symbol, and a string as the second argument. Send invokes whatever method it is passed, and passes it the specified arguement (in this case "Pickering"). So this is effectively the same as the other two approaches above.
- Please fix the following buggy expressions:
number six = 6"Maroon" + 5ture == flase
number_six = 6
"Maroon" + " 5"
true == false