Quoting Duke Normandin (firstname.lastname@example.org):
Ruby noob here! Using irb to learn.
created a sample class with 2 instance vars.
tried to access the vars. NO JOY!
Forgot to include the attr_reader.
Is there a way to amend/update a class in the irb?
Or do I have to re-type the entire class over again?
I do not think you will find it particularly useful, at this level of
confidence in the language, to learn sophisticated techniques to
modify loaded code.
IRB is a wonderful tool, but mostly suited for quick&dirty tasks. IRB,
for example, has substituted since ages my desktop calculator. But the
task at hand becomes quickly complex enough to justify an editable
file. If I were you, I'd keep an editor window with the code you
are developing and an IRB session open at the same time. To make a
trivial example, say you are editing a file called /tmp/t.rb, which
contains these lines:
In IRB you can type
will return 2, as expected. If you then modify your file so that it
from your IRB session you can reload the file, so that the new method
will overwrite the old one. You must first remove the file name from
the list of loaded files, which is contained in an array called
$LOADED_FEATURES. So, from IRB, you will want to execute a line like
will return 0.
Requiring the file simply asks the interpreter to execute its
contents, as if you had typed it. If you had changed your file like
reloading it would define a new method called 'other', but would *not*
remove old method 'm'.
IRB maintains a command history, so the reloading command will be easy
to re-execute every time you change your file. Just type the up-arrow
a few times until you recover it, or use Ctrl-R to search in the
Subject: updating a class in irb
Date: Fri 19 Nov 21 07:26:39PM -0700
* Se la Strada e la sua Virtu' non fossero state messe da parte,
* K * Carlo E. Prelz - email@example.com che bisogno ci sarebbe
* di parlare tanto di amore e di rettitudine? (Chuang-Tzu)