Skip to content

Instantly share code, notes, and snippets.

@kotp
Last active July 12, 2018 02:49
Show Gist options
  • Save kotp/2413004 to your computer and use it in GitHub Desktop.
Save kotp/2413004 to your computer and use it in GitHub Desktop.
RubyLearning Exercises and Answers

A collection of exercises and answers.

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

Read the sprintf documentation and the % documentation in the String class and figure out the output being printed by of this Ruby code.

puts "%05d" % 123

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)

Exercise4. Write a Ruby program that tells you how many minutes there are in a year (do not bother right now about leap years etc.).

# 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"

Exercise 5. The following program prints the value of the variable. Why?

my_string = 'Hello Ruby World'
def my_string
  'Hello World'
end
puts my_string

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment