Skip to main content

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 :)


jjinux said…
The next post in the series is:

Popular posts from this blog

Ubuntu 20.04 on a 2015 15" MacBook Pro

I decided to give Ubuntu 20.04 a try on my 2015 15" MacBook Pro. I didn't actually install it; I just live booted from a USB thumb drive which was enough to try out everything I wanted. In summary, it's not perfect, and issues with my camera would prevent me from switching, but given the right hardware, I think it's a really viable option. The first thing I wanted to try was what would happen if I plugged in a non-HiDPI screen given that my laptop has a HiDPI screen. Without sub-pixel scaling, whatever scale rate I picked for one screen would apply to the other. However, once I turned on sub-pixel scaling, I was able to pick different scale rates for the internal and external displays. That looked ok. I tried plugging in and unplugging multiple times, and it didn't crash. I doubt it'd work with my Thunderbolt display at work, but it worked fine for my HDMI displays at home. I even plugged it into my TV, and it stuck to the 100% scaling I picked for the othe

ERNOS: Erlang Networked Operating System

I've been reading Dreaming in Code lately, and I really like it. If you're not a dreamer, you may safely skip the rest of this post ;) In Chapter 10, "Engineers and Artists", Alan Kay, John Backus, and Jaron Lanier really got me thinking. I've also been thinking a lot about Minix 3 , Erlang , and the original Lisp machine . The ideas are beginning to synthesize into something cohesive--more than just the sum of their parts. Now, I'm sure that many of these ideas have already been envisioned within , LLVM , Microsoft's Singularity project, or in some other place that I haven't managed to discover or fully read, but I'm going to blog them anyway. Rather than wax philosophical, let me just dump out some ideas: Start with Minix 3. It's a new microkernel, and it's meant for real use, unlike the original Minix. "This new OS is extremely small, with the part that runs in kernel mode under 4000 lines of executable code.&quo

Haskell or Erlang?

I've coded in both Erlang and Haskell. Erlang is practical, efficient, and useful. It's got a wonderful niche in the distributed world, and it has some real success stories such as CouchDB and Haskell is elegant and beautiful. It's been successful in various programming language competitions. I have some experience in both, but I'm thinking it's time to really commit to learning one of them on a professional level. They both have good books out now, and it's probably time I read one of those books cover to cover. My question is which? Back in 2000, Perl had established a real niche for systems administration, CGI, and text processing. The syntax wasn't exactly beautiful (unless you're into that sort of thing), but it was popular and mature. Python hadn't really become popular, nor did it really have a strong niche (at least as far as I could see). I went with Python because of its elegance, but since then, I've coded both p