Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proc#compose ArgumentError incorrect #249

Open
domgetter opened this issue Dec 22, 2015 · 1 comment
Open

Proc#compose ArgumentError incorrect #249

domgetter opened this issue Dec 22, 2015 · 1 comment

Comments

@domgetter
Copy link

Proc#compose raises an error when it shouldn't. The following two lambdas can compose:

make_twin = -> n { return n, n }
take_twin = -> n, m { puts n; puts m }
twin_comp = -> n { take_twin[*make_twin[n]] }

twin_comp[2]
2
2
=> nil

But with Proc#compose, it raises an ArgumentError:

take_twin.compose(make_twin)
ArgumentError: arity count mismatch

One solution is to wrap the inner call in a begin:

class Proc
  def compose(g)
    lambda do |*a|
      begin
        self[ *g[*a] ]
      rescue ArgumentError => e
        puts "#{e.class}: #{e.message}" # or whatever message is appropriate
        puts e.backtrace
      end
    end
  end
end

There's not really a way to check a proc's return arity at runtime, since it can return in more than one way.

@trans
Copy link
Member

trans commented Jan 9, 2016

Hmm... good point. Perhaps we can just get rid to the argument error condition altogether?

def compose(g)
  lambda{ |*a| self[ *g[*a] ] }
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants