Turning a function into a proc object

Consider the following piece of code:

f = Proc.new {|*args| g(*args)}

f is a proc object which, when called, basically behaves like g (the
only difference being the fact that one additional function call is
executed, when g is called “via f”.

Now two questions about it:

  • Is there a (syntactically) easier way to write this? I first thought
    f = &:g
    would be valid, but it isn’t.

  • Is there a way in Ruby to define a “function pointer” f (using C
    terminology here), so that calling f(…) is absolutely equivalent to
    calling g(…)? I do NOT mean creating an alias to the function.

f = method(:g).to_proc
=> Proc

I am not certain what exactly you need, but try this for “function

Either this

h = method(:g)

or this


This is exactly what I was looking for. Thanks a lot.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs