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 8992385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:56:30+00:00 2026-06-15T22:56:30+00:00

How it is possible to easy access setter within a same class? Lets say

  • 0

How it is possible to easy access setter within a same class?

Lets say I have this call:

# some_file.rb , create a new object
temperature = Measurement.new 'temperature', 36.6

And a model:

# Measurements.rb , some model
class Measurement
  attr_accessor :value, :type
  attr_reader :device, :title, :label

  def initialize type, value
    # This one works, but not trigger the setter
    @type = type
    # And this one trigger setter
    self.type = type
  end

  def type= new_type
    # do something very important
  end    
end

Does this mean i always need to use self.var = instead of @var = if I ever want to use setters with this variable without renaming it everywhere in the class? Are there any more difference, and some better way to add a setter?

  • 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-06-15T22:56:32+00:00Added an answer on June 15, 2026 at 10:56 pm

    Yes, you always do. In Ruby, there is not such thing really as a setter. Just a method that ends in =. However, because of syntax ambiguity you must always preface setters with an object and a period, as otherwise Ruby will see an assignment to a local variable instead.

    In other words, remember that @blah is raw access, and will never trigger a method (not completely true, there are some logging and debugging hock methods, but never-mind).

    So just live with self.blah= if your setter is that important.

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

Sidebar

Related Questions

I am coming from c# where this was easy, and possible. I have this
Let's say in an environment I don't have easy access to trig functions, or
Is it possible/easy to create a web page which would act like a Microsoft
Possible Duplicate: Create shortcut to console.log() In javascript we can easy assign functions to
Hopefully this will be an easy one to answer. I created a class in
So say, for example I have two classes: Base = declarative_base() class Vendor(Base): __tablename__
I still very new using Subversion. Is it possible to have a working copy
Short version: Is it easy/feasible/possible to program modal window in Flash (AS3)? Is there
Hopefullay an easy answer but is it possible to disable outlook 2003 toast pop-up
Is it possible to use C/C++ code in Android? And how easy is it?

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.