Saturday, February 5, 2011

Separating Logic and Output in Ruby

Just finished up Chapter 4 of Beginning Ruby, which walks you through creating a basic application that pulls information from a separate text file and performs a few calculations based on the contents of the file. This was a good exercise, because up until now I'd just been coding information directly into the file itself, and coding so that Ruby can pull information from elsewhere is obviously more versatile and useful.

But here's a question. It seems like it makes sense to separate logic from output. So that this bit of code:

word_count = text.split.length
puts "#{word_count} words"
paragraph_count = text.split(/\n\n/).length
puts "#{paragraph_count} paragraphs"

Should really be written as:

word_count = text.split.length
paragraph_count = text.split(/\n\n/).length

puts "#{word_count} words"
puts "#{paragraph_count} paragraphs"

Seems like doing it this way makes it easier to go back and edit code later, since the two won't be mixed together. I'm sure there are exceptions to this rule, however.

On a side note, the program I wrote scanned a text file and displayed information based on the text file. But it kept throwing in some junk characters, adding a "\92" after every sentence. So I went back through my code, trying to figure out what I'd messed up. The real culprit was the text I'd copied and pasted from the internet, and clearing the formatting of the text file got rid of the junk characters. Awesome!

I'd like to thank everyone who's given me advice or commented on my blog or otherwise encouraged me to learn Ruby. I'm having a great time so far. I think about what I know about Ruby today and how it was completely unfathomable to me a week ago, and then I think about what I'll know next week that is completely unfathomable to me right now and I can't help but get excited.

Friday, February 4, 2011

Regular Expressions in Ruby

I'm about halfway through Chapter 4 of Beginning Ruby. Beginning Ruby has told me to write bits of code like so:

puts "This is a test".scan(/\w+/).length

