I’m trying to learn Ruby right now after learning Python and I’m having trouble translating this code to Ruby:
def compose1(f, g):
"""Return a function h, such that h(x) = f(g(x))."""
def h(x):
return f(g(x))
return h
Do I have to translate this using blocks? Or is there a similar syntax in Ruby?
Lets say
fandgare the following methods:We can define
compose1as:For this to work, we need to define h as:
h = compose1(:f, :g)You will need to pass the method names as a string / symbol for
sendto work. Then, you can doh.call 3 # => 8. More info can be found here