Skip to content

puts “Rudiments of Ruby : Level Zero.Three”

November 14, 11

Exercise 5 : More Variables and Printing

It’s about “format string”. Every time you put double-quotes around a piece of text you have made a string. You can print the string, save them to files, send them to web servers, all sorts of thing. This exercise was about making strings that have variables embedded in them.

my_name = 'Zed A. Shaw'
my_age = 35 # Not a lie
my_height = 74 #inches
my_weight = 180 #lbs
my_eyes = 'Blue'
my_teeth = 'white' # not a lie :)
my_hair = 'Brown'

puts "Let's talk about %s" %my_name
puts "He's %d inches tall" %my_height
puts "He's %d pounds heavy" %my_weight
puts "Actually that's not too heavy."
puts "He's got %s eyes %s hair" % [my_eyes,my_hair]
puts "His teeth are usually %s depending on the coffee" % my_teeth

#this line is tricky, try to get it exactly right
puts "If I add %d,%d and %d I get %d" %[my_age,my_height,my_weight,my_age+my_height+my_weight]

The tricky line here was the arithmetic operations can directly performed on variables, the result would be assigned to corresponding variables embedded in the formatted string.

Extra Credits

1. Change all the variable so there isn’t the my_ in front. Make sure you change them everywhere, not just the place where you used = to set them

This exercise was definitely to make the programmer to habituate renaming variables correctly, if done. Quite often programmers change variable names assignment or declaration, but fail to change them in other parts of the code. The better way would be to use the editors ‘Find and Replace’ feature, but should be careful while applying Replace All.

2. Try more format sequencing.

Now time to try %f – Float? nothing flashed other than floating point.
Lets try out a floating format

It prints the number in floating values with six digit precision.

3. Search online for all of the Ruby format sequences

Googling times. Googling “format sequences in ruby like %d %f” resulted http://www.ruby-forum.com/topic/131303. This forum has a list of format sequences

           b   | Convert argument as a binary number.
           c   | Argument is the numeric code for a single character.

           d   | Convert argument as a decimal number.

           E   | Equivalent to `e', but uses an uppercase E to indicate
               | the exponent.

           e   | Convert floating point argument into exponential notation
               | with one digit before the decimal point. The precision
               | determines the number of fractional digits (defaulting to six).

           f   | Convert floating point argument as [-]ddd.ddd,
               | where the precision determines the number of digitsafter
               | the decimal point.

           G   | Equivalent to `g', but use an uppercase `E' in exponent form.

           g   | Convert a floating point number using exponential form
               | if the exponent is less than -4 or greater than or
               | equal to the precision, or in d.dddd form otherwise.

           i   | Identical to `d'.

           o   | Convert argument as an octal number.

           p   | The valuing of argument inspect.

           s   | Argument is a string to be substituted. If the format
               | sequence contains a precision, at most that many characters
               | will be copied.

           u   | Treat argument as an unsigned decimal number. Negative integers
               | are displayed as a 32 bit two's complement plus one for the
               | underlying architecture; that is, 2 ** 32 + n. However, since
               | Ruby has no inherent limit on bits used to represent the
               | integer, this value is preceded by two dots (..) in order to
               | indicate a infinite number of leading sign bits.

           X   | Convert argument as a hexadecimal number using uppercase
               | letters. Negative numbers will be displayed with two
               | leading periods (representing an infinite string of
               | leading 'FF's.

           x   | Convert argument as a hexadecimal number.
               | Negative numbers will be displayed with two
               | leading periods (representing an infinite string of
               | leading 'ff's.

I tried out most of the above format, and here are the results.


4. Try to write some variables that converts the inches and pounds to centimeter and kilos. Do not just type in the measurements. Work out the math in Ruby.

How I converted inches into centimeter, pounds to kilos?

This exercise needed Googling to find the solution, it’s no more a straight forward simple exercises. Slowly started learning the hard way ?!

j= 'Learning'
puts " Happy %s" % [j]
Advertisement
One Comment leave one →
  1. February 18, 12 1:38 AM

    I found more about the format sequences, here: http://ruby-doc.org/core-1.9.3/Kernel.html
    Look at “format”, it’s a little bit down the page. :)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.