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 = 1This is different than Python that treats anything at the top-level of a file as a module-level global.
>> def f()
>> p a
NameError: undefined local variable or method `a' for main:Object
from (irb):3:in `f'
Ruby does not segregate global variables into modules. They're really global:
>> module MVariables that start with an uppercase letter are constants. Hence, you can't treat them like normal variables:
>> $a = 'foo'
>> def aThe 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!
>> L = 1
SyntaxError: compile error
(irb):13: dynamic constant assignment
L = 1
>> class CThat means you can't even explicitly write "self.private_method"!
>> def public_method
>> puts "Public!"
>> private_method # An implicit receiver is okay.
?> def someone_elses_private(other)
>> other.private_method # An explicit receiver is not.
>> def private_method
>> puts "Private!"
?> c = C.new
>> d = C.new
NoMethodError: private method `private_method' called for #<C:0x346cdc>
from (irb):33:in `someone_elses_private'
Ruby has variables at the class level and it has class variables, and they're not the same:
>> class CThe book says:
>> @class_instance_variable = 'hi'
?> def self.class_scope_method
>> p @class_instance_variable # This works.
?> def set_class_variable
>> @@class_variable = 'bar'
?> def print_class_variable
>> p @@class_variable # This works.
>> puts "Now, try @@class_instance_variable:"
>> p @@class_instance_variable # This doesn't.
>> c = C.new
>> d = C.new
Now, try @@class_instance_variable:
NameError: uninitialized class variable @@class_instance_variable in C
from (irb):77:in `print_class_variable'
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 = 11Ruby 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.
>> if x > 10: puts x; end
>> if x > 10 then puts x; end
>> puts x if x > 10
Ruby has a do/while construct like C:
>> beginThis is something that Python lacks, probably because the syntax doesn't fit into the language.
?> puts 'hi'
>> end while false
case "foo"Ruby has an interesting method-level syntax for exceptions handling:
>> def fOkay, that's it for this post, but I've got more coming :)
>> puts "safe!"