Calling Irb from inside an application

Discussion in 'Ruby' started by Bertram Scharpf, Dec 25, 2007.

  1. Hi,


    this may be easy to anwer but it is difficult to google for.

    While developping I would like to play around with objects from
    time to time. Just like this:

    irb(main):001:0> class C ; def f ; "F" ; end ; end
    => nil
    irb(main):002:0> irb C.new
    irb#1(#<C:0xb7910d40>):001:0> f
    => "F"
    irb#1(#<C:0xb7910d40>):002:0>

    In common my classes are not so easy to type in. So, I would like
    to call Irb from somewhere inside the application as I do here:

    ---->-call_irb.rb---------------
    #!/usr/bin/ruby

    class C
    def f
    "F"
    end
    end

    if $0 == __FILE__ then
    require "irb"
    $c = C.new
    IRB.start
    end
    ----<---------------------------

    and then

    user@host $ ./call_irb.rb
    irb(main):001:0> irb $c
    irb#1(#<C:0xb7be4ee0>):001:0> f
    => "F"
    irb#1(#<C:0xb7be4ee0>):002:0>

    Is there a way how I can call C.new's Irb directly without doing
    the long-winded definition of a global variable first? I already
    examined the irb sources but this seems to be well-hidden to me.

    Thanks in advance and Merry Christmas,

    Bertram


    --
    Bertram Scharpf
    Stuttgart, Deutschland/Germany
    http://www.bertram-scharpf.de
     
    Bertram Scharpf, Dec 25, 2007
    #1
    1. Advertising

  2. Alle marted=EC 25 dicembre 2007, Bertram Scharpf ha scritto:
    > Hi,
    >
    >
    > this may be easy to anwer but it is difficult to google for.
    >
    > While developping I would like to play around with objects from
    > time to time. Just like this:
    >
    > irb(main):001:0> class C ; def f ; "F" ; end ; end
    > =3D> nil
    > irb(main):002:0> irb C.new
    > irb#1(#<C:0xb7910d40>):001:0> f
    > =3D> "F"
    > irb#1(#<C:0xb7910d40>):002:0>
    >
    > In common my classes are not so easy to type in. So, I would like
    > to call Irb from somewhere inside the application as I do here:
    >
    > ---->-call_irb.rb---------------
    > #!/usr/bin/ruby
    >
    > class C
    > def f
    > "F"
    > end
    > end
    >
    > if $0 =3D=3D __FILE__ then
    > require "irb"
    > $c =3D C.new
    > IRB.start
    > end
    > ----<---------------------------
    >
    > and then
    >
    > user@host $ ./call_irb.rb
    > irb(main):001:0> irb $c
    > irb#1(#<C:0xb7be4ee0>):001:0> f
    > =3D> "F"
    > irb#1(#<C:0xb7be4ee0>):002:0>
    >
    > Is there a way how I can call C.new's Irb directly without doing
    > the long-winded definition of a global variable first? I already
    > examined the irb sources but this seems to be well-hidden to me.
    >
    > Thanks in advance and Merry Christmas,
    >
    > Bertram


    Not a direct answer to your question, but can't you load the file with the=
    =20
    definition of the class in irb?

    Stefano
     
    Stefano Crocco, Dec 25, 2007
    #2
    1. Advertising

  3. Bertram Scharpf

    James Tucker Guest

    Merry christmas!

    This was mercilessly stolen and adapted from a post on Errs blog (only =20=

    google has the answer ;):

    require 'irb'

    module IRB
    def self.start_session(binding)
    IRB.setup(nil)

    workspace =3D WorkSpace.new(binding)

    if @CONF[:SCRIPT]
    irb =3D Irb.new(workspace, @CONF[:SCRIPT])
    else
    irb =3D Irb.new(workspace)
    end

    @CONF[:IRB_RC].call(irb.context) if @CONF[:IRB_RC]
    @CONF[:MAIN_CONTEXT] =3D irb.context

    trap("SIGINT") do
    irb.signal_handle
    end

    catch:)IRB_EXIT) do
    irb.eval_input
    end
    end
    end

    def meths(o); puts (o.methods - Class.new.methods).join("\n"); end

    def dROP! b
    old_args =3D ARGV
    ARGV.size.times { ARGV.shift }

    if defined? IRBHelper
    foo =3D Class.new
    foo.instance_eval do
    include IRBHelper
    end
    puts "Helper Methods: #{(foo.new.methods - =20
    Class.new.methods).sort.join(', ')}"
    include IRBHelper
    end

    IRB.start_session b
    old_args.each { |a| ARGV << a }
    end





    On 25 Dec 2007, at 13:26, Stefano Crocco wrote:

    > Alle marted=EC 25 dicembre 2007, Bertram Scharpf ha scritto:
    >> Hi,
    >>
    >>
    >> this may be easy to anwer but it is difficult to google for.
    >>
    >> While developping I would like to play around with objects from
    >> time to time. Just like this:
    >>
    >> irb(main):001:0> class C ; def f ; "F" ; end ; end
    >> =3D> nil
    >> irb(main):002:0> irb C.new
    >> irb#1(#<C:0xb7910d40>):001:0> f
    >> =3D> "F"
    >> irb#1(#<C:0xb7910d40>):002:0>
    >>
    >> In common my classes are not so easy to type in. So, I would like
    >> to call Irb from somewhere inside the application as I do here:
    >>
    >> ---->-call_irb.rb---------------
    >> #!/usr/bin/ruby
    >>
    >> class C
    >> def f
    >> "F"
    >> end
    >> end
    >>
    >> if $0 =3D=3D __FILE__ then
    >> require "irb"
    >> $c =3D C.new
    >> IRB.start
    >> end
    >> ----<---------------------------
    >>
    >> and then
    >>
    >> user@host $ ./call_irb.rb
    >> irb(main):001:0> irb $c
    >> irb#1(#<C:0xb7be4ee0>):001:0> f
    >> =3D> "F"
    >> irb#1(#<C:0xb7be4ee0>):002:0>
    >>
    >> Is there a way how I can call C.new's Irb directly without doing
    >> the long-winded definition of a global variable first? I already
    >> examined the irb sources but this seems to be well-hidden to me.
    >>
    >> Thanks in advance and Merry Christmas,
    >>
    >> Bertram

    >
    > Not a direct answer to your question, but can't you load the file =20
    > with the
    > definition of the class in irb?
    >
    > Stefano
    >
     
    James Tucker, Dec 25, 2007
    #3
  4. Bertram Scharpf

    Ryan Davis Guest

    On Dec 25, 2007, at 05:08 , Bertram Scharpf wrote:

    > In common my classes are not so easy to type in. So, I would like
    > to call Irb from somewhere inside the application as I do here:
    >
    > if $0 == __FILE__ then
    > require "irb"
    > $c = C.new
    > IRB.start
    > end


    I'm a bit confused. Your subject line IS handled by this code. Indeed,
    this is what I grabbed from some of my code when I read the subject
    line:

    def explore
    Object.const_set :"G", self
    require 'irb'
    puts "Your grammar is in the constant G"
    IRB.start(__FILE__)
    end

    So, what is the actual subject of this question? It doesn't seem to be
    "long-windedness" (below) either.

    > and then
    >
    > user@host $ ./call_irb.rb
    > irb(main):001:0> irb $c
    > irb#1(#<C:0xb7be4ee0>):001:0> f
    > => "F"
    > irb#1(#<C:0xb7be4ee0>):002:0>
    >
    > Is there a way how I can call C.new's Irb directly without doing
    > the long-winded definition of a global variable first? I already
    > examined the irb sources but this seems to be well-hidden to me.


    I'm confused and havet o assume the question is a bit vague. How is
    "$c = " long winded? What do you actually want to know?
     
    Ryan Davis, Dec 25, 2007
    #4
  5. Hi James,

    Am Dienstag, 25. Dez 2007, 23:01:54 +0900 schrieb James Tucker:
    > This was mercilessly stolen and adapted from a post on Errs blog (only
    > google has the answer ;):
    >
    > [...]
    > old_args = ARGV
    > ARGV.size.times { ARGV.shift }
    > [...]
    > old_args.each { |a| ARGV << a }
    > [...]


    The code is not actually mature. The clearing could be done by an
    ARGV.clear; this part won't work either because old_args will be
    cleared as well.

    Besides that the solution works perfectly. Thank you very much!

    Bertram


    --
    Bertram Scharpf
    Stuttgart, Deutschland/Germany
    http://www.bertram-scharpf.de
     
    Bertram Scharpf, Dec 25, 2007
    #5
  6. Hi,

    Am Mittwoch, 26. Dez 2007, 05:16:03 +0900 schrieb Ryan Davis:
    > On Dec 25, 2007, at 05:08 , Bertram Scharpf wrote:
    >> Is there a way how I can call C.new's Irb directly without doing
    >> the long-winded definition of a global variable first? I already
    >> examined the irb sources but this seems to be well-hidden to me.

    >
    > I'm confused and havet o assume the question is a bit vague. How is "$c = "
    > long winded? What do you actually want to know?


    I wanted to know how to do it without having to type in the same
    constant or global variable name every time.

    Bertram


    --
    Bertram Scharpf
    Stuttgart, Deutschland/Germany
    http://www.bertram-scharpf.de
     
    Bertram Scharpf, Dec 25, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sam Stephenson
    Replies:
    1
    Views:
    229
    Andrew Walrond
    Jun 18, 2005
  2. Replies:
    1
    Views:
    162
    Florian GroƟ
    Oct 26, 2005
  3. Donkey Agony
    Replies:
    2
    Views:
    111
    Donkey Agony
    Jan 7, 2006
  4. anne001
    Replies:
    1
    Views:
    277
    anne001
    Jun 27, 2006
  5. Sijo Kg
    Replies:
    3
    Views:
    86
    Brian Candler
    Dec 11, 2008
Loading...

Share This Page