[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

[Q&A]: HOWTO start irb on a different object

Jeremy Henty

3/24/2009 12:21:00 PM

I wanted to have irb start its session on an object other than the
toplevel. I found a solution that I think is nicer than any I could
find elsewhere. The idea is to hook into IRB::Irb#initialize and
insert the WorkSpace object that you want.

require "irb"

class IRB::Irb
alias initialize_orig initialize
def initialize(workspace = nil, *args)
default = IRB.conf[:DEFAULT_OBJECT]
workspace ||= IRB::WorkSpace.new default if default
initialize_orig(workspace, *args)
end
end

Now, if your .irbrc sets IRB.conf[:DEFAULT_OBJECT], that becomes the
initial object for the irb session.

I am tempted to propose this as a change to irb. Thoughts?

Regards,

Jeremy Henty
9 Answers

Ben Bleything

3/24/2009 3:11:00 PM

0

On Tue, Mar 24, 2009 at 5:21 AM, Jeremy Henty <onepoint@starurchin.org> wro=
te:
> I wanted to have irb start its session on an object other than the
> toplevel. =A0I found a solution that I think is nicer than any I could
> find elsewhere. =A0The idea is to hook into IRB::Irb#initialize and
> insert the WorkSpace object that you want.

Do you find that you often want to start irb in the same object's
context? That wouldn't be terribly useful for me, but I'm far from
Mr. Every Use Case Ever :)

When I want to get into a new object, I usually just use "irb
that_object" from inside irb. That allows me to have a clean (and
quick to start) irb most of the time, and be able to jump into any
object I want, rather than having to alter a config file.

Another option that might be cleaner is to hook an environment
variable so you can set it as part of the irb invocation. An even
better option would be to use an argument for irb, but I haven't
looked into how long that would take :)

Ben

Brian Candler

3/24/2009 3:48:00 PM

0

Jeremy Henty wrote:
> I wanted to have irb start its session on an object other than the
> toplevel.

This is what I have been using:
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-t...

I'm not interested in setting up a different default object in .irbrb,
but rather being able to bolt irb onto an application object as an
interactive CLI for testing/debugging.
--
Posted via http://www.ruby-....

Jeremy Henty

3/24/2009 4:25:00 PM

0

On 2009-03-24, Brian Candler <b.candler@pobox.com> wrote:
> Jeremy Henty wrote:
>> I wanted to have irb start its session on an object other than the
>> toplevel.
>
> This is what I have been using:
> http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-t...

Yes, that's what I found too. AFAICT that solution copies the irb
method that doesn't quite do what you want and modifies the copy so
that it does do what you want. I prefer my solution for being shorter
and simpler. OTOH my solution monkey-patches the original irb method,
which is a point against it.

> I'm not interested in setting up a different default object in
> .irbrb, but rather being able to bolt irb onto an application object
> as an interactive CLI for testing/debugging.

That's what I want too! With my solution I can put an .irbrc in my
application directory that makes irb start up in the context of an
application object. Which is useful.

Regards,

Jeremy Henty

Jeremy Henty

3/24/2009 5:59:00 PM

0

On 2009-03-24, Ben Bleything <ben@bleything.net> wrote:

> Do you find that you often want to start irb in the same object's
> context?

You're right that there's little reason to use this hook in ~/.irbrc
but as I remarked elsewhere in the thread, irb will pick up a .irbrc
from the current directory as well as from the home directory. And
it's very common for me to want to start irb in the same object's
context whenever I'm running irb in a particular directory. With my
hack I get that just by creating a *local* .irbrc .

> When I want to get into a new object, I usually just use "irb
> that_object" from inside irb.

Sure, that works. I just like having a way to make that happen
automatically.

Regards,

Jeremy Henty

Ben Bleything

3/24/2009 6:12:00 PM

0

On Tue, Mar 24, 2009 at 10:56 AM, Jeremy Henty <onepoint@starurchin.org> wr=
ote:
> You're right that =A0there's little reason to use =A0this hook in ~/.irbr=
c
> but as I =A0remarked elsewhere in the thread, irb will =A0pick up a .irbr=
c
> from the =A0current directory as well =A0as from the =A0home directory. =
=A0And
> it's very =A0common for =A0me to want =A0to start =A0irb in the =A0same o=
bject's
> context whenever I'm =A0running irb in a particular =A0directory. =A0With=
my
> hack I get that just by creating a *local* .irbrc .

Oh, by default? I've been carrying around code in my ~/.irbrc to load
local .irbrc for years, and didn't know anyone else did that :)

> Sure, =A0that works. =A0 I just =A0like =A0having a =A0way to =A0make tha=
t =A0happen
> automatically.

Yeah, sure. Like I said, I've never really needed it to happen
automatically, but it's definitely a cool hack when you do.

Ben

Robert Klemme

3/24/2009 6:28:00 PM

0

