A collection of exercises and answers.
-
-
Save kotp/2413004 to your computer and use it in GitHub Desktop.
This is a collection of exercises, and answers. | |
Filenames will be in the form of w1e1.rb. | |
Answers will be w1e1a1.rb with subsequent 'best answers' being numbered consecutively. |
Exercise1. Before executing the code given below, guess the results. Next, execute the code. Did you get it right? If you did not get it right, can you think of why?
Discuss your first guess and what you got when running the code. Goal: Understanding operator precedence and association.
y = false
z = true
x = y or z
puts x
(x = y) or z
puts x
x = (y or z)
puts x
y = false # simply an assignment
z = true # simply an assignment
x = y or z # x is assigned false though true is returned for the line
puts x # prints false
(x = y) or z # x is again assigned false while the line returns true
puts x # prints false
x = (y or z) # (y or z) returns true, and that result is assigned to x
puts x # prints true as the parentheses gives precedence to or
sprintf format_string
%[flags][width][.precision]type
For %05d:
flags: 0 = Pad with zeros, not spaces. width: 5 type: d = Convert argument as a decimal number.
puts "%05d" % 123 # => Prints 00123 |
Exercise3. Write a Ruby program that displays how old I am, in years, if I am 979000000 seconds old.
Display the result as a floating point (decimal) number to two decimal places ( for example, 17.23).
Note: To format the output to say 2 decimal places, we can use the Kernel's format method. For example, if x = 45.5678 then format("%.2f", x) will return the string 45.57
def how_old(age_in_seconds) | |
Float(age_in_seconds) / 60 / 60 / 24 / 365 | |
end | |
age_in_seconds = 979_000_000 | |
puts "You are %.2f years old" % how_old(age_in_seconds) |
# Almost a sarcastic answer. But in reality, the simplest thing that solves | |
# the problem is the best solution, generally, so this is an awesome start. | |
puts 525600 |
minutes = 60 * 24 * 365 | |
puts "There are #{minutes} minutes in a year." | |
# => "There are 525600 minutes in a year" | |
The puts command is pointing to the variable 'my_string', where the output is 'Hello Ruby World'. The method 'my_string' is never called.
The method can be called in several ways. The simplest is my_string() with the parenthesis making it clear that it is a method call.
Generally, you would carefully consider the wisdom of using the same name for a variable as a method in the same scope.
Exercise6. Write a method called convert that takes one argument which is a temperature in degrees Fahrenheit.
This method should return the temperature in degrees Celsius.
To format the output to say 2 decimal places, we can use the Kernel's format method.
For example, if x = 45.5678 then format("%.2f", x) will return the string "45.57". This is also to say that if x = 10 then format("$.2f", x) will return the string "10.00"
Another way is to use the round function as follows: puts (x*100).round/100.0
def convert(temperature_in_F) | |
(temperature_in_F - 32) / 1.8 # This solution can cause problems. Why? | |
end | |
puts "%.2f" % convert(-40) # => -40.00 |