Accessing methods in a Ruby block

Faery

I have a block which is passed as argument to a function. This block contains several methods. It's like this:

def func(&block)
end

func do
  method1
  method2(arg)
  method3(arg)
end

I want func to return the composition of the functions in the block:

method3 ( method2 ( method1 ) )

Is there a way to have access to each of the methods in the block, so I can use methods.reduce(method1) { |method| method.call arg } or something like this?

Can you please give me some ideas?

Simone Carletti

No, this is not possible. You could (in theory) parse the Ruby code associated to the block definition, but I'm not sure that would make sense.

Your question is very generic, you don't provide any detail if you have control over the block or not, and a real world example would probably be more helpful.

From the details I have, my suggestion is that you should split the block at the origin. Instead of passing the whole block containing all those methods, pass an array of the methods as argument so that you can reuse them as you want.

You can wrap them in a lambda, to delay the execution.

def func(*chain)
end

func(
  ->(arg) { method1 },
  ->(arg) { method2(arg) },
  ->(arg) { method3(arg) }
)

You can also use Object.method to fetch the method and pass it as parameter.

def func(*chain)
end

func(
  method(:method1),
  method(:method2),
  method(:method3)
)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Accessing methods in a Ruby block

From Dev

Chaining methods in Ruby, and injecting a block into the chain

From Dev

Ruby - accessing instance methods / variables from anonymous class

From Dev

Trouble accessing certain methods from within Action Mailer in Ruby

From Dev

Trouble accessing certain methods from within Action Mailer in Ruby

From Dev

Replacing a verbose code block using native methods (Ruby on Rails)

From Dev

in ruby, when is a block not a block?

From Dev

Accessing $rootScope methods in Controller

From Dev

Accessing arrays through methods

From Dev

Accessing embedded methods in GoLang

From Dev

Implement Interface and accessing methods

From Dev

Accessing object methods with composition?

From Dev

multiple threads accessing methods

From Dev

Accessing enum variables/methods

From Dev

How do I access class methods in a define_singleton_method block in Ruby

From Dev

Accessing a file in ruby - differences

From Dev

Accessing Javascript value with ruby

From Dev

Accessing Ruby objects in javascript

From Dev

Setting and accessing variables in Ruby

From Dev

Accessing Key In Json - Ruby

From Dev

Block firefox from accessing webcam

From Dev

Do static methods block?

From Dev

Accessing Sizzle methods via jQuery

From Dev

AngularFire Accessing child element methods

From Dev

different signatures for accessing class methods

From Dev

accessing model manager methods in datamigration

From Dev

Accessing the methods of a member of an Object[] array

From Dev

Accessing a variable in different methods Java

From Dev

Accessing Highcharts methods in Rally chart