Thursday, February 05, 2009

Ruby: A Python Programmer's Perspective Part II

This is a somewhat random list of things that were interesting or surprising to me when I read Ruby for Rails. You may also be interested in my previous post: Ruby: A Python Programmer's Perspective.

In Python, directories map to packages and files map to modules. This is similar to Java. Ruby is not like this. Ruby is more like Perl. Basically, any file in any directory can contain code for any module. That means that there are module declarations in the code itself.

Ruby has global variables. They start with $, such as $gvar.

A variable defined at the top-level of a file is not global. The top-level has its own local scope, just as class, module, and method definition blocks each have their own local scope::
>> a = 1
=> 1
>> def f()
>> p a
>> end
=> nil
>> f
NameError: undefined local variable or method `a' for main:Object
from (irb):3:in `f'
from (irb):5
This is different than Python that treats anything at the top-level of a file as a module-level global.

Ruby does not segregate global variables into modules. They're really global:
>> module M
>> $a = 'foo'
>> end
=> "foo"
>> $a
=> "foo"
Variables that start with an uppercase letter are constants. Hence, you can't treat them like normal variables:
>> def a
>> L = 1
>> end
SyntaxError: compile error
(irb):13: dynamic constant assignment
L = 1
from (irb):14
The way private methods work is a bit unusual compared to C++, Java, etc. A private method is a method that cannot be called on any object other than the implicit self. That means, you cannot have an explicit receiver--even if the receiver is of the same class!
>> class C
>> def public_method
>> puts "Public!"
>> private_method # An implicit receiver is okay.
>> end
?> def someone_elses_private(other)
>> other.private_method # An explicit receiver is not.
>> end
?> private
>> def private_method
>> puts "Private!"
>> end
>> end
=> nil
?> c =
=> #<C:0x34911c>
>> c.public_method
=> nil
>> d =
=> #<C:0x346cdc>
>> c.someone_elses_private(d)
NoMethodError: private method `private_method' called for #<C:0x346cdc>
from (irb):33:in `someone_elses_private'
from (irb):45
That means you can't even explicitly write "self.private_method"!

Ruby has variables at the class level and it has class variables, and they're not the same:
>> class C
>> @class_instance_variable = 'hi'
?> def self.class_scope_method
>> p @class_instance_variable # This works.
>> end
?> def set_class_variable
>> @@class_variable = 'bar'
>> end
?> def print_class_variable
>> p @@class_variable # This works.
>> puts "Now, try @@class_instance_variable:"
>> p @@class_instance_variable # This doesn't.
>> end
>> end
=> nil
?> C.class_scope_method
=> nil
>> c =
=> #<C:0x31946c>
>> d =
=> #<C:0x317fe0>
>> c.set_class_variable
=> "bar"
>> d.print_class_variable
Now, try @@class_instance_variable:
NameError: uninitialized class variable @@class_instance_variable in C
from (irb):77:in `print_class_variable'
from (irb):85
The book says:
Class variables, like @@subclasses...are scoped in such a way that they are visible when self is the class to which they belong, a descendant (to any level) of that class, or an instance of the class or its descendants. Despite their name, they're not really class scoped; they're more like hierarchy scoped. Matz has mentioned plans to change the scoping of class variables in future versions of Ruby so that their visibility is more confined to the class (or module; modules can have class variables too) where they're defined. [p. 364]
These are all pretty much the same:
>> x = 11
=> 11
>> if x > 10: puts x; end
=> nil
>> if x > 10 then puts x; end
=> nil
>> puts x if x > 10
=> nil
Ruby has "===" and "==". "===" is called the "threequal operator" and it is the backbone of the case statement. Every class can define "===" and "==" in any way it sees fit, and they may not be the same. Ruby does not have an "is" keyword like Python does.

Ruby has a do/while construct like C:
>> begin
?> puts 'hi'
>> end while false
=> nil
This is something that Python lacks, probably because the syntax doesn't fit into the language.

Compared to C, C++, Java, and JavaScript, I think Ruby finally got the switch statement right. There's no need to use the word break:
case "foo"
when "foo"
puts "yep"
when "bar"
puts "impossible!"
puts "huh?"
Ruby has an interesting method-level syntax for exceptions handling:
>> def f
>> 1/0
>> rescue
>> puts "safe!"
>> end
=> nil
>> f
=> nil
Okay, that's it for this post, but I've got more coming :)

1 comment:

Shannon -jj Behrens said...

The next post in the series is: