-
Use only the "string concatenation" technique to complete the following:
-
What code can you write to output the string "FirstLast"?
- puts "First" + "Last"
-
What code can you write to output the string "LastFirst"?
- puts "Last" + "First"
-
What code can you write to output the string "First Last"?
- puts "First" + " " + "Last"
-
What code can you write to output the string "Last First Last First"?
- puts "Last " + "First " + "Last " + "First"
-
Then repeat 1-4 using only the "string interpolation" technique.
-
What code can you write to output the string "FirstLast"?
- last = "Last"
- puts "First#{last}"
-
What code can you write to output the string "LastFirst"?
- first = "First"
- puts "Last#{first}"
-
What code can you write to output the string "First Last"?
- first = "First"
- puts "Last #{first}"
-
What code can you write to output the string "Last First Last First"?
- first = "First"
- puts "Last #{first} Last #{first}"
-
- Can you come up with two ways to output just the fragment "Megan" from name_1?
- name_1.delete(" Smith")
- name_1.tr(" Smith","")
- Would either of your techniques from A would work to output "Todd" from name_2? Why or why not?
- Neither would work for name_2 because they specifically delete/trim the letters "Smith"
- Write code that can output the initials of name_2.
- name_2.tr("odd ark","")
- Write code to find the average of these four numbers.
- (a + b + c + d)/4.to_f
- Find the average yourself using paper or a calculator. Is your answer different than you found in A? Why?
- 51.5 It's the same answer as in A because I converted the answer to a Float.
- Say you have the operation a + b * c / d. What result do you get out from Ruby? What other outputs can you get out by adding one or more pairs of parentheses to the equation?
- 32 You could also get 24 if you put (a + b) in parentheses, or 20 if you do (a + (b * c)) / d
- Write code to output both the total characters in all the names together and the average length of the names.
- (a.length + b.length + c.length + d.length) = 20 total characters
- (a.length + b.length + c.length + d.length)/4 = 5 average length of names
- Say you have an age variable that holds the person's age. Write code to output the appropriate greeting.
- ("Happy" * age.to_i) + "Birthday"
- There's a silly compression algorithm that outputs the first letter, the number of letters in the middle, and the last letter. So for the string "Kalamazoo" it'd output "K7o" or "Denver" would be "D4r". Can you write code to implement that?
- string = "Something"
- string[0] +string[1..-2].length + string[-1]