1 min read

TIL: Ruby Methods are Functions: Getting a Reference

I have to look this up every single time I want to do this: let's grab a reference to a Ruby method so we can pass it around

I have to look this up every single time I want to do this: let's grab a reference to a Ruby method so we can pass it around.

puts_method = method(:puts)
puts_method.call('hello world')
# hello world

Let's grab a class method:

now_method = DateTime.method(:now)
now_method.call
# => Wed, 01 Jul 2020 15:46:42 +0100 

Let's grab an instance's method:

concat_method = 'hello'.method(:+)
concat_method.call(' world')
# => "hello world" 

Let's be absurd:

puts_method.call(concat_method.call(' world'))
# hello world
puts_method.call(now_method.call)
# 2020-07-01T15:50:17+01:00