: 
http://rubylearning.com/ http://rubylearning.com/satishtalim/tutorial.html http://rubylearning.com/download/downloads.html http://rubylearning.com/other/certification.html http://rubylearning.com/blog/ http://rubylearning.com/other/ruby_news.html http://rubylearning.com/other/testimonials.html http://rubylearning.com/jobs/ruby_jobs.html http://rubylearning.com/other/ruby_gurus.html http://rubylearning.com/satishtalim/services.html http://rubylearning.com/contact/contact.html http://rubylearning.com/satishtalim/about.html

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

puts 365*24*60


2. Write a Ruby program that asks for a numeric value of the temperature in degrees Fahrenheit. Finally, the program displays the equivalent value in degrees Centigrade. 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 a string 45.57. Another way is to use the round function as follows puts (x*100).round/100.0
Program p006ftoc.rb

# ftoc.rb

puts 'Enter temperature in Fahrenheit: '

STDOUT.flush

temp_in_fahrenheit = gets.chomp

temp_in_celsius = (((temp_in_fahrenheit.to_f - 32.0) / 9.0) * 5.0)

      puts 'Temperature ' + temp_in_fahrenheit + ' degree Fahrenheit = ' + format("%.2f", temp_in_celsius) + ' degree Celsius'

3. Write a Ruby program that asks for a year and then displays to the user whether the year entered by him/her is a leap year or not. Program p016leapyear.rb

=begin

Program to determine if a year is a leap year.

To determine if a year is a leap year, follow these steps:

1.   If the year is evenly divisible by 4, go to step 2. Otherwise, go to step 5.

2.   If the year is evenly divisible by 100, go to step 3. Otherwise, go to step 4.

3.   If the year is evenly divisible by 400, go to step 4. Otherwise, go to step 5.

4.   The year is a leap year (it has 366 days).

5.   The year is not a leap year (it has 365 days).

 

The above logic is combined into a single if check below

=end

 

# Get the input and determine if it is a leap year

puts "Enter the year: "

STDOUT.flush

input_year = gets.chomp.to_i

if ((input_year % 4 == 0) && (input_year % 100 > 0)) || (input_year % 400 == 0)

  puts "Year #{input_year} is a leap year"

else

  puts "Year #{input_year} is not a leap year"

      end

4. Write a method leap_year. Accept a year value from the user, check whether it's a leap year and then display the number of minutes in that year. Program p017leapyearmtd.rb

def leap_year(input_year)
  ((input_year % 4 == 0)  && (input_year % 100 > 0)) || (input_year % 400 == 0)
end

# Get the input and determine if it is a leap year
puts "Enter the year"
STDOUT.flush
input_year = gets.chomp.to_i
if leap_year(input_year)
  puts "Year #{input_year} is a leap year and has #{366*60*24} minutes in the year"
else
  puts "Year #{input_year} is not a leap year and has #{365*60*24} minutes in the year"
end

5. Given a string s = 'key=value', create two strings s1 and s2 such that s1 contains key and s2 contains value. Hint: Use some of the String functions. Program p021rangesex.rb

s = 'key=value'
i = s.index('=')
s1 = s[0...i]
puts s1
s2 = s[i+1,s.length]
puts s2

6. This assignment is from Chris Pine's Book.
a. Write a Deaf Grandma program. Whatever you say to grandma (whatever you type in), she should respond with HUH?!  SPEAK UP, SONNY!, unless you shout it (type in all capitals). If you shout, she can hear you (or at least she thinks so) and yells back, NO, NOT SINCE 1938! To make your program really believable, have grandma shout a different year each time; maybe any year at random between 1930 and 1950. You can't stop talking to grandma until you shout BYE.

# p026zdeafgm1.rb
a = ( 1930...1951).to_a

puts 'Enter your response: '
STDOUT.flush
until (response = gets.chomp).eql?('BYE')
  if (response.eql?(response.upcase ))
    puts 'NO, NOT SINCE   ' + a[rand(a.size)].to_s  + ' !'
  else
    puts 'HUH?!   SPEAK UP, SONNY!'
  end
  puts 'Enter your response: '
  STDOUT.flush
end

b. Extend your Deaf Grandma program: What if grandma doesn't want you to leave? When you shout BYE, she could pretend not to hear you. Change your previous program so that you have to shout BYE three times in a row. Make sure to test your program: if you shout BYE three times, but not in a row, you should still be talking to grandma.


# p026zdeafgm2.rb
a = ( 1930...1951).to_a

puts 'Enter your response: '
STDOUT.flush
until (response = gets.chomp).eql?('BYE BYE BYE')
  if response.eql?('BYE')
    # do nothing
  elsif response.eql?(response.upcase)
    puts 'NO, NOT SINCE   ' + a[rand(a.size)].to_s  + ' !'
  else
    puts 'HUH?!   SPEAK UP, SONNY!'
  end
  puts 'Enter your response: '
  STDOUT.flush
end

7. Write a Ruby program (call it p028swapcontents.rb) to do the following. Take two text files say A and B. The program should swap the contents of A and B ie. after the program is executed, A should contain B's contents and B should contains A's.


# p028swapcontents.rb - Program to swap the contents of 2 text files

# Asuumptions: The two files exist in the same folder as the program

 

# Function to read contents of one file and write them to another file

# Accepts 2 file names - file1 and file2

# Reads from file1 and writes to file2

def filereadwrite(file1, file2)

  f2 = File.open(file2, "w")

  f1 = File.open(file1, "r")

  while line = f1.gets

    f2.puts line

   end

  f1.close

  f2.close

end

 

filereadwrite("file1", "file1.tmp")

filereadwrite("file2", "file1")

filereadwrite("file1.tmp", "file2")

 

      File.delete('file1.tmp')

8.
Write a Ruby program that, when given an array as collection = [1, 2, 3, 4, 5] it calculates the sum of its elements. Program p020arraysum.rb

collection = [1, 2, 3, 4, 5]

sum = 0

collection.each {|i| sum += i}

      puts sum

9.
Write a Ruby program that, when given an array as collection = [12, 23, 456, 123, 4579] it displays for each number, whether it is odd or even. Program p021oddeven.rb

collection = [12, 23, 456, 123, 4579]

collection.each do |i|

  if i % 2 == 0

    puts "#{i} is even"

  else

    puts "#{i} is odd"

  end

      end

10. Previously you had written a program that swapped the contents of two text files. Modify that program to include exception handling.

11.
Write a test suite for a simple class called Student. This class stores a first name, a last name, and an age: a person's full name is available as a computed value.




Learning Ruby Navigation                                                                              <JRuby Tutorial  | TOC  | Ruby Quirks>