3 ms·
Functions (methods) are first class objects also in Ruby: methods are instances of the class Method, while Procs are a lightweight alternative. Default argument
by philwelch 15y ago
Functions (methods) are first class objects also in Ruby: methods are instances of the class Method, while Procs are a lightweight alternative. Default arguments in Ruby are not mutable in any kind of function object, be it a lambda proc, a regular proc, or a method.
You can use a mutable default argument as an ersatz static variable, e.g. for memoization.
- bodhi 15y ago> Default arguments in Ruby are not mutable in any kind of function object Maybe I misunderstood you, but they are perfectly mutable: class A attr_accessor :a def initialize @a = [] end def b x = a x << 1 end end obj = A.new # => #<A:0x007fd92b2e9850 @a=[]> obj.b # => [1] obj.b # => [1, 1] obj.a # => [1, 1] If you mean "inline default arguments are not mutable", that's not true either. What is true is that the default argument is evaluated when the function is called, not when it is defined: a = 0 # => 0 x = lambda {|y = (a + 1)| y } # => #<Proc:0x007fd92b1dae50@(irb):33 (lambda)> x[] # => 1 a = 5 # => 5 x[] # => 6
- philwelch 15y agoGood catch, I hadn't thought of the case where the default argument is expressed in terms of another variable.
- mratzloff 15y agoAnd this is expected behavior. What is unexpected is when this behavior is afforded to new list or dictionary arguments in Python, just as it would be unexpected to say ruby> class A; end => nil ruby> def foo(bar = A.new); return bar; end => nil ruby> foo => #<A:0x00000101985060> ruby> foo => #<A:0x0000010197a6b0> and get back the same object each time `foo` is called in Ruby. I've even seen a major Python library with this bug (I'm sorry, I don't recall which off-hand). It's really surprising behavior for new Python devs.