2013-01-15 127 views
1

我有一个看起来像传递任意参数方法

def SomeMethod (*args) 
    m = if args.length > 0 then 
      self.method(:method1) 
     else 
      self.method(:method2) 
     end 
    m.call ... #need to either pipe all arguments aside from the first 
       #and in other cases substitute the first argument 
end 

实际结构是痘痘更复杂的地方要调用的方法是不同的实例,并在某些情况下,我不得不管的方法以免第一,在其他情况下所有的参数我不得不与另一个值代替第一个参数

回答

0

你可以用哈希

def SomeMethod (hash_arg={}) 
    m = unless hash_arg.blank? then 
      self.method(:method1) 
     else 
      self.method(:method2) 
     end 
    m.call ... #need to either pipe all arguments aside from the first 
      #and in other cases substitute the first argument 
#Check if a certain arg has been passed=> do_something unless hash_arg[:certain_arg].nil? 
end 
尝试