rails console output shows "\n" rather than putting in new line? isthere a way to change this?

Discussion in 'Ruby' started by Greg Hauptmann, Jan 20, 2009.

  1. [Note: parts of this message were removed to make it a legal post.]

    Hi,
    I note rails console shows "\n" rather than putting in new line? For
    example:

    ?> Category.find:)first).to_yaml
    => "--- !ruby/object:Category \nattributes: \n updated_at: 2009-01-20
    03:39:22\n tax_item_id: \n category_type_id: \"2\"\n title: Investment\n id:
    \"1\"\n category_id: \n category_group_id: \"5\"\n created_at: 2009-01-20
    03:39:22\n active: \"0\"\nattributes_cache: {}\n\n"


    I note that if you "puts" the same thing from an Rspec test it formats fine
    (with new lines)

    Question: Is there a way to change this for the console?



    --
    Greg
    http://blog.gregnet.org/
     
    Greg Hauptmann, Jan 20, 2009
    #1
    1. Advertising

  2. Re: rails console output shows "\n" rather than putting in new line?is there a way to change this?

    On Jan 20, 2009, at 3:00 PM, Greg Hauptmann wrote:
    > Hi,
    > I note rails console shows "\n" rather than putting in new line? For
    > example:
    >
    > ?> Category.find:)first).to_yaml
    > => "--- !ruby/object:Category \nattributes: \n updated_at: 2009-01-20
    > 03:39:22\n tax_item_id: \n category_type_id: \"2\"\n title:
    > Investment\n id:
    > \"1\"\n category_id: \n category_group_id: \"5\"\n created_at:
    > 2009-01-20
    > 03:39:22\n active: \"0\"\nattributes_cache: {}\n\n"
    >
    >
    > I note that if you "puts" the same thing from an Rspec test it
    > formats fine
    > (with new lines)
    >
    > Question: Is there a way to change this for the console?
    > --
    > Greg
    > http://blog.gregnet.org/



    either:
    puts Category.find:)first).to_yaml
    or since irb saves the last expression into the variable _ (yes, an
    underscore):
    puts _

    -Rob

    Rob Biedenharn http://agileconsultingllc.com
     
    Rob Biedenharn, Jan 20, 2009
    #2
    1. Advertising

  3. Re: rails console output shows "\n" rather than putting in new line?is there a way to change this?

    [Note: parts of this message were removed to make it a legal post.]

    excellent! thanks

    On Wed, Jan 21, 2009 at 6:16 AM, Rob Biedenharn
    <>wrote:

    > On Jan 20, 2009, at 3:00 PM, Greg Hauptmann wrote:
    >
    >> Hi,
    >> I note rails console shows "\n" rather than putting in new line? For
    >> example:
    >>
    >> ?> Category.find:)first).to_yaml
    >> => "--- !ruby/object:Category \nattributes: \n updated_at: 2009-01-20
    >> 03:39:22\n tax_item_id: \n category_type_id: \"2\"\n title: Investment\n
    >> id:
    >> \"1\"\n category_id: \n category_group_id: \"5\"\n created_at: 2009-01-20
    >> 03:39:22\n active: \"0\"\nattributes_cache: {}\n\n"
    >>
    >>
    >> I note that if you "puts" the same thing from an Rspec test it formats
    >> fine
    >> (with new lines)
    >>
    >> Question: Is there a way to change this for the console?
    >> --
    >> Greg
    >> http://blog.gregnet.org/
    >>

    >
    >
    > either:
    > puts Category.find:)first).to_yaml
    > or since irb saves the last expression into the variable _ (yes, an
    > underscore):
    > puts _
    >
    > -Rob
    >
    > Rob Biedenharn http://agileconsultingllc.com
    >
    >
    >
    >
    >



    --
    Greg
    http://blog.gregnet.org/
     
    Greg Hauptmann, Jan 20, 2009
    #3
  4. Greg Hauptmann

    Ryan Davis Guest

    Re: rails console output shows "\n" rather than putting in new line?is there a way to change this?

    On Jan 20, 2009, at 12:00 , Greg Hauptmann wrote:

    > Hi,
    > I note rails console shows "\n" rather than putting in new line? For
    > example:
    >
    > ?> Category.find:)first).to_yaml
    > => "--- !ruby/object:Category \nattributes: \n updated_at: 2009-01-20
    > 03:39:22\n tax_item_id: \n category_type_id: \"2\"\n title:
    > Investment\n id:
    > \"1\"\n category_id: \n category_group_id: \"5\"\n created_at:
    > 2009-01-20
    > 03:39:22\n active: \"0\"\nattributes_cache: {}\n\n"


    irb inspects the result, either don't convert it to a string, or print
    it.

    ?> Category.find:)first)

    or

    ?> y Category.find:)first)

    or as rob pointed out:

    ?> puts Category.find:)first).to_yaml

    Usually I don't care about all the fields, so I'll do something like

    ?> Category.find:)first).title

    and leave it at that.
     
    Ryan Davis, Jan 20, 2009
    #4
  5. Greg Hauptmann

    Phlip Guest

    Re: rails console output shows "\n" rather than putting in new line?is there a way to change this?

    > Question: Is there a way to change this for the console?

    Don't rely on the console so much. Put puts in a unit test!
     
    Phlip, Jan 20, 2009
    #5
  6. Re: rails console output shows "\n" rather than putting in new line?is there a way to change this?

    Hi --

    On Wed, 21 Jan 2009, Phlip wrote:

    >> Question: Is there a way to change this for the console?

    >
    > Don't rely on the console so much. Put puts in a unit test!


    I rely heavily on the console, and also on unit tests. The two aren't
    in conflict or competition; they're different tools. It's important to
    know how irb (in Rails console form or otherwise) works, and why it
    produces the output it does.


    David

    --
    David A. Black / Ruby Power and Light, LLC
    Ruby/Rails consulting & training: http://www.rubypal.com
    Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2)

    http://www.wishsight.com => Independent, social wishlist management!
     
    David A. Black, Jan 20, 2009
    #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. gce
    Replies:
    0
    Views:
    773
  2. Kenneth McDonald
    Replies:
    1
    Views:
    404
    =?iso-8859-1?Q?Fran=E7ois?= Pinard
    Oct 10, 2004
  3. thinktwice
    Replies:
    10
    Views:
    1,759
    EventHelix.com
    Jul 1, 2008
  4. Greg Hauptmann
    Replies:
    4
    Views:
    149
    Greg Hauptmann
    Dec 8, 2009
  5. Gianni

    IE shows false and Firefox shows true

    Gianni, Jul 10, 2009, in forum: Javascript
    Replies:
    3
    Views:
    458
    Thomas 'PointedEars' Lahn
    Jul 10, 2009
Loading...

Share This Page