0

I'm trying to implement a gem that is redis wrapper for other library i.e to store the ruby object in the redis.

all work well but what I want is when I do

[Class].all It give object like this

[#<Peagon:0x007fba589de3a0 @name="a", @omg=false ,@payload="one",@handler="--- one\n...\n"> ,#<Peagon:0x007fba589de1a0 @name="b", @omg=true,@payload="two",@handler="--- two\n...\n">]

but instead I want it to be look like how active record present the object

[#<Peagon name: "a",omg: false ,handler: "--- one\n...\n"> ,#<Peagon name="b", omg: true,handler: "--- two\n...\n">]

The reason for this that I not interested in showing the user the @payload instance variable because that is something set by the other library

so basically like this happen [My gem]

class Peagon
 include SomeModule
 attr_accessor :name,:omg,:handler 
 def initialize(options)
   @name = options[:name]
   @omg  = options
   self.payload_object = options[:payload_object]
 end

end 

Now the [Other Library] has this module in it

module SomeModule 
 def payload=(object)
   @payload ||= object
   self.handler = @payload.to_yaml

 end 

 def payload
   @payload ||= YAML.load(self.handler)
 end

end

NOTE :

Overwriting the payload method from other library is not in my mind

Now is it possible to get what I meant above

1 Answer 1

1

Looks like you just want to adjust what irb, the Rails console, and friends will display for objects of your class. If so, they just call inspect:

inspect → string

Returns a string containing a human-readable representation of obj. By default, show the class name and the list of the instance variables and their values (by calling inspect on each of them). User defined classes should override this method to make better representation of obj.

So all you need to do is provide your own inspect implementation, something like:

def inspect
  "#<#{class} name: #{@name.inspect} ...>"
end
Sign up to request clarification or add additional context in comments.

4 Comments

Opps I forget to mention I tried this approach too and I gave on this because the inspect return string object thereby making invoking of object/instance method impossible but in activerecord it give you the object of the class so you can invoke the object/instance method on it
Any other approach that would give me back the object of the class with the desired behavior
Sorry, I don't understand. When I say Model in the Rails console, I get Model(id: integer, ...) as a result because that's what inspect says for the class; if I say Model.first, I get #<Model id: ...> because that's what inspect says for the instance. None of this has anything to do with getting an "object of the class with the desired behavior".
Opps :P I was thinking uptil now that inspect would return the string object cross checked it thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.