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

  • SEARCH
  • Home
  • 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 4537424
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T14:40:26+00:00 2026-05-21T14:40:26+00:00

I am trying to create a class that does a couple of things: 1)

  • 0

I am trying to create a class that does a couple of things:
1) Implements a custom getter for an attribute
2) Calls the custom getter from within the initialize method

Here’s what it looks like:

class Book

  # RSolr lib for interacting with Solr
  require 'rsolr' 

  # Instance variables 
  @isbn
  @title

  # Solr playlist instance URL
  @solr_domain
  @solr

  def initialize(isbn)
    @solr_domain = "http://solr.com:9003/solr"
    @solr = RSolr.connect :url => @solr_domain
    @isbn = isbn
    @title = self.title(isbn)
  end

  # Get Solr URL
  def solr_domain
    return @solr_domain
  end

# Set Solr URL and reset Solr connection object
  def solr_domain(newurl)
    @solr_domain = newurl
    @solr = RSolr.connect :url => @solr_domain
  end

  # Custom getter for title
  def title=(isbn)
    result = solr.get 'select', :params => {:q => 'isbn:(' + isbn + ')'}
    return result["response"]["docs"][0]["title"]
      end
end

The key lines are

@title = self.title(isbn)

where we attempt to call the getter for title, so that title gets set when the object is initialized.

What we want is a publicly accessible getter for title, as well as a way to populate @title during initialization of the object.

  • 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-21T14:40:27+00:00Added an answer on May 21, 2026 at 2:40 pm

    For being able to preset the title, you could do something like this:

    def initialize(isbn, user_title = nil)
      ...
      @title = user_title || self.title(isbn)
    end
    

    There’s an optional argument the user can supply. If he/she does, @title will get set to that value, otherwise we’ll look up the title ourselves with the ISBN. I hope I understood you correctly…

    Edit: BTW, are you sure you want class level instance variables?

    http://railstips.org/blog/archives/2006/11/18/class-and-instance-variables-in-ruby/

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

Sidebar

Related Questions

I'm trying to create a UserControl that inherits from a generic class. It does
I'm trying to create an abstract class that defines a property with a getter.
I am trying to create an ActionScript 3 class that implements two interfaces. The
I'm trying to create a LINQ to SQL class that represents the latest version
I need to create a class that gets a delegate does some calculation and
I'm trying to create a template class that stores a function pointer to a
I'm trying to create a class that represents hours of operation in an Obj-C
I'm trying to create a class that serves as a base object, which will
I'm trying to create a C++ class, with a templated superclass. The idea being,
I'm trying to create an instance of a class at run time. The classes

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.