| 1 |
On 15 May 2017 19:32, "Nomadic Sprite" <[email protected]> wrote: |
| 2 |
|
| 3 |
The intent was to return nil when the first value was nil. |
| 4 |
That was the op's issue. |
| 5 |
If one of the values was nil, she/he wanted nil. |
| 6 |
|
| 7 |
Nil && anything_else will always return nil, and I will not evaluate the |
| 8 |
second clause. |
| 9 |
|
| 10 |
On 8 May 2017 6:51 pm, "Andy Jones" <[email protected]> wrote: |
| 11 |
|
| 12 |
> >>>>>>>> |
| 13 |
> It should be: |
| 14 |
> 2.0 && 2.0 + 12.0 |
| 15 |
> <<<<<<<<< |
| 16 |
> |
| 17 |
> Ah! Yes, that works. |
| 18 |
> |
| 19 |
> |
| 20 |
People are intent on not understanding, aren't they. |
| 21 |
|
| 22 |
def nil_add_12 f |
| 23 |
f && f + 12.0 |
| 24 |
end |
| 25 |
|
| 26 |
Generalised to two parameters: |
| 27 |
|
| 28 |
def nil_add a, b |
| 29 |
a && b && a + b |
| 30 |
end |
| 31 |
|
| 32 |
The only quirk is the way they handle `false`. |
| 33 |
|
| 34 |
This is not tested, but it may be possible to do this, too: |
| 35 |
|
| 36 |
f&.+ b |
| 37 |
|
| 38 |
Cheers |