On 24.03.2009 17:25, Jeremy Henty wrote:
> On 2009-03-24, Brian Candler <b.candler@pobox.com> wrote:
>> Jeremy Henty wrote:
>>> I wanted to have irb start its session on an object other than the
>>> toplevel.
>> This is what I have been using:
>> http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-t...
>
> Yes, that's what I found too. AFAICT that solution copies the irb
> method that doesn't quite do what you want and modifies the copy so
> that it does do what you want. I prefer my solution for being shorter
> and simpler. OTOH my solution monkey-patches the original irb method,
> which is a point against it.
>
>> I'm not interested in setting up a different default object in
>> .irbrb, but rather being able to bolt irb onto an application object
>> as an interactive CLI for testing/debugging.
>
> That's what I want too! With my solution I can put an .irbrc in my
> application directory that makes irb start up in the context of an
> application object. Which is useful.

But wouldn't you have to start irb then with either

HOME=. irb

or with other options in order to make it read the local .irbrc? If you
do that then it's not as simple as typing "irb" and in that case you can
as well place a script in the local directory which does the proper
init. (Well, you could have a line like "load '.irbrc' if test ?r,
'.irbrc'" in ~/.irbrc but that somehow feels a bit too automatic to me.)

Btw, if you can make IRB read a "local" .irbrc already, then you can as
well place code in there like

application = whatever_initialization()
irb application
exit

At the moment I cannot see the benefit of your proposal.

Kind regards

robert

--
remember.guy do |as, often| as.you_can - without end

Jeremy Henty

3/25/2009 10:16:00 PM

0

On 2009-03-24, Robert Klemme <shortcutter@googlemail.com> wrote:
> On 24.03.2009 17:25, Jeremy Henty wrote:
>> ... I can put an .irbrc in my application directory that makes irb
>> start up in the context of an application object.
>
> But wouldn't you have to start irb then with either
>
> HOME=. irb
>
> or with other options in order to make it read the local .irbrc?

No, because irb automatically looks for a .irbrc in the current
working directory as well as in $HOME.

> Btw, if you can make IRB read a "local" .irbrc already, then you can
> as well place code in there like
>
> application = whatever_initialization()
> irb application
> exit

That doesn't work: "load error: /tmp/user/jeremy/.irbrc NoMethodError:
undefined method `irb' for main:Object". Commands like "irb
application" only work after irb starts its read-eval-print loop, and
it does that *after* it loads the config file. (I agree that this
trick would make my hack redundant if it did work.)

Regards,

Jeremy Henty

Robert Klemme

3/26/2009 7:51:00 AM

0

2009/3/25 Jeremy Henty <onepoint@starurchin.org>:
> On 2009-03-24, Robert Klemme <shortcutter@googlemail.com> wrote:
>> On 24.03.2009 17:25, Jeremy Henty wrote:
>>> ... I can put an .irbrc =A0in my application directory that makes irb
>>> start up in the context of an application object.
>>
>> But wouldn't you have to start irb then with either
>>
>> HOME=3D. irb
>>
>> or with other options in order to make it read the local .irbrc?
>
> No, =A0because irb =A0automatically =A0looks =A0for a =A0.irbrc =A0in the=
=A0current
> working directory as well as in $HOME.

I tested that but my versions apparently do not:

08:50:08 ~$ cd /tmp
08:50:10 tmp$ echo 'puts 123' > .irbrc
08:50:16 tmp$ irb
Ruby version 1.8.7
irb(main):001:0> exit
08:50:24 tmp$ irb19
Ruby version 1.9.1
irb(main):001:0> exit

I also checked that there are no errors in my ~/.irbrc

08:50:35 tmp$ ruby19 ~/.irbrc
Ruby version 1.9.1
08:50:45 tmp$ cat ~/.irbrc

puts "Ruby version #{RUBY_VERSION}"

require 'irb/completion'
08:51:29 tmp$

And irb is not aliased

08:52:29 tmp$ type -a irb irb19
irb is /usr/bin/irb
irb is /bin/irb
irb19 is /opt/bin/irb19
08:53:20 tmp$

What am I missing?

>> Btw, if you can make IRB read a "local" .irbrc already, then you can
>> as well place code in there like
>>
>> application =3D whatever_initialization()
>> irb application
>> exit
>
> That doesn't work: "load error: /tmp/user/jeremy/.irbrc NoMethodError:
> undefined =A0 method =A0`irb' =A0for =A0 main:Object". =A0 Commands =A0 l=
ike =A0"irb
> application" only work after =A0irb starts its read-eval-print loop, and
> it does =A0that *after* it =A0loads the config =A0file. =A0(I agree =A0th=
at this
> trick would make my hack redundant if it did work.)

Good point.

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end

Jeremy Henty

3/26/2009 10:17:00 AM

0

On 2009-03-26, Robert Klemme <shortcutter@googlemail.com> wrote:
> 2009/3/25 Jeremy Henty <onepoint@starurchin.org>:
>>
>> ... irb  automatically  looks  for a  .irbrc  in the  current
>> working directory as well as in $HOME.
>
> I tested that but my versions apparently do not:
> [...]
> What am I missing?

Sonething that I didn't understand: irb only loads the first .irbrc it
finds. I have no ~/.irbrc so it loads the local one. You do have a
~/.irbrc so it loads that and ignores any local one. Frankly I think
that's broken (irb, I mean, not your system of course). It's easy to
work around by having ~/.irbrc include the local .irbrc but you
shouldn't have to do that. How annoying!

Regards,

Jeremy Henty