Showing posts with label elsif. Show all posts
Showing posts with label elsif. Show all posts

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.