-
Notifications
You must be signed in to change notification settings - Fork 52
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
BatchLoader.for().batch {}
evaluated as Truthy but should be Falsey
#84
Comments
I know why. it's because |
given another case, some unexpected behavior:
|
nil
whenever it's calledBatchLoader.for().batch {}
evaluated as Truthy but should be Falsey
FYI If you try it outside of IRB, you'll see the actual
|
The delegation only works for / relies on method calls. Here's another example: require 'batch-loader'
class Foo; end
foo = BatchLoader.for(1).batch do |ids, loader|
ids.each { |id| loader.call(id, Foo.new) }
end
foo.class
#=> Foo but: case foo
when Foo
puts 'a Foo'
else
puts 'not a Foo'
end
# not a Foo This is because As a workaround, you could call case foo.itself
when Foo
puts 'a Foo'
else
puts 'not a Foo'
end
# a Foo Applied to your examples: b = BatchLoader.for(1).batch do |ids, loader|
ids.each { |id| loader.call(id, nil) }
end
b.itself.object_id
#=> 8
puts 1 unless b.itself
# prints 1
nil == b.itself
#=> true
[nil].include?(b.itself)
#=> true
b.itself || 1
#=> 1 |
@sos4nt Thanks for your detailed explanation. I totally understand what you mean. But I still think this should be a problem. We're trying to use
I thought we can just replace |
I think this bug is a serious problem. every time
BatchLoader.for().batch
is called, a new object ofnil
will be returned by default. this can cause some weird bugs in Ruby and is a waste of memory. you can reproduce it as the following:env: batch-loader (2.0.1)
this bug result in some weird behaviors that
unless b
cannot be evaluated astrue
and it's hard to debug, it confused me a lot.The text was updated successfully, but these errors were encountered: