This one works:
Beer.all.send(:sort)
and also it works with block:
Beer.all.sort_by{|b| b.name}
Beer.all.sort_by(&:name)
But when I give a executable block to send-method like this:
Beer.all.send(:sort_by{|b| b.name})
Beer.all.send(:sort_by(&:name))
I get syntax error. Is there any alternative way in Ruby to give an executable block to send-method?
You should try something like that :
Beer.all.send(:sort_by) {|b| b.name}
Blocks are special arguments in Ruby, they are not passed together with the regular arguments inside the parentheses. This doesn't have anything in particular to do with send. send is just a method like any other method, after all, it cannot change the syntax of Ruby.
Blocks are passed after all the other arguments, i.e. like this:
foo.bar(baz, qux) {|sillyname| do_stuff }
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