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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
D5df9fcd7ef4c3c937435d7d6adeab2a?d=identicon&s=25 Greg Hauptmann (Guest)
on 2009-01-20 21:03
(Received via mailing list)
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?
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2009-01-20 21:18
(Received via mailing list)
On Jan 20, 2009, at 3:00 PM, Greg Hauptmann wrote:
> 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@AgileConsultingLLC.com
D5df9fcd7ef4c3c937435d7d6adeab2a?d=identicon&s=25 Greg Hauptmann (Guest)
on 2009-01-20 21:28
(Received via mailing list)
excellent! thanks

On Wed, Jan 21, 2009 at 6:16 AM, Rob Biedenharn
5a837592409354297424994e8d62f722?d=identicon&s=25 Ryan Davis (Guest)
on 2009-01-20 22:28
(Received via mailing list)
On Jan 20, 2009, at 12:00 , Greg Hauptmann wrote:

> 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.
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2009-01-20 23:35
(Received via mailing list)
> 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!
F53b05cdbdf561cfe141f69b421244f3?d=identicon&s=25 David A. Black (Guest)
on 2009-01-20 23:50
(Received via mailing list)
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!
This topic is locked and can not be replied to.