Submitted by eric_mahurin (Sun Sep 11 16:48:43 UTC 2005)
a [, b [, c ...]], d
you have to do something like this:
def foo(a,*remaining) d = remaining.pop b = remaining.empty? ? b_default : remaining.shift c = remaining # splatted argument array ... end
A primary example would be for the []= method where the calling syntax is:
object[ arg,... ] = value
The receiving method gets the arguments in the same order as listed above. The variable length list of arguments between the [] comes first and the value is last. Unless the []= method has a fixed number of arguments between the [], the method will have to do something like above to pop off the value from the argument list.
def foo(a, b, c=c0, d=d0, *e, f, g) ... end
would be equivalent to:
def foo(a, b, *remaining) f,g = remaining.slice!(-2,2) c = remaining.empty? ? c0 : remaining.shift d = remaining.empty? ? d0 : remaining.shift e = remaining ... end
Also consider the cases where the optional arguments (c,d) or the splatted arg array (e) aren't there.
An optional part of this RCR would be to apply this to multi-assign statments. The following:
a,b,*c,d,e = mrhs
would be equivalent to:
a,b,*remaining = mrhs d,e = remaining.slice!(-2,2) c = remaining
Comments | Current voting | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
RCRchive copyright © David Alan Black, 2003-2005.
Powered by .
The implementation section needs some love - have you looked at the C code needed to implement this at all?
This would be very useful for #[]= methods.
It's much cleaner and intuitive than having to write