[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Interesting trival example of why open classes are good?

Peter Michaux

10/29/2006 7:41:00 AM

Hi,

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?

Thank you,
Peter

14 Answers

Paul Lutus

10/29/2006 8:02:00 AM

0

Peter Michaux wrote:

> Hi,
>
> 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.

Examples abound where this open-class business comes in handy.

--
Paul Lutus
http://www.ara...

Peter Michaux

10/29/2006 8:06:00 AM

0

> > 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

Kalman Noel

10/29/2006 8:20:00 AM

0

Peter Michaux:
> 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?

You may want to have a look at the facets code to get some ideas.

Kalman

Trans

10/29/2006 9:26:00 AM

0

'ello --

Peter Michaux wrote:
> 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.

# --- dynamic/X/here.rb

class X
def here
puts "Here I am!"
end
end

# ---

class X
def method_missing(s, *a, &b)
begin
require 'dynamic/X/#{s}'
send(s, *a, &b)
rescue LoadError
super
end
end
end

X.new.here #=> "Here I am!"

It's interesting pattern, don't you think? If you have large chunks of
code that only get used once in a while it's a way to speed up initial
load times. Actually you could write an entire program this way --with
each method in a different file (but that can be a chore to maintain).
In anycase it's an example of what you can do with open classes.

T.

dblack

10/29/2006 12:29:00 PM

0

Vincent Fourmond

10/29/2006 12:48:00 PM

0


Hello !

> 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.

I can give you that: in a meta system I'm writing, I want to be able
to build Qt widgets and to generate command-line parsers from a simple
definition. For the sake of simplicity, all this should reside in one
class. But I definitely don't want to link to Qt for a command-line-only
app. So here is my design:

common.rb:
class SomeClass
def parse_command_line(...)
end
end

qt.rb
class SomeClass
def create_qt_stuf(...)
end
end

Then, I always have command-line generation available (it's a standard
ruby lib), but if I want Qt, I just need to require 'qt.rb' in addition
to 'common.rb'.

Works great, and rdoc parses everything correclty, which is even greater !

Vince


--
Vincent Fourmond, PhD student
http://vincent.fourmon...

Vincent Fourmond

10/29/2006 1:14:00 PM

0


Hello !

> ---------------------------------
>
> There is no "odd?" function in Fixnum, but adding one is trivial.

Not for long ;-) -> RCR337

Vince

--
Vincent Fourmond, PhD student
http://vincent.fourmon...

Louis J Scoras

10/29/2006 2:51:00 PM

0

Open classes also are a real boon when you're coding something
interactively in irb.


--
Lou.

Wilson Bilkovich

10/29/2006 3:33:00 PM

0

On 10/29/06, dblack@wobblini.net <dblack@wobblini.net> wrote:
> Hi --
>
> On Sun, 29 Oct 2006, Peter Michaux wrote:
>
> >>> 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?
> >>
> ...
> > 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.
>
> That's harder, because if I give you an example like:
>
> class C
> def m
> end
> end
>
> class C
> def n
> end
> end
>
> I might as well have written it in one definition block :-)
>
> One possible use of this open-class feature is to put a class
> definition in more than one file. The Ruby library does this, for
> example, with the Date class. The pros and cons are probably pretty
> self-explanatory.
>

A side-effect of this is that when working with a team, you are less
likely to need to merge changes in your SCM tool.
Imagine if every piece of ActiveRecord were in one file. Rails would
probably just be getting around to 'Hello, world' about now.

Bob Hutchison

10/29/2006 3:53:00 PM

0

Hi,

On 29-Oct-06, at 3:10 AM, Peter Michaux wrote:

>> 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.

It isn't little, but xampl in Ruby makes heavy use of this capability
(see my sig for links). I'd think that most code-generators could
take advantage of open-classes.

It also allows you to organise your source code, at least partially,
on a functional basis rather than a class basis.

It is also kind of central to the implementation of Ruby's definition
of methods on objects, usually class objects, but not necessarily,
allowing you to do stuff like:


class Basics
def initialize(name)
@name = name
end
def tag
return "#{@name} says"
end
def hello
puts "#{tag}: hello"
end
def method_missing(s, *a, &b)
puts "#{tag}: pardon?"
end
end

a = Basics.new('a')
b = Basics.new('b')

a.hello
b.hello

a.bye
b.bye

def b.bye
puts "#{tag}: bye"
end

a.bye
b.bye



Cheers,
Bob

>
> Thanks again,
> Peter
>
>

----
Bob Hutchison -- blogs at <http://www.rec...
hutch/>
Recursive Design Inc. -- <http://www.rec...>
Raconteur -- <http://www.raconteur...
xampl for Ruby -- <http://rubyforge.org/projects/...