From: Peter Michaux Date: 2006-10-29T17:10:12+09:00 Subject: Re: Interesting trival example of why open classes are good? > > I am trying to explain why the fact that Ruby's classes are never > > closed is advantageous. I can only think of relatively complex examples > > like Rails plugins. Does anyone have an interesting, standalone example > > showing the utility of open classes in under 30 lines? under 20? > > That is way too easy. > > --------------------------------- > > #!/usr/bin/ruby -w > > class Fixnum > def odd? > return self & 1 == 1 > end > end > > 10.times do > puts rand(30).odd? > end > > --------------------------------- > > There is no "odd?" function in Fixnum, but adding one is trivial. Thanks for the example. I like it and I'll use it. > Examples abound where this open-class business comes in handy. Do you know of a nice little example that doesn't modify a built in Ruby class? I didn't realize I wasn't specific enough in my request. Thanks again, Peter