facets icon indicating copy to clipboard operation
facets copied to clipboard

ergo's behavior on nil isn't like tap

Open benatkin opened this issue 7 years ago • 1 comments

This is a neat gem. I especially like how you can pull in just the modules you want.

According to the docs, ergo is like tap. But this example in nil deviates from tap's behavior:

nil.ergo{ |o| o.bar }    #=> nil

Whereas with tap:

2.2.4 :024 > { value: nil }.tap { |o| o[:value] = o[:value].bar }
NoMethodError: undefined method `bar' for nil:NilClass
        from (irb):24:in `block in irb_binding'
        from (irb):24:in `tap'
        from (irb):24
        from /Users/benatkin/.rvm/rubies/ruby-2.2.4/bin/irb:11:in `<main>'
2.2.4 :025 >

Perhaps mention this in the docs, and consider changing it in a future release? This could be surprising behavior.

benatkin avatar Oct 16 '17 21:10 benatkin

Try { value: nil }.ergo { |o| o[:value] = o[:value].bar } and check the same result.

kuboon avatar Nov 01 '17 00:11 kuboon