newbie with a weird technical problem (@ least I think it's weird)

Discussion in 'Ruby' started by will, Dec 27, 2006.

  1. will

    will Guest

    Yep, as the post title implies, I'm a newbie. Being a pragmatic newbie
    I'd rather be learning to program, than searching for answers to
    non-language acquisition questions/problems. I'm pretty sure the
    following falls in that latter group, so if it's in a manual somewhere
    please tell me so _politely_ and point me to _the_ manual. :)

    I'm starting to go through Dave Thomas' Programming Ruby/Picaxe (I'm on
    pgs 25-26 2nd edition if you care) and I'm using TextMate/Terminal to
    code and run modified versions of the book's examples. So I ran a
    version of the following with a typographical error, which was apparent
    with the Terminal feedback. So far, so good. I fixed the error and
    tried to run it again. The problem is that now when I run it, all I
    get in response is a new blank prompt. I've tried renaming the file,
    the class, giving it a completely new set of variables in the array and
    quiting/restarting the Terminal. If I type the _exact_ same thing into
    irb I'm able to create a book and book2 instance and .inspect each of
    them. But with the TextMate/Terminal combo = Nothing, nadda, zip!
    HELP, PLEASE!


    #filename bib.rb

    class Book
    def initialize(title, author, pages)
    @title = title
    @author = author
    @pages = pages
    end
    end

    book = Book.new("Ruby Tutorial", "Dave", 831)
    book.inspect


    #terminal results
    computer: me$ ruby bib.rb
    computer: me$
     
    will, Dec 27, 2006
    #1
    1. Advertising

  2. will wrote:
    > Yep, as the post title implies, I'm a newbie. Being a pragmatic newbie
    > I'd rather be learning to program, than searching for answers to
    > non-language acquisition questions/problems. I'm pretty sure the
    > following falls in that latter group, so if it's in a manual somewhere
    > please tell me so _politely_ and point me to _the_ manual. :)
    >
    > I'm starting to go through Dave Thomas' Programming Ruby/Picaxe (I'm on
    > pgs 25-26 2nd edition if you care) and I'm using TextMate/Terminal to
    > code and run modified versions of the book's examples. So I ran a
    > version of the following with a typographical error, which was apparent
    > with the Terminal feedback. So far, so good. I fixed the error and
    > tried to run it again. The problem is that now when I run it, all I
    > get in response is a new blank prompt. I've tried renaming the file,
    > the class, giving it a completely new set of variables in the array and
    > quiting/restarting the Terminal. If I type the _exact_ same thing into
    > irb I'm able to create a book and book2 instance and .inspect each of
    > them. But with the TextMate/Terminal combo = Nothing, nadda, zip!
    > HELP, PLEASE!
    >
    >
    > #filename bib.rb
    >
    > class Book
    > def initialize(title, author, pages)
    > @title = title
    > @author = author
    > @pages = pages
    > end
    > end
    >
    > book = Book.new("Ruby Tutorial", "Dave", 831)
    > book.inspect
    >
    >
    > #terminal results
    > computer: me$ ruby bib.rb
    > computer: me$
    >
    >
    >


    It's a language question. The inspect method returns a string. That
    string is not automatically displayed on the terminal. You have to print
    the string if you want to see it. Use "puts book.inspect" for example.

    The reason you see the result in irb is that irb prints it for you.
     
    Timothy Hunter, Dec 27, 2006
    #2
    1. Advertising

  3. > If I type the _exact_ same thing into
    > irb I'm able to create a book and book2 instance and .inspect each of
    > them. But with the TextMate/Terminal combo = Nothing, nadda, zip!
    > HELP, PLEASE!

    <...>

    > book = Book.new("Ruby Tutorial", "Dave", 831)
    > book.inspect


    It is because you do not output anything.
    Change the last line to puts book.inspect


    Regards,
    Rimantas
    --
    http://rimantas.com/
     
    Rimantas Liubertas, Dec 27, 2006
    #3
  4. will

    Dick Davies Guest

    On 27/12/06, will <> wrote:


    > If I type the _exact_ same thing into
    > irb I'm able to create a book and book2 instance and .inspect each of
    > them. But with the TextMate/Terminal combo = Nothing, nadda, zip!
    > HELP, PLEASE!
    >
    >
    > #filename bib.rb
    >
    > class Book
    > def initialize(title, author, pages)
    > @title = title
    > @author = author
    > @pages = pages
    > end
    > end
    >
    > book = Book.new("Ruby Tutorial", "Dave", 831)
    > book.inspect
    >
    >
    > #terminal results
    > computer: me$ ruby bib.rb
    > computer: me$


    The reason you see output in irb is that irb shows you what
    the last statement evaluated to, like this:

    rasputnik@hypnotoad:~$ irb
    irb(main):001:0> 1
    => 1
    irb(main):002:0> quit


    Object#inspect returns a string, but you aren't doing anything with it!
    irb is showing you the value of book.inspect as usual, but the script
    version is less verbose.

    try

    puts book.inspect

    and you'll see output from the non-irb version.

    --
    Rasputin :: Jack of All Trades - Master of Nuns
    http://number9.hellooperator.net/
     
    Dick Davies, Dec 27, 2006
    #4
  5. will

    will Guest

    Thank you! Your explanation helps me understand the subsequent
    failures. Any idea why it 'almost' worked the first time? (it printed
    the @pages and @author info, and then it told me there was an error.)


    On Dec 27, 6:44 am, Timothy Hunter <> wrote:
    > will wrote:
    > > Yep, as the post title implies, I'm a newbie. Being a pragmatic newbie
    > > I'd rather be learning to program, than searching for answers to
    > > non-language acquisition questions/problems. I'm pretty sure the
    > > following falls in that latter group, so if it's in a manual somewhere
    > > please tell me so _politely_ and point me to _the_ manual. :)

    >
    > > I'm starting to go through Dave Thomas' Programming Ruby/Picaxe (I'm on
    > > pgs 25-26 2nd edition if you care) and I'm using TextMate/Terminal to
    > > code and run modified versions of the book's examples. So I ran a
    > > version of the following with a typographical error, which was apparent
    > > with the Terminal feedback. So far, so good. I fixed the error and
    > > tried to run it again. The problem is that now when I run it, all I
    > > get in response is a new blank prompt. I've tried renaming the file,
    > > the class, giving it a completely new set of variables in the array and
    > > quiting/restarting the Terminal. If I type the _exact_ same thing into
    > > irb I'm able to create a book and book2 instance and .inspect each of
    > > them. But with the TextMate/Terminal combo = Nothing, nadda, zip!
    > > HELP, PLEASE!

    >
    > > #filename bib.rb

    >
    > > class Book
    > > def initialize(title, author, pages)
    > > @title = title
    > > @author = author
    > > @pages = pages
    > > end
    > > end

    >
    > > book = Book.new("Ruby Tutorial", "Dave", 831)
    > > book.inspect

    >
    > > #terminal results
    > > computer: me$ ruby bib.rb
    > > computer: me$It's a language question. The inspect method returns a string. That

    > string is not automatically displayed on the terminal. You have to print
    > the string if you want to see it. Use "puts book.inspect" for example.
    >
    > The reason you see the result in irb is that irb prints it for you.
     
    will, Dec 27, 2006
    #5
  6. On Dec 27, 2006, at 8:37 AM, will wrote:

    > ... I'm using TextMate/Terminal to
    > code and run modified versions of the book's examples.


    Since you're using TextMate (as I do), I recommend running Ruby code
    from within TextMate. No need to bother with Terminal.

    1. Use Bundles>Ruby>Run to run your script. Output goes to a RubyMate
    window which will pop up.

    2. Try the following:

    class Book
    def initialize(title, author, pages)
    @title = title
    @author = author
    @pages = pages
    end
    end

    book = Book.new("Ruby Tutorial", "Dave", 831)
    book.inspect # =>

    Note the '# =>' added to the last line. Choose Ruby>Evaluate and
    Update '# =>' Markers from the Bundles menu. The last line will
    change to

    book.inspect # => "#<Book:0x25454 @author=\"Dave\", @title=\"Ruby
    Tutorial\", @pages=831>"

    There are shortcuts for these bundle commands, and there are several
    other aids to evaluating Ruby code built-in to the TextMate Ruby
    bundle. I think you will find TextMate makes it easy to explore Ruby
    without constantly switching back and forth to Terminal.

    Regards, Morton
     
    Morton Goldberg, Dec 27, 2006
    #6
  7. will

    Phrogz Guest

    will wrote:
    > book = Book.new("Ruby Tutorial", "Dave", 831)
    > book.inspect


    Others have mentioned the need to output the value you are returning,
    and have suggested:
    puts book.inspect
    as a way to see that information. I just wanted to add the suggestion
    that the 'p' method:
    p book
    automatically calls #inspect on the object(s) passed, and then spits
    them to stdout like puts does. (For comparison, the puts method calls
    #to_s on the object(s) passed.) 'p' is very convenient when developing
    and testing out your application, as the output sometimes gives you
    more insight into the actual format of the data...particularly for
    arrays:

    irb(main):001:0> a = [ 1, '2', [ '3 4', 5 ], 6 ]
    => [1, "2", ["3 4", 5], 6]

    irb(main):002:0> puts a
    1
    2
    3 4
    5
    6
    => nil

    irb(main):003:0> p a
    [1, "2", ["3 4", 5], 6]
    => nil

    As you can see from that first line, it's the result of #inspect that
    irb uses when outputting the result of the last command. (Both puts and
    p return nil when finished, which is where those two "=> nil" lines
    come from in irb.)
     
    Phrogz, Dec 27, 2006
    #7
    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. Brian
    Replies:
    4
    Views:
    300
    Brian
    Apr 25, 2005
  2. Replies:
    0
    Views:
    636
  3. Replies:
    1
    Views:
    898
    Jack Klein
    Apr 13, 2005
  4. AAaron123
    Replies:
    0
    Views:
    631
    AAaron123
    Oct 3, 2008
  5. David Mark
    Replies:
    17
    Views:
    255
    David Mark
    Mar 23, 2010
Loading...

Share This Page