Ruby object printing format

Consider the following:

irb(main):001:0> class A
irb(main):002:1> def initialize
irb(main):003:2> @string = "my string"
irb(main):004:2> end
irb(main):005:1> def to_s
irb(main):006:2> puts @string
irb(main):007:2> end
irb(main):008:1> end
=> nil
irb(main):009:0> a = A.new
=> #<A:0x2ea606c @string="my string">
irb(main):010:0> puts a
my string
#<A:0x2ea606c>
=> nil

When 'puts' outputs the string my does it also append the refference of the object ?

Is there anyway to get rid of this behavior? What I want outputed is justs the string @string as defined in the classes 'to_s' method

Thanks,

RM

Answers


You want your to_s method to just return @string, not do puts @string.


Need Your Help

Is it safe to use the TAG `android:switcher: + R.id.ot_pag_main + :0` to find a fragment?

android android-viewpager

I haven't seen this on the Android docs. Is it safe to use this automatically generated tag to refer to the fragments inside a viewPager?

Using MongoDB should I nest everything related to a user in the user schema?

node.js mongodb nosql mongoose

Threw an example below, makes the question easier I think. Should I do this, or is it better for performance to split things up? I'm probably going to want to add things to this later too, down the...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.