Previous Reductive Labs, LLC Next

We Have Introspection Like LISP Has Parentheses

class Funtest
    def foo(one, two)
        puts "Got '%s' and '%s'" % [one, two]
    end
end

class Yaytest
    def foo(one)
        puts "Only got '%s'" % one
    end
end

[Funtest.new, Yaytest.new, "a string"].each do |obj|
    if obj.respond_to? :foo
        if obj.method(:foo).arity == 1
            obj.foo "one argument"
        else
            obj.foo "first argument", "second argument"
        end
    else
        puts "'%s' does not respond to :foo" % obj
    end
end

Produces:

Got 'first argument' and 'second argument'
Only got 'one argument'
'a string' does not respond to :foo
Previous Luke Kanies luke@reductivelabs.com | Index Next