views:

161

answers:

4

When using something like object.methods.sort.to_yaml I'd like to have irb interpret the \n characters rather than print them.

I currently get the following output:

--- \n- "&"\n- "*"\n- +\n- "-"\n- "<<"\n- <=>\n ...

What I'd like is something similar to this:

--- 
 - "&"
 - "*"
 - +
 - "-"
 - "<<"
 - <=>

Is this possible? Is there another method I can be calling which will interpret the string perhaps?

A: 

That's just irb -- I don't think you can control the return formatting.

You can still use print or puts to display it as you want.

Jonathan Lonowski
+2  A: 

Prefix your output with puts:

> puts object.methods.sort.to_yaml
--- 
 - "&"
 - "*"
 - +
 - "-"
 - "<<"
 - <=>
 => nil
Konrad Rudolph
Thanks very much - this is exactly what I was looking for :)
mlambie
By default, irb calls the inspect method to print the object, which, for a string, escapes special characters such as \n. Print and puts interpret special characters and call to_s on non-strings.
Firas Assaad
+1  A: 

Another option is to start irb with the noinspect option:

C:\>irb --noinspect
irb(main):001:0> Object.methods.to_yaml
=> ---
- instance_method
- yaml_tag_read_class
.....
- constants
- is_a?

irb(main):002:0>
Firas Assaad
+1  A: 

The Ruby yaml library includes the "y" command, which takes care of both the yamlizing and the formatting:

y object.methods.sort
slothbear