I have some code that needs to be called directly or passed to another method that must take a block. Pseudocode:
class Foo
def bar
if condition
return method_that_needs_a_block!('string', named1:, named2:) do
shared_method('a', 'b')
end
end
shared_method('a', 'b')
end
def shared_method(arg1, arg2)
puts arg1
puts arg2
end
end
You can see the method that must take a block method_that_needs_a_block
has a string for the first parameter and the rest are named parameters. How can shared_method
be used as either a method or a block and still be able to pass the arguments to it? I've attempted making the method a lambda but I'm still unsure how to use it within the block context.
I'm not sure I 100% understand the question but if you want to pass a method as a block use
&method(:method_name)
.Since parens are optional in Ruby there is a special
method
method which is used to get refences to methods - it returns a Method object. This is somewhat like a function reference in other languages.Since blocks in Ruby are not actually objects and passed differently the
&
operator is used to turn the method object into a Proc.