-
Go through the ruby String documentation and look for a method or function that prepends or combine one string to another string. Using said method prepend the string "
Learning
" to the string "Ruby
". -
Go through the ruby String documentation and look for a method or function that remove characters from a string. Using said method, it turns the string "
Learning Ruby
" into the string "Lrnng Rby
". -
There is a ruby String method that allows to justify a string, padding it with another string. Find said method and use it on the string "
Ruby
" together with "<3
" so that you get the following string result:
"Ruby<3<3<3"
- Create a ruby application called
exercise_04.rb
. Fill it in with the following lines:
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# your code goes here...
So that, when you run your code, e.g., ruby exercise_04.rb
, you get the following output
5
- Copy
exercise_04.rb
toexercise_05.rb
, then open this new file. Add another line before the line that you just added, so that, when you run your code you get the following output:
55
- Create a new ruby file called
exercise_06.rb
, and fill in the following line:
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# your code goes here...
puts numbers.inspect
So that you get the following output:
[2, 4, 6, 8, 10]
Tip: Consult the ruby Array documentation for the correct method or function to use.
- Again, copy the finished
exercise_06.rb
toexercise_07.rb
.
Now change the code so that you get the following output:
[10, 8, 6, 4, 2]
Tip: Consult the ruby Array documentation for the correct method or function to use.
Note: The method select that you used in the last exercise returns an array. On this array (the returned value) you can use another method by appending another dot .
, and the method that would produce the output [10, 8, 6, 4, 2]
.
Note: There is another method that reverses the order of the array. You can find it by googling for “ruby array reverse”.
- Again, copy the finished
exercise_07.rb
, then change your code so that you get the following output:
[10, 8, 4, 2]
Tip: Consult the ruby Array documentation for the correct method or function to use.
Bonus: Find at least three different solutions for this last change.
- Create a new file called
exercise_09.rb
, and fill the following lines:
dictionary = { :one => "ichi", :two => "ni", :three => "san" }
# your code goes here...
So that it prints out
ni
- Create a new file called
exercise_10.rb
and fill in the following line:
dictionary = { :one => "ichi", :two => "ni", :three => "san" }
# your code goes here...
puts dictionary[:four]
So that it prints out
shi