digaev said almost 8 years ago on Random Ruby Tips and Tricks :

Nice tricks, knew most of them, but somehow I missed `current_user&.full_name `. Thanks!


danielpclark said almost 8 years ago on Random Ruby Tips and Tricks :

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.


davearonson said almost 8 years ago on Random Ruby Tips and Tricks :

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.


Scudelletti said almost 8 years ago on Random Ruby Tips and Tricks :

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!


kamil89 said over 7 years ago on Random Ruby Tips and Tricks :

There's also a trick with passing method instances:

def say_hello(name)
  puts "Hello #{name}"
end
names = ["James", "Jacob", "Jack"]

One way to call say_hello with args from array is:

names.map { |name| say_hello(name) }

And the shortened version with method instance would be:

names.map(&method(:say_hello))

Login to Comment