Skip to content

Proc#compose ArgumentError incorrect #249

Closed

Description

@domgetter

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.

Activity

  1. trans commented on Jan 9, 2016

    @trans
    Member

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

    def compose(g)
      lambda{ |*a| self[ *g[*a] ] }
    end
  2. trans commented on Nov 2, 2026

    @trans
    Member

    Proc#compose is deprecated (removal after 2027-09-30) and now just calls Ruby's Proc#<<, so Facets' arity check is gone. Ruby passes the inner result to the outer proc as one argument; for a multi-value return, splat explicitly: ->(n){ take_twin[*make_twin[n]] }.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions