JavaScript的Function.prototype.bind是否有Ruby等价物?

时间:2013-08-28 15:49:54

标签: javascript ruby

JavaScript欢乐时光有趣的土地

// make a method
var happy = function(a, b, c) {
  console.log(a, b, c);
};

// store method to variable
var b = happy;

// bind a context and some arguments
b.bind(happy, 1, 2, 3);

// call the method without additional arguments
b();

输出。耶!

1 2 3

在Ruby中

# make a method
def sad a, b, c
  puts a, b, c
end

# store method to variable
b = method(:sad)

# i need some way to bind args now
# (this line is an example of what i need)
b.bind(1, 2, 3)

# call the method without passing additional args
b.call

期望的输出

1, 2, 3

对于它的价值,我知道JavaScript可以通过传递给.bind的第一个参数来改变绑定的上下文。在Ruby中,即使我无法改变上下文,我也会感到高兴。我主要需要简单地将参数绑定到方法。

问题

有没有办法将参数绑定到Ruby Method的实例,这样当我在没有附加参数的情况下调用method.call时,绑定的参数仍会传递给方法?

目标

这是一种常见的JavaScript习惯用法,我认为它在任何语言中都很有用。目标是将方法M传递给接收方R,其中R不需要(或具有)当R执行该方法时要向M发送哪个(或多少个)参数的内在知识。 / p>

JavaScript演示如何有用

/* this is our receiver "R" */
var idiot = function(fn) {
  console.log("yes, master;", fn());
};


/* here's a couple method "M" examples */
var calculateSomethingDifficult = function(a, b) {
  return "the sum is " + (a + b);
};

var applyJam = function() {
  return "adding jam to " + this.name;
};

var Item = function Item(name) {
  this.name = name;
};


/* here's how we might use it */
idiot(calculateSomethingDifficult.bind(null, 1, 1));
// => yes master; the sum is 2

idiot(applyJam.bind(new Item("toast")));
// => yes master; adding jam to toast

2 个答案:

答案 0 :(得分:6)

通常,重绑定方法不是你在Ruby中做的。相反,你使用块:

# This is our receiver "R"
def idiot(&block)
  puts("yes, master; #{block.call}")
end


# Here's a couple method "M" examples
def calculateSomethingDifficult(a, b)
  return "the sum is #{a + b}"
end

def applyJam(object)
  return "adding jam to " + object.name
end

class Item
  attr_reader :name
  def initialize(name)
    @name = name
  end
end


# Here's how we might use it
idiot do
  calculateSomethingDifficult(1, 1)
end
#=> yes master; the sum is 2

# You *can* change calling context too (see instance_exec), but I'd
# discourage it. It's probably better to just pass the object as a
# parameter.
idiot do
  applyJam(Item.new("toast"))
end
#=> yes master; adding jam to toast

如果你真的想要像在JavaScript中那样“绑定”方法,那么它绝对可以:

class Method
  def bind *args
    Proc.new do |*more|
      self.call *(args + more)
    end
  end
end

这应该使您的示例几乎,如您最初描述的那样:

# make a method
def sad a, b, c
  puts a, b, c
end

# store method to variable
b = method(:sad)

# Get a "bound" version of the method
b = b.bind(1, 2, 3)

# call the method without passing additional args
b.call

如果您确切需要它,您可以定义Object#bindable_method以返回一些符合您需要的BindableMethod类。对于大多数情况,虽然我认为上述内容适合您。

答案 1 :(得分:4)

Ruby中的

Proc#curry与JavaScript中的bind类似。

def happy(a, b, c, d = 100)
  puts a, b, c, d
end

proc = method(:happy).to_proc.curry # proc is now a curried Proc

b = proc.call(1,2) # b is a curried Proc with 1 and 2 bound as the first arguments

b.call(3) # Call the proc, providing the 3rd argument

你不能完全复制你的示例代码,因为当使用必要的参数调用curried的proc时,它会返回proc的结果---换句话说,你不能绑定所有的参数然后调用proc之后---你必须至少留下一个不受限制的论点。

这不一定比Ajedi32提供的代码更好,但我认为值得一提,因为它内置于Ruby。

请参阅此处的文档:http://ruby-doc.org/core-2.2.0/Proc.html#method-i-curry