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.new
=> #<C:0x34911c>
>> c.public_method
Public!
Private!
=> nil
>> d = C.new
=> #<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
"hi"
=> nil
>> c = C.new
=> #<C:0x31946c>
>> d = C.new
=> #<C:0x317fe0>
>> c.set_class_variable
=> "bar"
>> d.print_class_variable
"bar"
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
11
=> nil
>> if x > 10 then puts x; end
11
=> nil
>> puts x if x > 10
11
=> 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
hi
=> 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!"
else
puts "huh?"
end
Ruby has an interesting method-level syntax for exceptions handling:
>> def f
>> 1/0
>> rescue
>> puts "safe!"
>> end
=> nil
>> f
safe!
=> nil
Okay, that's it for this post, but I've got more coming :)

Comments

jjinux said…
The next post in the series is:

http://jjinux.blogspot.com/2009/02/ruby-python-programmers-perspective_05.html

Popular posts from this blog

Drawing Sierpinski's Triangle in Minecraft Using Python

In his keynote at PyCon, Eben Upton, the Executive Director of the Rasberry Pi Foundation, mentioned that not only has Minecraft been ported to the Rasberry Pi, but you can even control it with Python. Since four of my kids are avid Minecraft fans, I figured this might be a good time to teach them to program using Python. So I started yesterday with the goal of programming something cool for Minecraft and then showing it off at the San Francisco Python Meetup in the evening.

The first problem that I faced was that I didn't have a Rasberry Pi. You can't hack Minecraft by just installing the Minecraft client. Speaking of which, I didn't have the Minecraft client installed either ;) My kids always play it on their Nexus 7s. I found an open source Minecraft server called Bukkit that "provides the means to extend the popular Minecraft multiplayer server." Then I found a plugin called RaspberryJuice that implements a subset of the Minecraft Pi modding API for Bukkit s…

Apple: iPad and Emacs

Someone asked my boss's buddy Art Medlar if he was going to buy an iPad. He said, "I figure as soon as it runs Emacs, that will be the sign to buy." I think he was just trying to be funny, but his statement is actually fairly profound.

It's well known that submitting iPhone and iPad applications for sale on Apple's store is a huge pain--even if they're free and open source. Apple is acting as a gatekeeper for what is and isn't allowed on your device. I heard that Apple would never allow a scripting language to be installed on your iPad because it would allow end users to run code that they hadn't verified. (I don't have a reference for this, but if you do, please post it below.) Emacs is mostly written in Emacs Lisp. Per Apple's policy, I don't think it'll ever be possible to run Emacs on the iPad.

Emacs was written by Richard Stallman, and it practically defines the Free Software movement (in a manner of speaking at least). Stal…

Creating Windows 10 Boot Media for a Lenovo Thinkpad T410 Using Only a Mac and a Linux Machine

TL;DR: Giovanni and I struggled trying to get Windows 10 installed on the Lenovo Thinkpad T410. We struggled a lot trying to create the installation media because we only had a Mac and a Linux machine to work with. Everytime we tried to boot the USB thumb drive, it just showed us a blinking cursor. At the end, we finally realized that Windows 10 wasn't supported on this laptop :-/I've heard that it took Thomas Edison 100 tries to figure out the right material to use as a lightbulb filament. Well, I'm no Thomas Edison, but I thought it might be noteworthy to document our attempts at getting it to boot off a USB thumb drive:Download the ISO. Attempt 1: Use Etcher. Etcher says it doesn't work for Windows. Attempt 2: Use Boot Camp Assistant. It doesn't have that feature anymore. Attempt 3: Use Disk Utility on a Mac. Erase a USB thumb drive: Format: ExFAT Scheme: GUID Partition Map Mount the ISO. Copy everything from the I…