(which counts the elements in "This is a test"

It was easy enough to grasp WHAT the code did, but HOW it did it was a bit trickier. And I get hung up on these kinds of things, but I had no idea what the / and \ were for. But I've got it now. The forward slashes signal the beginning and end of a regular expression, and the \ is a stop, which tells Ruby the character following it is a character and not code. This doesn't apply to characters that have no other meanings in regular expressions.

And the pipes are just used to separate information inside a regular expression. So in a bit of code such as

We have the forward slashes signaling the beginning and end of the regular expression, the backslashes telling Ruby that the . and the ? are characters to be counted by this code, not code itself and the pipes telling Ruby there's a new piece of information ahead. And again, a backslash is not needed for the exclamation mark, because it has no other meaning inside of a regular expression.

I think the best thing I can do is to remember to get a handle on big picture things, and not get hung up on the syntax.

Thursday, February 3, 2011

Working with Nesting Logic in Ruby

So you want to talk about nesting logic? Ok, let's do it. We can talk about elsif too.

For a while, I didn't get why a piece of code such as:

puts "I don't need an end, dude"

Didn't need to be written as

puts "I don't need an end, dude"
end

While something like this:

fruit = "potato"
if fruit == "orange"
color = "orange"
puts "You have an orange."
elsif fruit == "apple"
color = "green"
puts "You have a green apple."
elsif fruit == "banana"
color = "yellow"
puts "You have a banana."
else
color = "unknown"
puts "You have something that I don't know what it is."
end

DOES need the end delimiter. (I didn't know this was called a delimiter. Is that what everyone calls it? It sounds more like something the Ghostbusters would use than a piece of code but ok)

But when I was finishing up Chapter 3 in Beginning Ruby, it made sense. You need the end to tell Ruby that the previous argument is over. It's DONE, Ruby. Let's start a new one! Am I thinking about this in the right way?

So far my learning has been something like I'm given a piece of the puzzle, by Beginning Ruby or a friend or a tutorial online and I don't fully get what the piece is for until I work a little further.

Also while I was working on stuff tonight I built a perpetual motion machine.

x = 1
while x = 1
puts x
end

Perpetual until you stop the task anyway!

Really looking forward to digging into Chapter 4 this weekend. It walks you through building a functional (though basic) Ruby program and I think some real practical experience will do me a world of good.

Wednesday, February 2, 2011

Working with Arrays in Ruby

Since I've been off work for two days thanks to ICEPOCALYPSE 2011, I've had some extra time to work with Ruby. I haven't been as diligent as I'd like. But I've spent a good couple hours each day working through Beginning Ruby and reading about Ruby online, so it's not a total wash.

Today I've been learning about arrays. Arrays are collections of elements and once elements have been put into an array, you can tell Ruby to manipulate them in various ways.

Here's some example code from Beginning Ruby:

x = [3, 7, 8, 9]
puts x.first
puts x.last

This makes sense. We have an array known as x, with four elements (3, 7, 8, and 9)
The rest of the code will display the first element and then the last element (3 and 9)

Once I saw how that worked, I wondered if some of the other ways of manipulating data I've learned previously would work. So I expanded on the example code:

x = [3, 7, 8, 9]
puts x.first
puts x.last
z = x.last - x.first
puts z
y = x.last / x.first
puts y
a = x.last ** x.first
puts a
puts "Yes, 9 is larger than 3" if x.last > x.first

So now in addition to listing the first and last element, the code also subtracts the 3 from 9, divides 9 by 3, multiples 9 by the third power, and then displays that 9 is, in fact, larger than 3.

Ruby is easy to work with, once you've got an idea of how it works. Learning Ruby won't be a matter of memorizing every possible bit of code, but a matter of learning how to tell it to perform a series of tasks.

My goal for the evening is to get through the end of chapter 3 (Hashes await!) so I can get started on chapter 4, which will take me through building an actual program.

Tuesday, February 1, 2011

Syntax Issues in Ruby

Still working through Chapter 3 of Beginning Ruby. It's slow going because every time I get to an example (and there are lots of examples) I have to stop and do it, then run it to see what it does. Then I try to change the code around using what I already know. Sometimes this works. I'm actually surprised at how often I can predict what will work in Ruby.

Sometimes it doesn't work. Take this bit of code for example. All it does is check a string for vowels and returns a result.

This code is valid:

x = "The quick brown fox jumped over the lazy dogs."
puts "This string has vowels" if x =~ /[aeiou]/

This code is not valid:

x = "The quick brown fox jumped over the lazy dogs."
if x =~ /[aeiou]/ puts "This string has vowels"

I don't get why this doesn't work. All I've done is rearranged the bits of code. It's possible I need a bit more code to make this work this way and it's possible it just doesn't work this way. It's also possible I'm just getting ahead of myself (again) but I've never been able to learn something, even the basics of something, without wanting to mess around with it and see what else it could do.

Either way, the example from the book didn't use a variable. In place of x the book had "Test string" which still returns the same result, "This string has vowels" but is less useful because a variable is more useful. If this was a bit of code I was actually going to use for something, I could pull that variable from anywhere - a file, user input, etc. So I don't have a problem understanding variables, at least.

How I Learned to Stop Worrying and Love the Code

Ok so here's what's up. I was at a housewarming party helping some old friends warm up their brand new house this past Saturday. My friend Eli (also known in some circles as jqr) gave me the same pitch he gives me every 4 or 5 months which is basically:

"Get off your ass and learn Ruby."

So that's what I'm doing. I don't consider myself a programmer or a coder by any stretch of the imagination, but I've been thinking about learning Ruby off and on for about 4 years now. So here we are. I've spent some times with Beginning Ruby (I'm about midway through Chapter 3 now) and some time with Hackety Hack (nice, simple, easy to understand, perhaps a bit limited) and I've gone through Ruby In 20 Minutes (which is another good supplemental thing, and showed me how to produce a finished program, but I'm still feeling a bit lost)

So far I'm enjoying what I'm doing and my biggest problems are remembering the correct order of the syntax (a common problem for any beginner) and understanding just HOW these things I'm learning to do (strings, arrays, etc) fit together to make a program. I'm probably getting ahead of myself, but it's like learning how to make a nut or a bolt that will eventually go into making a car but not having any idea how the car gets put together.

Still, what I'm reminding myself is just to do a little each day, and build on what I've learned and I'll have some EUREKA! moments. I've learned complicated things before and this is how it always works. The key is not to get overwhelmed with the material and to take it slow and begin with the basics.

puts "Ruby is awesome!"

Now run and tell THAT.