'Alias method with argument
I have a method
def press_button(*key_buttons)
# some interaction to send button
end
and I use this with an argument: :shift, :tab, :backspace etc. I want aliases for this method with a fixed argument so that press_shift would stand for press_button(:shift). Is it possible to do this? Or, do I have to wrap this method like:
def press_shift
press_button(:shift)
end
def press_tab
press_button(:tab)
end
def press_backspace
press_button(:backspace)
end
Solution 1:[1]
I'm not quite sure I understand your question, but I believe this does what you're asking for:
[:shift, :tab, :backspace].each do |k|
define_method("press_#{k}") { press_button(k) }
end
Now the methods press_shift, press_tab, and press_backspace are defined.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Darshan Rivka Whittle |
