Previous Reductive Labs, LLC Next

Iteration Is Sweet

class Funtest
    include Enumerable

    def each
        @params.each { |key, value|
            yield key, value
        }
    end

    def initialize(hash)
        @params = hash
    end
end

f = Funtest.new(:first => "foo", :second => 59, :third => :symbol)

f.each { |key, value|
    puts "Value '%s' is '%s' of type '%s'" % [key, value, value.class]
}

puts f.find { |key, value| value.is_a?(Symbol) }.join(" => ")
puts f.collect { |key, value| value.to_s }.join(" -- ")

Produces:

Value 'first' is 'foo' of type 'String'
Value 'second' is '59' of type 'Fixnum'
Value 'third' is 'symbol' of type 'Symbol'
third => symbol
foo -- 59 -- symbol
Previous Luke Kanies luke@reductivelabs.com | Index Next