IRB reset?

C

Chuck Brotman

I'm not sure if this is a Ruby or FreerRIDE question, so I'll ask it here
and hope for the best.

When I "load" my program into the FreeRide IRB for a second time in a given
session, I get warning messages in he copnsole stating that I am redifining
my constants. Apparently there is left over state in the IRB. I've seen
some other anomolies that sweem similar (like a "load" causing a previously
entered IRB statement to be re-evaluated.

Am I right aboiut this being "left over" state? and if so is there a way tro
reset the IRB state, short of exiting and re-entering??
 
R

rcoder

This isn't specific to IRb -- any time you reload code which defines
constants, (variables with names that begin with a capital letter, such
as 'MY_VARIABLE' or 'MyConstant') the Ruby runtime will throw a warning
about the constant being redefined.

You can see this behavior in its simplest form with code like the
following:

--- begin 'const_warn_demo.rb' ---

# set a constant
FOO = 'bar'

# do something...

# now, set the constant again
FOO = 'baz'

--- end ---

Constants are designed to be read-only values, so a warning when
they're re-assigned is appropriate.

-Lennon
 
C

Chuck Brotman

rcoder said:
This isn't specific to IRb -- any time you reload code which defines
constants, (variables with names that begin with a capital letter, such
as 'MY_VARIABLE' or 'MyConstant') the Ruby runtime will throw a warning
about the constant being redefined.

You can see this behavior in its simplest form with code like the
following:

--- begin 'const_warn_demo.rb' ---

# set a constant
FOO = 'bar'

# do something...

# now, set the constant again
FOO = 'baz'

--- end ---

Constants are designed to be read-only values, so a warning when
they're re-assigned is appropriate.

-Lennon
Lennon.

Thanks for your reply!
I understand your answer to part one of my question, do you have anything to
say about part two? I.e. can an IRB session be reset to an uninitialized
state? In other words, is there a way to do an "unload?" or the
equivalent??? A related question might be: can one undefine a constant,
(like one can undef a methiod)?
 
M

Mark Hubbart

=20

Lennon.
=20
Thanks for your reply!
I understand your answer to part one of my question, do you have anything= to
say about part two? I.e. can an IRB session be reset to an uninitialized
state? In other words, is there a way to do an "unload?" or the
equivalent??? A related question might be: can one undefine a constant,
(like one can undef a methiod)?

Object.class_eval do
const_remove :Foo if defined? Foo
const_remove :Bar if defined? Bar
end

All subconstants (ie, Bar::Baz) will be removed too. Putting code like
this near the top of your file will make it not complain about
redefining constants.

It might be good not to leave the code in there for any release
versions, though; it's nice to be able to tell if you somehow
accidentally load a library more than once.

cheers,
Mark
 
R

rcoder

You can also put a sigil on the const_remove calls to prevent them from
being executed except when running in IRB:

--- begin ---

if (defined? IRB && IRB.CurrentContext)
const_remove :FOO
end

--- end ---

That would allow you to undefine the constants only while in a
currently-running IRB session, while still seeing warnings when your
"production" system (which presumably won't be running inside IRB)
loads a library multiple times.

As for resetting the IRB state, I think you're probably best off simple
exiting and restarting the process, esp. if you have the readline
extensions enabled. You'll still have your command history, and can use
the up and down arrow keys to re-execute any commands from your
previous session.

-Lennon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,142
Latest member
arinsharma
Top