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
0 comments:
Post a Comment