attr_accessor default values
Rails has attr_accessor_with_default so you could write
class Like
attr_accessor_with_default :politics,false
end
i = Like.new
i.politics #=> false
and thats all
UPDATE
attr_accessor_with_default has been deprecated in Rails 3.2.. you could do this instead with pure Ruby
class Like
attr_writer :politics
def politics
@politics || false
end
end
i = Like.new
i.politics #=> false
How to do attr_accessor_with_default in ruby?
attr_accessor :pancakes
def after_initialize
return unless new_record?
self.pancakes = 11
end
This ensures that the value is initialized to some default for new record only.
best way to set a default value on a property without ActiveRecord?
class Foo
# class-level instance variable
# setting initial value (optional)
@class_var = 42
# making getter/setter methods on the class itself
class << self
attr_accessor :class_var
end
# instance-level variable getter/setter methods
attr_accessor :inst_var
def initialize
# setting initial value (optional)
@inst_var = 17
end
end
p Foo.class_var
#=> 42
Foo.class_var = 99
p Foo.class_var
#=> 99
f1 = Foo.new
f2 = Foo.new
f2.inst_var = 1
p [f1.inst_var, f2.inst_var]
#=> [17,1]
How to initialize 'attr_accessor' attribute values?
For Rails 3.2 or earlier, you could use attr_accessor_with_default
:
class User < ActiveRecord::Base
attr_accessor_with_default :attribute_name1, true
attr_accessor_with_default :attribute_name2, true
attr_accessor_with_default :attribute_name3, true
...
end
Since your default value is an immutable type (boolean), this form of the method is safe to use here. But don't use it if the default value is a mutable object, including an array or string, because all of your new model objects will share the exact same instance, which is probably not what you want.
Instead, attr_accessor_with_default
will accept a block, where you can return a new instance each time:
attr_accessor_with_default(:attribute_name) { FizzBuzz.new }
Rails - attr_accessor :- read the attribute value instead of the function overriding it
attr_accessor
defines a instance variable and a method to access it (read/write).
So the easy way is to write:
def open_ledger_account
create_ledger_account!(opening_balance: @opening_balance)
end
The read_attribute
would only work if opening_balance
was an attribute in the database on Customer
.
Specify default values for instance variables outside constructor
One thing you can do is call a method from the initialize
of Animal
, providing a hook for subclasses to add custom functionality:
class Animal
attr_reader :age
def initialize(age)
@age = age
setup_defaults
end
private
def setup_defaults
# NOOP by default
end
end
class Sheep < Animal
attr_accessor :likes
private
def setup_defaults
@likes = []
end
end
A second way, that you mention in your post, you can do this is use a custom def likes
instead of the attr_reader
/attr_accessor
:
def likes
@likes ||= [] # shorter way of doing what you have
end
As a third option, if you don't mind using initialize
(your primary concern seems to be possibly changing the superclass' signature), since you don't care about any the parameters to initializeSheep
is you can overwrite the initialize
like:
class Sheep < Animal
attr_accessor :likes
def initialize(*)
super
@likes = []
end
end
this is the same as doing something like def initialize(*args)
except you don't name the variable, and works since super
passes in the original arguments by default. Now, if you go back and change animal to have, say, a name
argument to its initialize
:
class Animal
attr_reader :age, :name
def initialize(name, age)
@name = name
@age = age
end
end
Sheep
still works without any changes.
Regarding attr_accessor and def initialize
attr_accessor is just an attribute and by default it is nil. You cannot assign any value to it. If you need it to an array then initialize it as an array before using it.
What is attr_accessor in Ruby?
Let's say you have a class Person
.
class Person
end
person = Person.new
person.name # => no method error
Obviously we never defined method name
. Let's do that.
class Person
def name
@name # simply returning an instance variable @name
end
end
person = Person.new
person.name # => nil
person.name = "Dennis" # => no method error
Aha, we can read the name, but that doesn't mean we can assign the name. Those are two different methods. The former is called reader and latter is called writer. We didn't create the writer yet so let's do that.
class Person
def name
@name
end
def name=(str)
@name = str
end
end
person = Person.new
person.name = 'Dennis'
person.name # => "Dennis"
Awesome. Now we can write and read instance variable @name
using reader and writer methods. Except, this is done so frequently, why waste time writing these methods every time? We can do it easier.
class Person
attr_reader :name
attr_writer :name
end
Even this can get repetitive. When you want both reader and writer just use accessor!
class Person
attr_accessor :name
end
person = Person.new
person.name = "Dennis"
person.name # => "Dennis"
Works the same way! And guess what: the instance variable @name
in our person object will be set just like when we did it manually, so you can use it in other methods.
class Person
attr_accessor :name
def greeting
"Hello #{@name}"
end
end
person = Person.new
person.name = "Dennis"
person.greeting # => "Hello Dennis"
That's it. In order to understand how attr_reader
, attr_writer
, and attr_accessor
methods actually generate methods for you, read other answers, books, ruby docs.
Related Topics
Get All Instance Variables Declared in Class
In Ruby Why Does Nil.Id Return 4
Why Can't I Use an Integer as a Key Using the New Ruby 1.9.2 Hash Syntax
Unix Commands Work on Server But Not in Ruby Ssh Session
What Is Your Preferred Way to Produce Charts in a Ruby on Rails Web Application
How to Remove the String "\N" from Within a Ruby String
Ruby: What Is the Easiest Method to Update Hash Values
Doing a Http Basic Authentication in Rails
How to Downgrade from Ruby 1.9.2 to Ruby 1.8.7 to Run Rails 2.0.2
Trying to Install Ruby-Filemagic on Snow Leopard Using Brew Rather Than Ports
Override Ruby Constant in Subclass So Inherited Methods Use New Constant Instead of the Old
What Does "<Top (Required)>" Mean in a Ruby Stack Trace
How to Escape #{ from String Interpolation
Ruby on Rails Display Half a Star for a Decimal Rating, E.G. 4.5