On 2005-04-11, Martin DeMello <martindemello / yahoo.com> wrote:
> Csaba Henk <csaba / phony_for_avoiding_spam.org> wrote:
>> 
>> but with irb, the api-friendly way is as follows:
>> 
>>   IRB.context.workspace.main
>
> irb.context.workspace.main actually

Uh-oh. Stupid me, again.

 irb.context.workspace.main

starts a new reader first, and when that's completes, it gives you the
respective IRB::Irb object. So even if that's the toplevel object again,
I don't think you really want this. IRB.context.workspace.main is the
correct solution, under the right circumstances... :) no wonder it
doesn't work for you. For my irb hacks, I use

 module IRB
    def self.context
      conf[:MAIN_CONTEXT]
    end
 end

and having this, what I told you will work.

To put it simply: 

 IRB.conf[:MAIN_CONTEXT].workspace.main

is the right thing.

Sorry for iterating confusion.

Though... even the above will break if someone starts a subirb with a
different workspace.main. Not that likely, but why not. To get the
evaluator of the current irb object, you have to do something like 

 IRB.conf[:MAIN_CONTEXT].workspace.main.jobs.current_job.context.workspace.main

Whew! OK, I looked up how to simplify that:

 IRB.JobManager.current_job.context.workspace.main 

So, the purest solution would be to get #gets dynamically dispatch to
the vaule of the above expression. 

Such things are handled by the module IRB::ExtendCommandBundle of
'irb/extend-command.rb'.

I guess by adding the right thing to @EXTEND_COMMANDS of the above,
the magic would happen. I don't know though if you can actually overwrite
existing methods this way. Maybe you'd have to do something with
IRB::ExtendCommandBundle.included.

I'd say that if you are lazy, just use
"IRB.conf[:MAIN_CONTEXT].workspace.main", that's a 97% solution at least
anyway.

Csaba