Flylib.com

Books Software

 
 
 

Dictionaries Are Hashes

 
   

Ruby Way
By Hal Fulton
Slots : 1.0
Table of Contents
 


Dictionaries Are Hashes

Use => instead of : to indicate pairings in a hash. Hash keys can be mutable objects (see rehash ). Inverting a hash does not automatically group duplicates into an array; you will lose entries if there were duplicate values.


hsh = { } hsh['key'] = 7 hsh2 = { "A"=>65, "B"=>66 }

Ruby supports default values for hashes.


   

 

 

 
   

Ruby Way
By Hal Fulton
Slots : 1.0
Table of Contents
 


Classes

Defining a new class creates a new context nested in the enclosing class (class Object by default). Use the :: scope operator to access nested classes/modules and their constants or class methods .


class MyClass #

name

must be capitalized C = "constant in MyClass" class Inner C = "in MyClass::Inner" end end p MyClass::C # constant in MyClass p MyClass::Inner::C # in MyClass::Inner

Existing classes, both built-in and newly defined, may be reopened and altered or extended at any time (even from within a C extension). Any existing instances will be updated with the new class definitions.


class String # existing built-in class def my_method puts "length: #{ length} " # implicit self end end "abc123".my_method # length: 6

Any class may be subclassed; use the < symbol instead of parentheses. Ruby classes support single inheritance only. Modules are used to add shared functionality among classes (and objects) in place of using multiple inheritance.

Use initialize instead of __init__ for the "constructor." You do not need to pass in self for method definitions, since Ruby automatically binds it for you. Remember dot notation is for method calls only, so prepend instance variables with the @ symbol instead of self .

Note

Prepending a method name with self within a class definition is optional, since it will be called from within the context of the actual instance by default.


Use super to call the same-named method from the superclass (or an ancestor thereof).


class MyString < String def initialize(str) @str = str # instance variable super # call String#initialize(str) end end

Instantiate an object via the Class#new method. It will call any defined initialize method after allocating the object.


mystr = MyString.new("text here")

There is no equivalent to Python's __del__ hook, since Ruby does not use reference counting (see the section "Garbage Collection"). This may require you to organize your code differently.

Ruby has both class variables and class methods.


class MyClass @@class_variable = "Accessible to this class and subclasses" MyConstant = "Globally accessible via MyClass::MyConstant" def MyClass.class_method # dot notation used in definition puts "Available as method of the class object itself" end end

Ruby has no internal-use, class-private, or magic-attribute naming like Python's _* , __* , and __*__ forms. Ruby uses the __*__ form sometimes, but only to accommodate certain naming conflicts. For example, Object#__send__ is an alias for send .


   

 

 

 
   

Ruby Way
By Hal Fulton
Slots : 1.0
Table of Contents
 


Modules

A module in Ruby is not file-based as it is in Python. It is an object with methods and attributes defined within a module/end block of code, like a class, but without the ability to be instantiated .


module MyModule # Module

names

are constants (capitalized) MyConst = "Module Constant MyModule::MyConst" $my_global = "Unrelated to module namespace (global is global)" my_local = "Inaccessible from without" module NestedModule # Access via MyModule::NestedModule end class InnerClass end def module_method # Can be accessed as MyModule.module_method end module_function :module_method end module AnotherModule # In same file... end

You can define more than one module in the same source file, and even nest them inside each other to organize your namespace. Loading an external file with module definitions is the same as defining them in the source file, and there is no requirement to give these external files the same name as your module(s).

Once defined in the source file, a module is immediately available. You can access nested modules or constants via the :: scope operator, and module functions (shared public methods) via dot notation. You may also as a whole include the module into a class namespace, or extend any object with its methods. There is no provision for selectively including constants or methods of a module as Python's import allows, because it is considered to be a single unit.

When you use this mixin approach to incorporate such shared code from multiple sources, the included modules become part of the single inheritance tree, similar to a subclass. Yes, order does matter.


class MyString < String end p MyString.ancestors # [ MyString, String, ...] module AlterString end class MyString include AlterString end p MyString.

ancestors

# [ MyString, AlterString, String, ...]

The mixin module is not copied but referenced, as in Python.