Third (code)

<p>Unit Conversion ^ Ruby Example Code</p>

<p>&lt;&lt;Adder Test units.rb Unit Conversion Driver&gt;&gt; This is a lot of code to demonstrate something simple. The module is a convenient place to group defintions which work together. In this case, to perform unit conversions. The next file is a short driver which uses this module.</p>

@@

  1. Here's a different way to add up an array.

fred = 4, 19, 3, 7, 32 sum = 0 fred.each { |i| sum += i } print "Sum of ", fred.join(" "), " is #{sum}\n"

  1. Or create a secret message.

key = { 'A' => 'U', 'B' => 'Q', 'C' => 'A', 'D' => 'F', 'E' => 'D', 'F' => 'K', 'G' => 'P', 'H' => 'W', 'I' => 'N', 'J' => 'L', 'K' => 'J', 'L' => 'M', 'M' => 'S', 'N' => 'V', 'O' => 'Y', 'P' => 'O', 'Q' => 'Z', 'R' => 'T', 'S' => 'E', 'T' => 'I', 'U' => 'X', 'V' => 'B', 'W' => 'G', 'X' => 'H', 'Y' => 'R', 'Z' => 'C' } print "\nThe encoded message is: " "The secret message".each_byte do | b |

   b = b.chr.upcase
   if key.has_key?(b) then

print keyb

   else

print b

   end

end print "\n"

  1. But give us the info to read it anyway.

print "The key is: " ct = 8 key.each { | k, v |

   if ct == 8 then 
       print "\n   "

ct = 0

   else

print ", "

   end
   ct = ct + 1
   print "#{v} => #{k}"

} print "\n\n"

  1. Some interesting things from Integer.

3.times { print "Hi! " } print "\n"

print "Count: " 3.upto(7) { |n| print n, " " } print "\n" @@

Page top