0
您可以在Ruby(可能使用Rails)中使用curried參數的方法嗎?在Ruby中使用curried參數的別名
def say(name)
puts "Hi, I'm #{ name }"
end
alias_method :introduce_john, :say, "John"
introduce_john # puts "Hi, I'm John"
您可以在Ruby(可能使用Rails)中使用curried參數的方法嗎?在Ruby中使用curried參數的別名
def say(name)
puts "Hi, I'm #{ name }"
end
alias_method :introduce_john, :say, "John"
introduce_john # puts "Hi, I'm John"
缺省情況下,卻是很容易的創建一個:
class Object
def self.curried_alias_method(new, original, *pre_args)
define_method(new) do |*args|
send(original, *pre_args, *args)
end
end
end
用法:
class Foo
def say(name)
puts "Hi, I'm #{ name }"
end
curried_alias_method :introduce_john, :say, "John"
end
Foo.new.introduce_john
# prints "Hi, I'm John"
爲什麼downvote? –
你怎麼能Currize那已經是一元的方法?你可以用什麼方式Currize一種方法?返回值是什麼類型? – sawa