On 2002.09.22, Philip Mak <mfraser / seas.gwu.edu> wrote:
> 
> NameError: private method `gsub' called for [1, 2, 3]:Array
>         from /usr/local/lib/ruby/1.6/cgi.rb:260:in `escapeHTML'
>         from (irb):7

The beauty of Ruby (and OO in general) is being able to make "objects"
... what if you wanted to pass in your own object that wasn't inherited
from String (and thus, is not a #kind_of? String) but implements the
#gsub method?

> Regarding this, I have two questions:
> 
> (1) Is it bad programming practice to write a library that can crash if
> garbage is passed in?

Yes and no.  It's just as bad programming practice as not having unit
tests and it seems programmers don't mind not having those ...

> (2) How do I program libraries in a friendly fashion? Should I do stuff
> like:
> 
> raise "Expected String, got #{str.class}" unless str.kind_of?(String)

Definitely not.  See my comment above.

Even testing using #respond_to? isn't foolproof because of
#method_missing ...

> Then again, doing (2) seems to add a significant amount of extra code I
> have to write. In typed languages, I just need to specify what type a
> variable should be; not have to write a whole line to raise an exception
> if the variable isn't the right type.
> 
> Does anyone have a better idea?

Automated unit tests.  You get test coverage and peace of mind with
dynamically typed languages at the same time.

-- Dossy

-- 
Dossy Shiobara                       mail: dossy / panoptic.com 
Panoptic Computer Network             web: http://www.panoptic.com/ 
  "He realized the fastest way to change is to laugh at your own
    folly -- then you can let go and quickly move on." (p. 70)