Three of these were new to me. &&= and fetch piping in the parameter into the block are possibly useful to me. I just need to think of a use case in my day to day coding.
Be careful when using ||= with Boolean values. ||= will set something not only if it's nil, but also if it's literally false, i.e., the one object named false, of class FalseClass. In short it will set it if it's "falsey".
That part also reminded me of some strange code I encountered recently: "if status == status = 1 || status = 2 || status = 3". See http://blog.codosaur.us/2016/11/x-x-wtf.htmlfor a writeup of what that was probably meant to do, and what it really did.
Nice tricks, knew most of them, but somehow I missed `current_user&.full_name `. Thanks!
Three of these were new to me. &&= and fetch piping in the parameter into the block are possibly useful to me. I just need to think of a use case in my day to day coding.
Be careful when using ||= with Boolean values. ||= will set something not only if it's nil, but also if it's literally false, i.e., the one object named false, of class FalseClass. In short it will set it if it's "falsey".
That part also reminded me of some strange code I encountered recently: "if status == status = 1 || status = 2 || status = 3". See http://blog.codosaur.us/2016/11/x-x-wtf.html for a writeup of what that was probably meant to do, and what it really did.
It's always nice to see tricks.
Some time ago I did a Ruby Tips and Tricks presentation, maybe it can give you more ideas for the part II.
http://www.slideshare.net/Scudelletti/ruby-tips-andtricks
About `fetch` you can also pass the default value as the second argument instead of a block.
`{a: 1}.fetch(:b, 200)`
Nice video!
There's also a trick with passing method instances:
One way to call say_hello with args from array is:
And the shortened version with method instance would be: