1

I'm wondering if there's a way to return an object instead of a string when calling an object without any methods.

For instance:

class Foo
  def initialize
    @bar = Bar.new
  end
end

Is there any way to define the Foo class so that the following happens:

foo = Foo.new
foo #returns @bar  

In the specific case I'm interested in I'm using a presenter in a Rails view. The presenter sets up one main object and then loads a bunch of related content. The important part looks like this:

class ExamplePresenter

  def initialize( id )
    @example = Example.find( id )
  end

  def example
    @example
  end

  ...

end

If I want to return the example used by the ExamplePresenter I can call:

@presenter = ExamplePresenter.new(1)
@presenter.example

It would be nice if I could also return the example object by just calling:

@presenter

So, is there a way to set a default method to return when an object is called, like to_s but returning an object instead of a string?

3
  • Could you be more specific about where @presenter is used? Is this in a view? Jul 7, 2011 at 19:11
  • This is in a Rails view, but really I'm interested in just the ruby language related to objects period. I'm curious if there's a way to define a class so that it would return something other than itself when you call it without any methods.
    – Andrew
    Jul 7, 2011 at 19:14
  • you can't call a class cause its an object ( you can call object_id on class) not a method you might redefine to_s or inspect methods maybe
    – Bohdan
    Jul 7, 2011 at 19:23

2 Answers 2

1

If I understand correctly, you want to return the instance of Example when you call the ExamplePresenter instance. Such a direct mechanism does not exist in any language, and even if it did, it would block all access to the ExamplePresenter instance and its methods. So it is not logical.

There is something you can do however. You can make the ExamplePresenter class delegate methods to the Example instance inside it. Effectively you do not get a real Example from @presenter but you get an ExamplePresenter that passes all eligible methods into its internal Example effectively acting in behalf of it.

Some ways of doing this is:

method_missing

class ExamplePresenter
  … # as defined in the question

  def method_missing symbol, *args
    if @example.respond_to?(symbol)
      @example.send(symbol, *args)
    else
      super
    end
  end
end

This will pass any method call down to the internal Example if the ExamplePresenter cannot respond to it. Be careful, you may expose more than you want of the internal Example this way, and any method already defined on ExamplePresenter cannot be passed along.

You can use additional logic inside method_missing to limit exposure or pre/post process the arguments/return values.

Wrapper methods

You can define wrapper methods on ExamplePresenter that do nothing but pass everything to the internal Example. This gives you explicit control on how much of it you want to expose.

class ExamplePresenter
  … # as before

  def a_method
    @example.a_method
  end
  def another_method(argument, another_argument)
    @example.another_method(argument, another_argument)
  end
end

This gets tedious fast, but you can also add logic to alter arguments before passing it along to the Example or post process the results.

You can also mix and match the above two methods

Delegator library

There is a library in Ruby stdlib called Delegator built exactly for this purpose. You may look into it.

2
  • Thanks for the detailed answer. Your first note that "it would block all access to the ExamplePresenter instance" occurred to me about half an hour after I wrote the question... but your suggestions regarding method_missing and wrappers would definitely let me do what I'm looking to do. This is very helpful. Thanks!
    – Andrew
    Jul 7, 2011 at 20:28
  • there's also the forwardable ruby-doc.org/stdlib/libdoc/forwardable/rdoc/index.html if you only want a few methods. Jul 8, 2011 at 2:12
0

Although this is not recommended, you can do:

class Foo
  def self.new
    @bar = Bar.new
  end
end

If you actually do need to create an instance of Foo, then

class << Foo
  alias original_new :new
end

class Foo
  def self.new
    self.original_new # It will not be useful unless you assign this to some variable.
    @bar = Bar.new
  end
end

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.