Questions tagged [attr-accessor]

An attribute accessor in Ruby is a way of declaring attribute accessibility (read and write) via Ruby's metaprogramming facilities.

Usage

Given any Ruby class, one can use the attr_accessor method like so:

class Car
    attr_accessor :speed
end

This enables us to read and write the speed instance variable of any instance of Car:

my_car = Car.new
my.car.speed = 100
my.car.speed # => 100

The attr_accessor method implements this functionality by creating new methods for the class it was called in.

Internals

Therefore, using attr_accessor is actually equivalent to the following:

class Car
    attr_reader :speed
    attr_writer :speed
end

which in turn is the same as

class Car
    def speed
        @speed
    end

    def speed=(v)
        @speed = v
    end
end 

Further reading

166 questions
545
votes
5 answers

Why use Ruby's attr_accessor, attr_reader and attr_writer?

Ruby has this handy and convenient way to share instance variables by using keys like attr_accessor :var attr_reader :var attr_writer :var Why would I choose attr_reader or attr_writer if I could simply use attr_accessor? Is there something like…
Saturn
  • 17,888
  • 49
  • 145
  • 271
71
votes
7 answers

attr_accessor default values

I'm using rails and I want to make it so that attr_accessor :politics is set, by default, to false. Does anyone know how to do this and is able to explain it in simple terms for me?
Vasseurth
  • 6,354
  • 12
  • 53
  • 81
33
votes
7 answers

How do I set an attr_accessor for a dynamic instance variable?

I dynamically created an instance variable within my class: class Mine attr_accessor :some_var def intialize @some_var = true end def my_number num self.instance_variable_set "@my_#{num}", num end end How do I make @my_#{num}…
eywu
  • 2,654
  • 1
  • 22
  • 24
25
votes
3 answers

What would a default getter and setter look like in rails?

I know that I can write attr_accessor :tag_list to make a virtual attribute tag_list for an object in Rails. This allows there to be a tag_list attribute in forms for the object. If I use attr_accessor :tag_list I can, in the model, perform actions…
jay
  • 12,066
  • 16
  • 64
  • 103
21
votes
3 answers

Ruby: dynamically generate attribute_accessor

I'm trying to generate the attr_reader from a hash (with nested hash) so that it mirror the instance_variable creation automatically. here is what i have so far: data = {:@datetime => '2011-11-23', :@duration => '90', :@class => {:@price => '£7',…
Yannick Schall
  • 32,601
  • 6
  • 29
  • 42
21
votes
4 answers

Using attr_accessor and attr_accessible on the same field

What happens in the background with the following code? class User < ActiveRecord::Base attr_accessor :name attr_accessible :name end Hint: When instantiating the class, will it be persisted to the database? Why or why not?
Magne
  • 16,401
  • 10
  • 68
  • 88
19
votes
5 answers

Ruby private and public accessors

When defining accessors in Ruby, there can be a tension between brevity (which we all love) and best practice. For example, if I wanted to expose a value on an instance but prohibit any external objects from updating it, I could do the…
A Fader Darkly
  • 3,516
  • 1
  • 22
  • 28
18
votes
2 answers

attr_accessor strongly typed Ruby on Rails

Just wondering if anyone can shed some light on the basics of getter setters in Ruby on Rails with a view on strongly typed. I am very new to ruby on rails and predominately have a good understanding of .NET. For example, let's consider we have a…
ekynox
  • 459
  • 2
  • 7
  • 13
11
votes
1 answer

Ruby attr_accessor vs. getter/setter benchmark: why is accessor faster?

I just tested attr_accessor against equivalent getter/setter-methods: class A # we define two R/W attributes with accessors attr_accessor :acc, :bcc # we define two attributes with getter/setter-functions def dirA=(d); @dirA=d; end def…
rhavin
  • 1,512
  • 1
  • 12
  • 33
10
votes
3 answers

model missing required attr_accessor for 'photo_file_name' when uploading with paperclip and S3 on heroku

Setting up paperclip with S3 in my linux dev environment was a snap -- everything works out of the box. However, I can't get it to work on Heroku. When I try to do an upload, the log shows: Processing ItemsController#create (for 72.177.97.9 at…
eksatx
  • 1,023
  • 2
  • 10
  • 15
10
votes
3 answers

Specify attribute list in attr_accessor with method call

I want to create large number of attributes which can be done with ease if constructed with method call like this, attr_accessor :attr_list def attr_list [:x1, :y1, :x2, :y2] end This is not working. Is there any other way to achieve…
maximus ツ
  • 7,949
  • 3
  • 25
  • 54
10
votes
1 answer

Virtual attributes in rails 4

How can I use virtual attributes(getter, setter) in rails 4, as 'attr_accessible' removed. I am getting issue, here def tags_list @tags = self.tags.collect(&:name).join(', ') end I can reach above method, but not able to reach setter…
7
votes
3 answers

how does the assignment symbol work - Ruby

In Ruby if i just assign a local variable. sound = "bang". is that a main.sound=("bang") method? if so, where and how is that method "sound=" being defined? or how is that assignment working? if not, what is actually happening? i know that for a…
Zach Smith
  • 8,458
  • 13
  • 59
  • 133
7
votes
4 answers

Ruby instance_eval on a class with attr_accessor

I understand the basic difference between instance_eval and class_eval. What I've discovered though when playing around is something strange involving attr_accessor. Here's an example: A = Class.new A.class_eval{ attr_accessor :x } a = A.new a.x =…
brad
  • 31,987
  • 28
  • 102
  • 155
6
votes
5 answers

understand self for attr_accessor class method

class Test class << self attr_accessor :some def set_some puts self.inspect some = 'some_data' end def get_some puts self.inspect some end end end Test.set_some => Test puts Test.get_some.inspect =>…
kriysna
  • 6,118
  • 7
  • 30
  • 30
1
2 3
11 12