Archive for September, 2008

ruby aliasing

irb(main):001:0> class Foo
irb(main):002:1>   alias model class
irb(main):003:1>   def bar
irb(main):004:2>     p self.model
irb(main):005:2>   end
irb(main):006:1>   def baz
irb(main):007:2>     p model
irb(main):008:2>   end
irb(main):009:1>   def self.bam
irb(main):010:2>     p model
irb(main):011:2>   end
irb(main):012:1> end
=> nil
irb(main):013:0> f = Foo.new
=> #
irb(main):014:0> f.bar
Foo
=> nil
irb(main):015:0> f.baz
Foo
=> nil
irb(main):016:0> Foo.bam
NameError: undefined local variable or method `model’ for Foo:Class
        from (irb):10:in `bam’
        from (irb):16
        from :0

irb(main):017:0> Foo.model
NoMethodError: undefined method `model’ for Foo:Class
        from (irb):17
        from :0

Thread local variables in ruby

The following is taken from the Pickaxe

As we described in the previous section, a thread can normally access any variables that are in scope when the thread is created. Variables local to the block of a thread are local to the thread, and are not shared.But what if you need per-thread variables that can be accessed by other threads — including the main thread? Thread features a special facility that allows thread-local variables to be created and accessed by name. You simply treat the thread object as if it were a Hash, writing to elements using []= and reading them back using [].

Thread.current[:thread_local_var] = :foo
Thread.current[:thread_local_var] # prints foo

class and module order relation in ruby

irb(main):001:0> class Stone
irb(main):002:1> end
=> nil
irb(main):003:0>
irb(main):004:0* module Alive
irb(main):005:1>   # superset
irb(main):006:1* end
=> nil
irb(main):007:0>
irb(main):008:0* class Reptile
irb(main):009:1>   # subset of alive creatures
irb(main):010:1*   include Alive
irb(main):011:1> end
=> Reptile
irb(main):012:0>
irb(main):013:0* class Mammal
irb(main):014:1>   # subset of alive creatures
irb(main):015:1*   include Alive
irb(main):016:1> end
=> Mammal
irb(main):017:0>
irb(main):018:0* class Dolphin < Mammal
irb(main):019:1>   # subset of alive creatures
irb(main):020:1* end
=> nil
irb(main):021:0>
irb(main):022:0* Alive > Stone
=> nil
irb(main):023:0> Alive > Reptile
=> true
irb(main):024:0> Alive > Mammal
=> true
irb(main):025:0> Alive > Dolphin
=> true
irb(main):026:0>
irb(main):027:0* Stone   < Alive
=> nil
irb(main):028:0> Reptile < Alive
=> true
irb(main):029:0> Mammal  < Alive
=> true
irb(main):030:0> Dolphin < Alive
=> true
irb(main):031:0>
irb(main):032:0* Dolphin < Mammal
=> true
irb(main):033:0> Mammal > Dolphin
=> true
irb(main):034:0>
irb(main):035:0* Dolphin > Mammal
=> false
irb(main):036:0> Mammal < Dolphin
=> false
irb(main):037:0>
irb(main):038:0* Reptile > Mammal # unrelated creatures
=> nil
irb(main):039:0> Reptile < Mammal # unrelated creatures
=> nil
irb(main):040:0> Mammal < Reptile # unrelated creatures
=> nil
irb(main):041:0> Mammal > Reptile # unrelated creatures
=> nil

Patterns for require (3)

dir = File.dirname(__FILE__)
require dir + '/core_ext'
require dir + '/randexp/parser'
require dir + '/randexp/reducer'
require dir + '/randexp/dictionary'
require dir + '/randgen'
require dir + '/wordlists/real_name'

exceptions in ruby method, class and module definitions

taken from The Book Of Ruby

def calc
  result = 1/0
rescue Exception => e
  puts( e.class )
  puts( e )
  result = nil
  return result
end 

class X
  @@x = 1/0
rescue Exception => e
  puts( e.class )
  puts( e )
end 

module Y
  @@x = 1/0
rescue Exception => e
  puts( e.class )
  puts( e )
end