Wednesday, 28 August 2013

Is there a Ruby equivalent for JavaScript's Function.prototype.bind?

Is there a Ruby equivalent for JavaScript's Function.prototype.bind?

JavaScript happy times fun land
// 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();
Output. Yay!
1 2 3



In 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
Desired output
1, 2, 3

No comments:

Post a Comment