Skip to content

Title = “Rudiments of Ruby : Level Zero.Four” ; puts Title

November 17, 11

Exercise 6 : Strings and Text

This exercise is more on strings, so far we have seen two different ways of injecting variables into strings, first one use something called “string interpolation” where #{} used.

name1 = "Joe" name2 = "Mary" puts "Hello #{name1}, where is #{name2}?"

The second way is using formatted strings instead of “string interpolation”

name1 = "Dhanasekar" name2 = "Jack Sparrow" puts "Hellow %s, where is %s?" %[name1,name2]

Here was an exercise to type a whole bunch of strings, variables, formats and print them.

x = "There are #{10} types of people."
binary = "binary"
do_not = "don't"
y= "Those who know #{binary} and those who #{do_not}."

puts x
puts y
puts "I said : #{x}."
puts "I also said : '#{y}'."

hilarious = false
joke_evaluation = "Isn't that joke so funny?! "{hilarious}"
puts joke_evaluation

w = "This is the left side of...."
e= "a string with a right side."
puts w+e

What you should see

There are 10 types of people.
Those who know binary and those who don't.
I said : There are 10 types of people...
I also said : 'Those who know binary and those who don't.'.
Isn't that joke so funny?! false
This is the left side of...a string with a right side.

Extra credits
1. Go through this program and write a comment above each line explaining it.

2. Find all the places where a string is put inside a string. There are four places

Lets carefully note the strings that were put inside a string, in each line
Line 1 : #{10} – (not a) string ??, as it’s not having quotes
Line 2 : just variable assignment
Line 3 : another variable assignment
Line 4 : Binary and Do_not So is count 2
Line 5 and 6 : variable assignments
Line 7 : x count is 3 now
Line 8 : y so count is 4, so does this mean #10 in line one is not a string
Line 9 : variable assignment
Line 10 : Variable assignment but there is an injection of variable to the string, but that’s non-string assignment
Nothing found after line 10.
So, there are four strings that were put inside a string
1. Binary
2. do_not
3. x
4. y

3. Are you sure there’s only four place? How do you know? Maybe I like lying.

Ha..ha..ha… so, does this mean 10 and hilarious are strings?  By the way was he lying in extra credit two or three? :P
I decide to use my life line GG (Google God ;-) )
I couldn’t come to a conclusion if those two were considered as strings… that needs to be explored later again.

4. Explain why adding the two string w and e with + makes a longer string

This is called string concatenation, here two strings are just combined, practically there would be many times where you may need to combine any two of many strings available, based on the resulting conditions.

x = "Happy"
y=  " Learning"
puts x+y
Advertisement
3 Comments leave one →
  1. Jari Laakso permalink
    January 7, 12 3:17 PM

    Hi Dhanasekar,

    Thanks for the fun and interesting post! You have a really personal way to write.

    I am waiting for others to see the 2 bugs in the expected results you wrote. :-)

    Have a funtastic New Year 2012!

    Best regards,
    jari

  2. February 14, 12 5:43 PM

    Interesting post.More informative

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.