Procs ‘n’ blocks

May 1, 2008

A block in Ruby is an expression delimited by braces that has, to try to put it simple, no life by itself. Thus if we write:


irb(main):001:0> {|x| x*x}
SyntaxError: compile error

we’ll get a compile error. But blocks can be very powerful and go by the hand with other important concept in Ruby, namely the Procs. Procs, put again in a simple way, are blocks given life, thus are blocks bounded to some variables they can work with. To create a new proc, we can simply call the constructor of the class Proc or the kernel reserved word lambda, what will get us to the same thing, except that the latter will provide us argument checking. Thus:


irb(main):002:0> my_proc = Proc.new{|x| x*x}
=> #<Proc:0x00006a7c@(irb):2>
irb(main):003:0> my_other_proc = lambda{|x| x*x}
=> #<Proc:0x00086640@(irb):3>
irb(main):005:0> my_proc.call(5)
=> 25
irb(main):006:0> my_other_proc.call(5, 4)
(irb):3: warning: multiple values for a block parameter (2 for 1)
        from (irb):7
TypeError: can't convert Array into Integer
        from (irb):3:in `*'
        from (irb):3
        from (irb):7
        from :0
irb(main):007:0> my_other_proc.call(5)
=> 25

Notice that a proc is executed by using the reserved word call. That means that we have to use it instead of yield when we are passing a proc rather than a block to a method:


irb(main):032:0> def gimme_those_five
irb(main):033:1> 5.times {yield}
irb(main):034:1> end
=> nil
irb(main):035:0> gimme_those_five{puts "hi"}
hi
hi
hi
hi
hi
=> 5

but


irb(main):038:0> def gimme_just_one(kind)
irb(main):039:1> kind.call
irb(main):040:1> end
=> nil
irb(main):041:0> gimme_just_one lambda{puts "hi"}
hi
=> nil

Advertisements
class EnumerableAttribute

  attr_reader :values

  def initialize(*args)

    @values = Hash.new

    0.upto(args.size-1) do |i|
      @values[args[i].to_s] = i
    end

    @values.each do |key, value|
      EnumerableAttribute.class_eval do
        define_method key do
          value
        end
      end
    end
  end
end

This class allows the dynamical creation of enumerable types mapped to integers in the database, starting from 0. The constructor will take a list of symbols representing the keys and will create a Hash that will map each key to one integer value. Once created, we can have access to the Hash via the method values or to the integer value of each key via the method called like the symbol.

E.g.

class Car
  include EnumerableAttribute
end

>> engine_type = Car::EnumerableAttribute.new(:diesel, :gasoline, :solar)
=> #<EnumerableAttribute::EnumerableAttribute:0x2467f5c @values={"diesel"=>0, "gasoline"=>1, "solar" =>2}>

>> engine_type.values
=> {"diesel"=>0, "gasoline"=>1, "solar" =>2}

>> engine_type.gasoline
=> "1

Enjoy! 🙂