Redefining core classes

Hi you all,
I am reading the â??Whyâ??s (poignant) guide to Rubyâ?. Iâ??ve reached to the
example where he explains how you can change the core â??Stringâ? class
definition, adding variables and methods (in
http://poignantguide.net/ruby/chapter-5.html). My question isâ?¦ if I run
this file with the new definitions…, does the core String class of
Ruby remain changed forever? I dunnoâ?¦is not this feature a little
dangerous?

It remains changed for as long as that particular instance of the ruby
interpreter is run.

On 7/6/06, Damaris F. [email protected] wrote:

My question is? if I run
this file with the new definitions…, does the core String class of
Ruby remain changed forever? I dunno?is not this feature a little
dangerous?

“Ruby, you’ll shoot your eye out”

It is changed until you unchange it, or the interpreter stops – not
forever. But yes changing core classes is “dangerous.” But so are most
things worth doing or having. To quote another movie:

“with great power comes great responsibility”

Write unit tests and enjoy.
pth

Hello,

Yeah, it could be dangerous. But it’s also incredibly useful at
times. For instance, in Rails it’s possible to write declarations like

30.minutes.ago

that allow you to express units of time and space as Ruby
declarations. However, if you look at Numeric in Ruby, there is no
such method ‘minutes’ or ‘ago’ defined. Rails adds them, so they can
have them defined for all Numeric objects for convenience. To see why
this is convenient, imagine how that code would be in the traditional
model where you’d extend through inheritance. Rails would have to
define a class RailsFixnum that adds those methods and the user would
have to code this as

x = RailsFixnum.new(30)
x.minutes.ago

Ugly and more error prone. Ruby code is supposed to be consistent and
concise, and the flexibility of modifying base classes helps that
sometimes. It’s democratic. You can work with main classes to suit
them better for your needs. I think it’s excellent to modify and
extend core classes for your needs, but be sure to test!

Jake

Hi again,

It is changed until you unchange it, or the interpreter stops – not
forever. But yes changing core classes is “dangerous.” But so are most
things worth doing or having. To quote another movie:

I’m quite new to Ruby, so… I open the Irb, I change a core class
and…then… I exit Irb… if I run Irb again…this changes are not
there, right? I’m pretty sure this question is very stupid, but I wanna
be sure I’ve understood this (I have not worked with interpreters
languages either before…)

What if I’m running two rails apps on the same (apache) server?
Couldn’t changes to core classes made by one of these apps effect the
other one?

Or does each instance of “ruby someAppOrOther.rb” start up a new
interpreter in memory?

thanks,
jp

Daniel B. wrote:

On 7/9/06, Damaris F. [email protected] wrote:

I’m quite new to Ruby, so… I open the Irb, I change a core class
and…then… I exit Irb… if I run Irb again…this changes are not
there, right?

That’s right. When you change core classes, you’re changing the state
of
the Ruby that exists “in memory”, so to speak.

;D

On Jul 8, 2006, at 8:30 PM, Jeff P. wrote:

What if I’m running two rails apps on the same (apache) server?
Couldn’t changes to core classes made by one of these apps effect the
other one?

Or does each instance of “ruby someAppOrOther.rb” start up a new
interpreter in memory?

thanks,
jp

Jeff-

Yeah each ruby foo.rb starts a new interpreter in memory and opening

classes will only affect that instance of the interpreter. Your two
rails apps won’t step on each other.

-Ezra

On Jul 9, 2006, at 11:38 PM, Ezra Z. wrote:

jp

Jeff-

Yeah each ruby foo.rb starts a new interpreter in memory and
opening classes will only affect that instance of the interpreter.
Your two rails apps won’t step on each other.

-Ezra

Ezra might know more than I do on this subject, but I remember
hearing that mod_ruby needed a special version of dispatch.rb to
avoid rails apps conflicting. All the other deployment methods just
work though. In short, make sure to read the docs and you’ll be fine.
-Mat

On Jul 10, 2006, at 5:30 AM, Mat S. wrote:

Ezra might know more than I do on this subject, but I remember
hearing that mod_ruby needed a special version of dispatch.rb to
avoid rails apps conflicting. All the other deployment methods
just work though. In short, make sure to read the docs and you’ll
be fine.
-Mat

Yeah mod_ruby is a different story altogether. I don't knoiw anyone

who actually uses it with rails though as it is not a good option for
rails apps. IIRC it shares an interprer between apps so you can’t run
more then one rails app on one apache server with mod_ruby. I seem to
remember a patched version that got around this somehow but i am not
certain. I would stay away from mod_ruby for your rails apps
completely. But mod_ruby is great if you just want to serve .rhtml
or .rb files for simple little apps.

-Ezra

I know this is lame and has been asked 64 times before.
Currently, what is the best way to have access to serial ports
on a PC using Ruby ?

Regards

Gus S Calabrese
Denver, CO
720 222 1309 303 908 7716 cell
I allow everything with “spamcode2006” in the subject or text to
pass my spam filters

On 7/9/06, Damaris F. [email protected] wrote:

I’m quite new to Ruby, so… I open the Irb, I change a core class
and…then… I exit Irb… if I run Irb again…this changes are not
there, right?

That’s right. When you change core classes, you’re changing the state
of
the Ruby that exists “in memory”, so to speak.

;D

This was the previous answer to your question.

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/35355

You might also look at the ruby-serialport project on rubyforge.

http://rubyforge.org/projects/ruby-serialport/

This should be installable as a gem, but I have never used it, nor do
I know if it works on Windows.

By the way, have you ever attended the Boulder/Denver Ruby U. Group?
We meet once a month in Superior just up US36 from Denver. Good
times are had by all, and I always leave with a little more ruby foo
under my belt.

http://rubyforge.org/news/?group_id=1241

That link gives the meeting times and locations. The next meeting is
Wednesday, July 19 at 6:00 PM.

Blessings,
Tim P.

I know this is lame and has been asked 64 times before.
Currently, what is the best way to have access to serial ports
on a PC using Ruby ?

You don’t mention the platform, but on Linux I just open /dev/tts/X and
read and write to it like a file. As long as you have it set up before
hand using the stty command, it works just fine.

Caleb

Gus S Calabrese wrote:

I know this is lame and has been asked 64 times before.
Currently, what is the best way to have access to serial ports
on a PC using Ruby ?
Windows? Do you need CTS/RTS, DSR/DTR ?

Regards,
JJ

Windows is fine and I do not need CTS RTS DSR DTR
AGSC

On 2006-Jul 11, at 15:28hrs PM, John J. wrote:

Gus S Calabrese wrote:

I know this is lame and has been asked 64 times before.
Currently, what is the best way to have access to serial ports
on a PC using Ruby ?
Windows? Do you need CTS/RTS, DSR/DTR ?

Regards,
JJ

Gus S Calabrese
Denver, CO
720 222 1309 303 908 7716 cell
I allow everything with “spamcode2006” in the subject or text to
pass my spam filters