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 #<Object:0x8106cd8> (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