updating a class in irb

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

···

--
Duke

Hey Duke,

One of the cool things about Ruby is the ability to modify code at runtime.

I wouldn't suggest doing this in real code unless you're very confident
regarding the results.

Here is an example in which I use Module#class_eval (aka
Module#module_eval) to add an attr_reader to a class in irb:

class MissingAttrReader
  def initialize(value)
    @value = value
  end
end

MissingAttrReader.new('some value).value
#=> undefined method `value' for #<MissingAttrReader:0x00007f9831210900
@value="some value"> (NoMethodError)

MissingAttrReader.class_eval { attr_reader :value } # class_eval accepts a
block which is evaluated in the context of the object we are sending to

MissingAttrReader.new('some value).value
#=> "some value"

If you'd like to read more about class_eval, you can see the docs here:
https://apidock.com/ruby/v2_6_3/Module/class_eval

Ruby really is great for metaprogramming, but it can also be dangerous and
behave unexpectedly if you aren't careful!

There are a couple of decent books and a ton of good blog posts about
metaprogramming in Ruby, if you're interested in learning more.

···

On Fri, Nov 19, 2021 at 8:27 PM Duke Normandin <dukeofpurl@gmx.com> wrote:

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?
TIA ...
--
Duke

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<ruby-talk list: member options login page>

all class are open in ruby. you can overwrite the method with what you wanted.

···

On 2021-11-20 10:26, Duke Normandin wrote:

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?
TIA ...
--
Duke

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<ruby-talk list: member options login page>

Hi Duke,

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

Since you're just getting started, I would strongly recommend that you put anything longer than 10 lines into a file and try running it using
- ruby [file_name.rb]

That way, if it throws up an error, you can easily fix it.

That said, the other answers are technically correct. Ruby allows you to enter the same stuff again and it will overwrite something with the same name.

So, if you had:
class MyClass1
def initialize
@a = 23
end
def something
# something
end
end

Now, if you need to just change something, you could do

class MyClass1
def something
# something else
end
end

It will update the definition of 'something' to the new implementation you provided.

For this reason, retyping the class > method > implementation minimally will let you overwrite it.

See which looks easier for you and just run with it.

Finally welcome to the community and feel free to shout out if there's something we can help with!

Best Regards,
Mohit.
2021-11-20 | 12:59 pm.

···

On 2021-11-20 10:26 am, Duke Normandin wrote:

@M_sindhwani
@J_Herrington
@S_Hoffman

Thanks for each of your suggestions and clarifications!
All good stuff. Enjoying Ruby so far!

···

--
Duke

Quoting Duke Normandin (dukeofpurl@gmx.com):

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:

def m(a)
  a+1
end

In IRB you can type

require('/tmp/t.rb')

and then

m(1)

will return 2, as expected. If you then modify your file so that it
contains

def m(a)
  a-1
end

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
this one:

$LOADED_FEATURES.delete('/tmp/t.rb');require('/tmp/t.rb')

and then

m(1)

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

def other(a)
  a*Math::PI
end

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

HTH

Carlo

···

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 - fluido@fluido.as che bisogno ci sarebbe
  * di parlare tanto di amore e di rettitudine? (Chuang-Tzu)

use ruby 3 since it has multiline editing in irb (well 2.7 will work too
but just leap :slight_smile:

in irb, just use up arrow to go back
to insert a line use alt+enter key combination
keystrokes are similar to bash/emacs

···

On Sat, Nov 20, 2021 at 10:26 AM Duke Normandin <dukeofpurl@gmx.com> wrote:

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?
TIA ...
--
Duke

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<ruby-talk list: member options login page>

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.

I hear you!

IRB is a wonderful tool, but mostly suited for quick&dirty tasks.

[snip the mini-tutorial]

Thanks for that very helpful mini-tutorial Carlo! It did help.
I've fiddled with Common Lisp/emacs/slime quite a bit, so I'll be
using emacs - maybe in ruby-mode if there is one.

···

On Sat, 20 Nov 2021 07:38:39 +0100 "Carlo E. Prelz" <fluido@fluido.as> wrote:
--
Duke

Thx! All very helpful ...

···

On Sat, 20 Nov 2021 15:49:59 +0800 botp <botpena@gmail.com> wrote:

use ruby 3 since it has multiline editing in irb (well 2.7 will
work too but just leap :slight_smile:

in irb, just use up arrow to go back
to insert a line use alt+enter key combination
keystrokes are similar to bash/emacs

--
Duke

Quoting Duke Normandin (dukeofpurl@gmx.com):

I've fiddled with Common Lisp/emacs/slime quite a bit, so I'll be
using emacs - maybe in ruby-mode if there is one.

Of course there is! Emacs is also my editor of choice - has been since
1986 or thereabouts. From what I heard, this is also true for Matz.

The official emacs ruby mode file is found in the '/misc' subdirectory
of the Ruby source tarball (file ruby-style.el), but I am confident
that more or less standard distributions or emacs already come with
the appropriate configuration.

Happy coding!

Carlo

···

Subject: Re: updating a class in irb
  Date: Sat 20 Nov 21 09:39:39AM -0700

--
  * Se la Strada e la sua Virtu' non fossero state messe da parte,
* K * Carlo E. Prelz - fluido@fluido.as che bisogno ci sarebbe
  * di parlare tanto di amore e di rettitudine? (Chuang-Tzu)

Swell! Thx for confirming that emacs also rocks with Ruby! :smiley:

···

On Sun, 21 Nov 2021 06:09:38 +0100 "Carlo E. Prelz" <fluido@fluido.as> wrote:

  Subject: Re: updating a class in irb
  Date: Sat 20 Nov 21 09:39:39AM -0700

Quoting Duke Normandin (dukeofpurl@gmx.com):

> I've fiddled with Common Lisp/emacs/slime quite a bit, so I'll
> be using emacs - maybe in ruby-mode if there is one.

Of course there is! Emacs is also my editor of choice - has been
since 1986 or thereabouts. From what I heard, this is also true
for Matz.

The official emacs ruby mode file is found in the '/misc'
subdirectory of the Ruby source tarball (file ruby-style.el), but
I am confident that more or less standard distributions or emacs
already come with the appropriate configuration.

--
Duke