On Tue, 17 Oct 2000, GOTO Kentaro wrote: > In message "[ruby-talk:5600] passing single or multiple strings." > on 00/10/17, Hugh Sasse Staff Elec Eng <hgs / dmu.ac.uk> writes: > > >That's fine, and quite useful; if args are Strings and I just get one > >string then I don't get caught out by wanting to write: > > > > case args.type [...] > >which does not work. (You have to use if args.type == Array...elsif.... > >for that.) > > `type' is not needed because `Klass === obj' is identical to > `obj.is_a? Klass.' > > def something(args) > case args > when Array > :Array > when String > :String > end > end > > p something([]) #=> :Array > p something("") #=> :String > Oh! Thank you for this. :-) > >But this is quite different: > > > > def something(*bunch_of_args) > > bunch_of_args.each do > > ... > > end > > end > > > >because then things are not "shelled" out and if I pass in an Array I get > >a nested Array. something(["x", "y"]) => bunch_of_args == [["x", "y"]] > > > >So what method of Array, if any, will do this "shelling" for me? > > How about something(*["x", "y"]); > > def something(*bunch_of_args) > bunch_of_args > end > > p something(*["x", "y"]) #=> ["x", "y"] > Not tried it on a right hand side really....: irb(main):001:0> x = [[[2.3],4,[5,6]]] [[[2.3], 4, [5, 6]]] irb(main):002:0> *x SyntaxError: compile error (irb):2: parse error (irb):2:in `irb_binding' irb(main):003:0> p *x [[2.3], 4, [5, 6]] nil irb(main):004:0> Not sure why *x didn't work there... So is there a method corresponding to this * prefixing? Methods does show a "*" method for Array: irb(main):011:0> x.*() TypeError: no implicit conversion from nil (irb):11:in `*' (irb):11:in `irb_binding' [It is trying to multiply. OK.] ...is it by implication a method of Object: irb(main):012:0> q = Object.new #<Object:0xcf0b0> irb(main):013:0> q * x NameError: undefined method `*' for #<Object:0xcf0b0> (irb):13:in `irb_binding' irb(main):014:0> It seems not. Hmm. I'm still a bit puzzled. > -- gotoken > > Hugh hgs / dmu.ac.uk