Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6104999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:54:25+00:00 2026-05-23T13:54:25+00:00

I’m wondering if there’s a way to return an object instead of a string

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T13:54:25+00:00Added an answer on May 23, 2026 at 1:54 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
I want to count how many characters a certain string has in PHP, but
I have text I am displaying in SIlverlight that is coming from a CMS
Seemingly simple, but I cannot find anything relevant on the web. What is the
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.