There are no do, no, package, and use keywords for modules. Also, modules in Ruby do not need to be in a separate specially named file. However, a separate file may contain one or more modules and can be loaded with require or load. You can then optionally "include" a named module into your namespace, but you can't selectively import individual methods or constants. Ruby modules are not aggregations of functions but rather sets of methods and constants inseparable from each other. There is no exact equivalent to the Perl AutoLoader module. There is autoload, which lets you register a file to be required when a Ruby module is first accessed. You cannot access just any variable via the :: scope operator but only constants and methods. Ruby still has Perl's BEGIN{} and END{}, but because modules and classes do not need to be allotted one per file and because there's no equivalent to Perl's Exporter, they are not really used the same way. Classes, Objects, and Methods There are no bless, ref, tie, tied, and untie keywords relating to classes. Also, existing classes, including the built-in ones, are easily extended at any time. Here's an example:
class String # pre-existing built-in class def char_at(pos) # new method being added self[pos,1] end end "abcdef".char_at(3) # "d" 123.char_at(1) # error A method added to the built-in String class only applies for string objects. Methods added to the Object class apply to all objects (compare this to Perl's UNIVERSAL class). If you make a method without specifying the class, it will belong to the Object class as a private method. Note Ruby has built-in handling of private, protected, and public methods. Creating your own class is also easythere is no need to make separate files. You will often define your own class to represent custom objects. Ruby allows for something resembling a constructor; any method named initialize will be called by the class method new when the object is created:
class Foo def initialize # Code... end end obj = Foo.new # Internally calls the initialize method Also, you can nest classes for namespace control:
class Foo class Bar end end baz = Foo::Bar.new Notice how this differs from Perl's file-based class organization, where Foo::Bar implies Foo/Bar.pm. Using include from within a class causes the module methods to be available to all the objects of the class. This is a mixin in Ruby. Use this approach in place of multiple inheritance. You can still define your classes in separate files and load them into your script with require or load. There is no use or do equivalent, but you can use include to import module constants and methods into your class. However, you cannot use it to individually import methods. There is no __PACKAGE__ or __DATA__ file literal, but you can still use __END__ for the main file. See more on the DATA object (an instance of class IO). Also, there is no INIT. Ruby uses a conservative mark/sweep garbage-collection (GC) algorithm and does not have an equivalent to DESTROY. See finalizers. Note that the GC algorithm is going to change soon, but this should be transparent to the programmer (other than a performance gain). Ruby's inheritance model is smoother:
# Perl package SomeClass use AnotherClass @ISA = ("AnotherClass") # ... # Ruby class SomeClass < AnotherClass # ... Also, overriding methods in Ruby is clearer:
# Perl $self->SUPER::somemethod() # Does lookup in @ISA # Ruby def somemethod super # Looks in superclass(es) end To obtain behavior similar to Perl's AUTOLOAD, see method_missing. Also, Ruby's built-in Struct class is analogous to Perl's class generator Class::Struct. Defining Methods and procs A proc is an object made from a block, which also maintains its original binding. A method is different in that it has a scope tied to the object and its class, and it has access to its class and instance variables and methods. Here's an example: Tip You can define methods ending in a ? or !.
def methodname(arg1, arg2="Default", *moreargs, &attachedblock) # ... # moreargs is now an array of parameters passed in if they exist # attachedblock is now a Proc if a block was provided end Note that the block is treated as a special case; ordinarily a "starred" parameter such as *moreargs would consume all the remaining parameters passed into the method. There is no direct equivalent to Perl's sub, but an anonymous subroutine in Perl is similar to a proc object in Ruby. Redefining Operator Methods By this point, you should understand that + is defined differently for number and string objects. This method takes an argument, and it is up to the implementation of the method to do the right thing with the argument. In fact, the string class method for + raises an exception if the argument is not a string. Here's an example:
x = "a" + 1 # TypeError: failed to convert Fixnum into String It is an easy thing to modify this method in Ruby:
class String def +(arg) concat arg.to_s end end Many operators in Ruby are redefinable methods, allowing you to override the built-in equivalents. These include **, !, ~, +, -, *, /, %, <<, >>, &, ^, |, <=, ==, ===, =~, [], and []=. Precedence Precedence is slightly different from what we find in Perl. For example, =~ has a lower precedence, the bitwise &, ^, and | have a higher precedence, and the keyword operators have a lower precedence. Also note that the logical operators && and || are not interchangeable with and and or regarding precedence. There is no list operator. Parentheses are used for grouping either expressions or method arguments, but not both at the same time. |