This blog no longer exists.
You will be redirected to something cool.
Showing posts with label variables. Show all posts
Showing posts with label variables. Show all posts

Thursday, October 28, 2010

Getting in the Groove (Embedded Variables)

Yeah, I'm still on the whole embedded variables thing, but this is SOOOOOO much easier. Today I worked on getting used to doing things this way. It'll take a bit of time to memorize, but in the meantime I created a mad lib to kind of get used to it. Enjoy!

#  Is it customary to start variable counts at one? (ie. noun1) or
#  at zero (ie. noun and the second being noun1) or how else is this
#  handled?

print( 'Enter an adjective: ' )
adjective1 = gets.chomp()
print( 'Enter a verb ending in "ed": ')
verbEd = gets.chomp()
print( 'Enter a plural noun: ')
pluralNoun1 = gets.chomp()
print( 'Enter a liquid: ')
liquid = gets.chomp()
print( 'Enter another plural noun: ')
pluralNoun2 = gets.chomp()
print( 'Enter a famous person: ')
famousPerson = gets.chomp()
print( 'Enter a place: ')
place = gets.chomp()
print( 'Enter an occupation: ')
occupation = gets.chomp()
print( 'Enter a noun: ')
noun1 = gets.chomp()
print( 'Enter a nationality (ie. American or Irish): ')
nationality = gets.chomp()
print( 'Enter a female celebrity: ')
femaleCelebrity = gets.chomp()
print( 'Enter another noun: ')
noun2 = gets.chomp()
print( 'Enter the name of a female friend: ')
femaleFriend = gets.chomp()
print( 'You\'re almost done. Enter another plural noun: ')
pluralNoun3 = gets.chomp()
print( 'Enter a number: ')
number = gets.chomp()
print( 'Enter an adjective: ')
adjective2 = gets.chomp()

puts ( 'Personal Ad:')
puts
puts ( "I enjoy long, #{adjective1} walks on the beach, getting #{verbEd} in 
the rain and serendipitous encounters with #{pluralNoun1}.
I really like piƱa coladas mixed with #{liquid}, and romantic, candle-lit
#{pluralNoun2}. I am well-read from Dr. Seuss to #{famousPerson}. I travel
frequently, especially to #{place}, when I am not busy with work. (I am a
#{occupation}.) I am looking for #{noun1} and beauty in the form of a 
#{nationality} goddess. She should have the physique of #{femaleCelebrity}
and the #{noun2} of #{femaleFriend}. I would prefer if she knew how to
cook, clean, and wash my #{pluralNoun3}. I know I’m not very attractive in
my picture, but it was taken #{number} days ago, and I have since become 
more #{adjective2}. ")

Embedded Variables in Strings

So I'm learning Ruby again, but this time I've got a program in mind. It will take a lot of brushing up and general learning before I will be able to start the program. I already know how to write the program without an interface, but I want to build an interface too, so it'll take a bit of work. I think once I get this particular program coded that I'll know enough about programming to be able to work on other programs so that will be really nice.

Tonight I started reading "The Book of Ruby" which is a big step up from the last book I read, but I think I'm understanding it fairly well considering the difference in difficulty between the two texts.

So tonight's topic was Strings and Embedded Evaluation. The sample code in the lesson is as follows:

print( 'Enter your name: ' )
name = gets()
puts( "Hello #{name}" )

After reading the OTHER book on Ruby I would have programmed this like so:
puts 'Enter your name:'
name = gets.chomp
puts 'Hello ' + name + '.'

However, I learned that by using "print" instead of puts, I can keep the cursor on the same line... which is cool, I don't know what real difference it would make other than aesthetics, but I guess I'll find out later. I did add a period so that I could make it a proper sentence and added the corresponding chomp to make this possible.

What I mostly learned was this whole embedded variables thing. I'm a little confused as to the purpose -- if it's just faster to code like this and less confusing than all the plusses and whatnot from something like 'Words here' + someVariable + 'another string.' I can really see that being the case, however, I'm not going to assume that it is simply for cleaner and faster programming -- until yeah, I am told so.

I also played a bit with "\n" and "\t" which are newline and tab. It took a bit getting used to the whole new way of doing the embedded thing and playing with newline and tab helped me get used to it a bit. Here's the code I created using this newfangled stuff I learned:

# Playing with tab \t and newline \n
puts ( 'How about a sentence of sort?')
sentence = gets()
puts ( "\t #{sentence}\t\t #{sentence}")

I used the sentence "I like rocks and giraffes." (Without the quotes.) And thus, the program spits out this:
I like rocks and giraffes.
     I like rocks and giraffes.
          I like rocks and giraffes.

Definitely some stuff to play around with for a bit. I perhaps I could rewrite this to capitalize the first letter if it is not capitalized and to add a period if there is not one. I think this would be a good project for tomorrow. :P