Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

alias def []=(key, value) method in ruby

Tags:

ruby

Normally to override a method I would just alias it:

 alias original_method_name method_name

I have now come across a method that looks like this:

def []=(key, value)
end

And I am not sure how to alias this one.

like image 365
Derek Ekins Avatar asked Mar 22 '26 16:03

Derek Ekins


1 Answers

Maybe I didn't understand the question, but

>> class Hash
..   alias :foobar :[]=
..     end
=> nil
>> h = Hash.new
=> {}
>> h.foobar(:a, 1)
=> 1
>> h
=> {:a=>1}
like image 200
Michael Kohl Avatar answered Mar 25 '26 13:03

Michael Kohl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!