Skip to content

Update conditional argument handling to correctly support lambdas#246

Merged
geekq merged 1 commit into
geekq:developfrom
yjukaku:patch-1
Oct 16, 2025
Merged

Update conditional argument handling to correctly support lambdas#246
geekq merged 1 commit into
geekq:developfrom
yjukaku:patch-1

Conversation

@yjukaku

@yjukaku yjukaku commented Aug 7, 2025

Copy link
Copy Markdown
Contributor

Summary

This PR fixes the event condition call to check arity even when not using methods, since the user may have defined a condition using a lambda instead of a block. ie.

state :inside do
  event :exit, transitions_to: :outside, if: ->(obj) { dont_need_args(obj) }
end

Without this PR's change, this example raises an ArgumentError if you do something like obj.exit!(123), because the lambda is being called with 2 arguments when it expects 1.

Explanation

There are two types of Procs in Ruby, lambda-based procs and non-lambda (block-like) procs. They enforce arguments differently:

Block like

irb(main):001* proc_block = Proc.new do |a,b|
irb(main):002*   puts "a: #{a}, b: #{b}"
irb(main):003> end
=> #<Proc:0x000000012d315208 (irb):1>
irb(main):005> proc_block.call(1)
a: 1, b:
=> nil
irb(main):006> proc_block.call(1,2)
a: 1, b: 2
=> nil
irb(main):007> proc_block.call(1,2, 3)
a: 1, b: 2
=> nil

Lambda/stabby arrow

irb(main):004> proc_lambda = -> (a,b) { puts "a: #{a}, b: #{b}"  }
=> #<Proc:0x000000012cd39c80 (irb):4 (lambda)>
irb(main):008> proc_lambda.call(1)
(irb):4:in 'block in <top (required)>': wrong number of arguments (given 1, expected 2) (ArgumentError)
irb(main):009> proc_lambda.call(1,2)
a: 1, b: 2
=> nil
irb(main):010> proc_lambda.call(1,2,3)
(irb):4:in 'block in <top (required)>': wrong number of arguments (given 3, expected 2) (ArgumentError)

Thus we should check arity when calling an event's condition method or block/proc/lambda.

@yjukaku

yjukaku commented Aug 7, 2025

Copy link
Copy Markdown
Contributor Author

@geekq review please :)

I also have another PR incoming for the condition kwarg issue I tagged you in yesterday

@yjukaku

yjukaku commented Oct 15, 2025

Copy link
Copy Markdown
Contributor Author

Ping

@geekq geekq merged commit 24212f4 into geekq:develop Oct 16, 2025
@geekq

geekq commented Oct 16, 2025

Copy link
Copy Markdown
Owner

Merged this PR first. The #247 will require a rebase

@ranaldobowker

Copy link
Copy Markdown

hey, thanks for the gem @geekq, and thanks for the fix @yjukaku.

any idea when a new version will be released that includes this fix?

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

Successfully merging this pull request may close these issues.

3 participants