From: Joseph McDonald Date: 2000-12-04T15:10:02+09:00 Subject: [ruby-talk:6795] Re: using join() Thanks. You guys are great! You're right, I was thinking in perl. the habits are hard to break. I can't wait for the book. I hope you guys aren't getting sick of my questions until I get it. Here's a prob I just tumbled into. During debugging it would be cool to do something like: def printvars (args) args.each do |obj| print obj.name, "=", obj, "\n"; end end urls = ["hello", "world"] text = "world!" printvars([urls, text]) but I can't seem to get the objects name. thanks, -joe "David Alan Black" wrote in message news:Pine.LNX.4.21.0012032312040.8861-100000@candle.superlink.net... > Hello -- > > On Mon, 4 Dec 2000, Joseph McDonald wrote: > > > while I await the "Programming Ruby" book. Can someone tell me what > > I'm doing wrong here: > > > > i = j = 2 > > a = join(":",i,j); > > print a > > > > undefined method `join' for # (NameError) > > > Yes: you're writing Perl :-) > > join is an instance method of class Array. Therefore, you need > an array object, so that you can call its join method. For > example: > > i = j = 2 > a = [i,j].join ':' # => "2:2" > > This is different from Perl, where join is a universal function which > takes its second and subsequent arguments as the things to join. > Ruby's join only takes one argument, the separator. What gets joined > are the elements of the array on which the method is called (i.e., > the "receiver" of the join "message"). > > (By the way, note that in the above example, the 2's get conveniently > turned into strings. That happens to happen with join; it isn't > generally automatic in Ruby as it is in Perl, though.) > > > also, is it true that this: > > > > s/^\s+|\s+$// # trim whitespace > > > > doesn't work? > > > > when I changed it to: > > > > line = $_ > > line.sub!(/^\s+|\s+$/ , "") # trim whitespace > > > > it appeared to work. Just wondering if s/// works in ruby and I'm doing it > > wrong. I also tried $_ =~ s/^\s+|\s+$// > > There's no s/// operator in Ruby: you're right to use [g]sub[!]. > Also, in this particular case you can save yourself some typing: > > line.strip! > > will do it. > > You'll love the book. > > > David > > -- > David Alan Black > home: dblack@candle.superlink.net > work: blackdav@shu.edu > Web: http://pirate.shu.edu/~blackdav > > >