Is it possible to send nothing to an object?
Let me elaborate. For instance I could have something like this:
val = some_stack.include?(some_val) ? some_val : nil
obj1.obj2.send(val).obj3.obj4
The above call wont't work because nil is not a symbol. So the solution is:
if val.nil?
obj1.obj2.obj3.obj4
else
obj1.obj2.send(val).obj3.obj4
end
However I'm not too fond of this. Is there any other way?
I'm not aware of any method that returns self without taking any arguments (but I might be overlooking something), but there's no reason you couldn't monkeypatch one into the class you're using:
irb(main)> class Object
irb(main)> def ignore
irb(main)> self
irb(main)> end
irb(main)> end
=> nil
irb(main)> msg = nil
=> nil
irb(main)> 'hi'.send(msg || :ignore).upcase
=> "HI"
irb(main)> msg = :reverse
=> :reverse
irb(main)> 'hi'.send(msg || :ignore).upcase
=> "IH"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With