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

Setting an option doesn't set it "everywhere" #244

Closed
unleashy opened this issue Oct 4, 2019 · 2 comments
Closed

Setting an option doesn't set it "everywhere" #244

unleashy opened this issue Oct 4, 2019 · 2 comments

Comments

@unleashy
Copy link

unleashy commented Oct 4, 2019

Take this code:

result = Slop.parse do |o|
  o.bool '-f', '--foo', 'it does a foo'
end

result[:foo] = true

puts result[:foo]

Okay, it writes true to the output, so far so good. But if you change the last line to puts result.foo?… it writes false! How can this be possible? Is the method access syntax not just forwarding to []? I investigated a little and it seems used_options is at blame here:

slop/lib/slop/result.rb

Lines 52 to 58 in b3e382a

def method_missing(name, *args, &block)
if respond_to_missing?(name)
(o = option(name.to_s.chomp("?"))) && used_options.include?(o)
else
super
end
end

The option does exist and is set to true properly (result.option(:true) confirms this), but used_options doesn't update. Given #options returns every time I think line 54 should just be rewritten to (o = option(name.to_s.chomp("?")))&.value which works nicely.

@leejarvis
Copy link
Owner

Thanks for reporting. I don't see this as a bug. result is not a hash, so mutating values inside it wouldn't do anything. The ? suffixed methods are not meant to check that the value is true, it's meant to check that the value was used. Is there a particular use-case where you think you need this behaviour?

@unleashy
Copy link
Author

unleashy commented Oct 8, 2019

The ? suffixed methods are not meant to check that the value is true, it's meant to check that the value was used.

Oh, I thought it would check if the value is true—my mistake. I'll be closing this.

@unleashy unleashy closed this as completed Oct 8, 2019
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