the singularity of being and nothingness
Learning Ruby: Day 5
Today’s excursion into Ruby is a short one. The 3 Koans (constants, control statements, and true/false) are pretty basic. However, upon completing them, I am now officially over 50% done with Ruby Koans!
Of course, I am by no means ready to really start doing anything with Ruby. However, I do feel like I am grasping basic concepts, and my journey through the Koans is becoming less of flailing blindly and more of thinking about what I’ve learned and trying to reason through the challenges I encounter. Granted, not impressive, but it is progress 🙂
Constants
The first Koan dealt with the concept of constants. The role of constants in programming is pretty well-understood, so there’s nothing difficult to understand about their usage in Ruby…in fact, the Koan dealt mostly with how to access constants (within and outside of their definitions relative to particular classes). I did find a nice, more in-depth explanation of the role and usage of constants within Ruby. Check it out when you have a chance.
Control Statements
Ah, what would programming be without control (if, else, for, while…) statements? Probably not worth doing!!
A couple cool things I want to point out about control statements in Ruby.
First, I’m warming to the idea of “statement modifiers”. For example, consider the following:
def mytest()   value = if true       :somethingtrue      else       :somethingfalse    end end
Pretty straightforward, a standard if->else convention. However, in Ruby, you can do something like this:
def mytest()    value = :somethingfalse    value = :somethingtrue if true end
Notice the bit at the end of the third line, the ending “if true”. This accomplishes the same thing as the first example, without the *bulk* of the extra lines. By default, Ruby will return the first “value”, since methods ALWAYS return something. However, the value is overwritten with :somethingtrue if true is…well…true. Probably not a huge advantage either way, but it’s kind of an interesting way to go about it.
The other thing I wanted to mention is related to the first in that it uses a statement modifier. However, instead of using an “if x” evaluator, this uses “unless”:
def mytest()    value = :somethingfalse    value = :somethingtrue unless false end
Nothing terribly profound about that, but it’s a nice way of shortcutting a bunch of unnecessary evaluation, if suitable.
True and False
Coming from a ColdFusion background, this one was a bit interesting. ColdFusion has interesting ways of dealing with booleans, and interesting “things” which evaluate as booleans. Check out Mark Kruger’s excellent article on the subject.
If you are from the same background, a word of warning: Ruby treats true and false in different ways than ColdFusion!
Here’s a rundown, from the Koan, of some ways that Ruby deals with true and false:
true => true false => false nil => false 1 => true 0 => true [] => true {} => true "icecream" => true "" => true
In a nutshell, Ruby treats “false” and “nil” as false. Beyond that, everything else—EVEN EMPTY STRING?!?–is treated as “true”. Interesting 🙂
Wrapping Up
Yay, 5 days of Ruby…done! Onward!
Print article | This entry was posted by existdissolve on January 26, 2012 at 2:04 am, and is filed under Cool Stuff, Ruby. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |