How do I stub a Class object in Rspec? - ruby

I am trying hard to enforce encapsulation (but am probably not doing very well), and want to test the code in Rspec. The Customer class will take a class Object (as klass) when it is instantiated in a factory class. Through an as yet non-existent UI, the Customer will create an Order.
My current test is as follows. I just want to confirm that the order is the Order class.
describe 'Customer' do
let(:customer){Customer.new}
let(:customer_with_instantiation){Customer.new(:klass=>order, :name=>'Oscar Wilde', :number=>'0234567')}
let(:order){double :order, :name=>:order}
it 'klass object to be the order class when customer is instantiated with a klass attribute' do
expect(customer_with_instantiation.klass).to be_a(order)
end
end
Class code as follows:
class Customer
attr_accessor :name, :number, :klass
DEFAULT_CUSTOMER_ORDER = {:order_detail => [{ :dish=>"",
:item_count=>0 }],
:order_total_cost=>0 }
def initialize(options={})
#name=options.fetch(:name, "")
#number=options.fetch(:number, "")
#klass=options.fetch(:klass, Object)
#customer_order=DEFAULT_CUSTOMER_ORDER
end
def place_order(menu)
#requires user input
customer_order=klass.new({:order_detail => [{:dish => :pizza, :item_count => 3},
{:dish => :burger, :item_count => 3}],
:order_total_cost => 210})
klass.test_customer_order(customer_order, self)
end
end
class Order
attr_reader :order_detail, :order_total_cost
attr_accessor :total_check
def initialize(options={})
#order_detail=options.fetch(:order_detail, Object)
#order_total_cost=options.fetch(:order_total_cost, Object)
end
def self.test_customer_order(customer_order, customer, menu, assistant)
customer_order.total_check = 0
customer_order.order_detail.each do |order_item|
menu.dishes.each do |dish|
if order_item[:dish]==dish.name
customer_order.total_check += dish.price*order_item[:item_count]
end
end
end
assistant.take_order(customer_order, customer, customer_order.total_check)
end
end
Any help gratefully appreciated!

By using be_a, you're testing that klass is an instance of klass, which is probably not what you want.
It seems to me that when testing the initialize method and the getter for klass (which is what you're doing, in effect), you should only be interested in confirming that whatever you send into Customer.new can be read afterwards.
So maybe something like this:
class Foo
attr_reader :klass
def initialize(args)
#klass = args.fetch(:klass)
end
end
describe Foo do
describe "#initialize" do
let(:klass) { double }
let(:instance) { Foo.new(klass: klass)}
it "sets klass" do
expect(instance.klass).to eq(klass)
end
end
end
Some general points:
If you want to test whether the order is an instance of klass, you should probably rewrite your code to make that easier to test
klass isn't a very useful name in this case. It isn't clear why a Customer would need a klass.
You want to decouple the order from the customer, but the customer is clearly making some assumptions about the interface of the order. Did you really achieve anything?
I'd recommend not putting test methods in the classes themselves, but rather in the test files.
Using Object as a default in fetch is probably not what you want. To begin with, you probably want them to be instances of some class, not class objects.
Is it really the job of an instance of the Customer class to create orders? If the point is to make sure that any kind of abstract order can be instantiated based on user input, maybe a separate OrderCreator class would be more appropriate? This class could accept user data and an order class and the affected customer.

Related

How can I dynamically create class methods in Ruby?

In Rails we can call "Person.find_by_attribute" where 'attribute' is dynamically placed by attributes in the 'Person' migration. I want to replicate this logic n my Person class without rails and call 'find_by_attribute' for the attributes. What I have so far:
class Person
def attributes
[
{ id: 1, nice_attribute: "something" },
{ id: 2, nice_attribute: "another thing" },
]
end
def find_by_id(id)
attributes.select { |d| d[:id] == id }
end
But defining a find_by like this could bloat the class quickly. How can I achieve dynamic def setting?
I'm reasonably sure this is not how Rails does this, but here is some implementation that might assist you:
What you have defined there is a Singleton class and you should be aware of how to dynamically define attribute records in such a class before committing to that design. It might be a good choice, but you should be feeding that class an access point to search through records defined elsewhere. Regardless, the below will get you started:
class Person
##attributes = { id: Integer, nice_attribute: String }
# getter method
def self.attribute_types
##attributes
end
attribute_types.keys.each do |attr|
define_singleton_method :"find_by_#{attr}" do |value|
# logic
end
end
end
Then you will be able to call Person.find_by_id or Person.find_by_nice_attribute
When you implement it may be better for you to have a PersonRecord module that collects records from a database and the Person class might be an instance like so:
class Person
def initialise(opts ={})
define_find_by_methods
end
def attribute_keys
# stub method, but should be collecting this information dynamically
[ :id, :nice_attribute ]
end
def define_find_by_methods
attribute_keys.each do |attr|
# no longer using define_singleton_method here and define_method is a class method you will have to send it to self.class
self.class.send(:define_method, :"find_by_#{attr}") do |val|
# logic
end
end
end
end

Referencing class attributes in mixin

I'm fairly new to Ruby, and I have a code organisation problem.
I have a class, called Movie, which includes a module called IMDBMovieInfo.
class Movie
include IMDBMovieInfo
attr_accessor :name
attr_accessor :year
attr_accessor :movieID
end
IMDBMovieInfo has a method which takes the movieID and uses it to build an IMDB URL:
module IMDBMovieInfo
def imdb_url()
"http://www.imdb.com/title/tt#{self.movieID}/"
end
end
The problem here is I'm not sure I should be referencing something in the movie class, as IMDBMovieInfo doesn't know about that class, and shouldn't. I could add an argument of a movie ID, but then if you don't know about the Movie object, you'd be doing this, which doesn't make sense:
movie = Movie.new("Titanic", "1997", "0120338")
movie.imdb_url(movie.movieID)
What would be the correct way to organise this code?
You would only need to extract this code into a separate module if you were to use it in multiple classes. I can however reenact the wish to split up a large model into several small chunks.
You could for example establish a protocol that the including class must adhere to and leave it up to the including class where the id comes from. In the following example, the including class must implement the method imdb_id which is supposed to return the id that will be used for the url:
module IMDBMovieInfo
def imdb_id
raise NotImplementedError, 'including class needs to override imdb_id'
end
def imdb_url
"http://www.imdb.com/title/tt#{imdb_id}/"
end
end
class Movie
include IMDBMovieInfo
def imdb_id
self.movieID
end
attr_accessor :name
attr_accessor :year
attr_accessor :movieID
end
# In another classs
# (suppose IMDB lists computer games in the future)
class ComputerGame
include IMDBMovieInfo
def imdb_id
self.gameID
end
attr_accessor :name
attr_accessor :year
attr_accessor :gameID
end
I must however say that I find this whole extracting into mixins a bit clumsy. Another way would be to create a utility class that knows how to build an url but not where the id comes from:
class IMDBUtil
def initialize(imdb_id)
#imdb_id = imdb_id
end
def imdb_url
"http://www.imdb.com/title/tt#{#imdb_id}/"
end
end
class Movie
include IMDBMovieInfo
def imdb_url
IMDBUtil.new(self.movieId).imdb_url
end
attr_accessor :name
attr_accessor :year
attr_accessor :movieID
end
To wrap this up, here's a great blog post from CodeClimate on how to refactor fat Rails models.
In respect to your comment I would say, that the IMDBMovieInfo module shouldn't have a function that needs a movieID. You could just move it to your Movie class.

Variable scope definitions and inheritance

I'm working on a extended search feature for my webpage.
I looked at ransack, however it's lacking some functionalities I need, makes the url-query string very long and has some bugs (reported).
Thus I started to implement my own hack.
First I want to present my idea, afterwards I want to ask kindly how to fix my issue and in the end if there are other ways to improve this.
The idea:
A model defines something like this (additionally, the model is inside an engine):
module EngineName
class Post < ActiveRecord::Base
search_for :name, :as => :string do |b, q|
b.where{name =~ "%#{q}%"}
end
end
end
:name is to define the query-param to use e.g. this would be ?q[name]=something
I know that this is not fully generic like ransack, but well...
:as is to build up the correct form-tag. :string would be for text_field, :integer for number_field and so on. I want to extend it further to implement auto-generating of collections for associations etc.
Now the block is a simple scope to use.
I run into several shortcomings with ransack when building up complex queries (like with count() etc.). Now I can specify my own optimized query in squeel.
I extended ActiveRecord::Base to set up the logic (the global one, not inside the engine. I want to use it everywhere).
I defined a scope :search so I can use Model.search(param[q]) like in ransack.
Also I tried to keep a list of keys which are "searchable" defined by the search_for calls.
class ActiveRecord::Base
##searchable_attributes = Hash.new({})
def self.search_for(name, *opts, &search_scope)
return unless search_scope
##searchable_attributes[name] = {
:type => opts[:as],
:condition => search_scope
}
unless ##searchable_attributes.has_key? :nil
##searchable_attributes[:nil] = Proc.new { scoped }
end
end
scope :search, lambda {|q|
next unless q.kind_of?(Hash)
base = ##searchable_attributes[:nil].call
q.each do |key, search|
next unless base.class.searchable_attributes.has_key?(key)
base = ##searchable_attributes[key][:condition].call(base, search)
end
base
}
end
Now the issues:
It has mostly to do with inheritance of the classes. But even after reading and trying 3, 4 it does not worked.
Please take a look at the second line in the scope :search.
There I'm calling the simple Proc I definied above which only includes "scoped"
This is to get arround the issue that self returns "ActiveRecord::Base" and not the model itself like "Post" or "Comment".
It's because the scope is called on the Base class on inheritance, however I did not find anything to fix this.
As search_for is called on the model itself (e.g. Post) the scope-model returned there is "the right one".
Does anyone know how to circumvent this?
The next question would be, how to store the list of "searchable" scopes. I used ##variables. But as they are shared within every subclass, this would be a no-go.
However, it needs to be static as the search_for is called without initialize a instance (isn't it?)
Last but not least, it is somekind horrible to always specify the base-model to use on every scope so that I can chain them together.
Is there any other possibilities to improve this?
Ok, it seems I got it finally myself my putting several other answers from other questions together.
Model:
module EngineName
class Post < ActiveRecord::Base
searchable
search_for :name, :as => :string do |b, q|
b.where{name =~ "%#{q}%"}
end
end
end
My "Plugin" currently as an initializer:
class ActiveRecord::Base
def self.searchable
include Searchable
end
end
module Searchable
def self.included(base)
base.class_eval {
##searchable_attributes = Hash.new({})
def self.search_for(name, opts)
return unless block_given?
##searchable_attributes[name] = {
:type => opts[:as],
:condition => Proc.new
}
end
# Named scopes
scope :search, lambda {|q|
next unless q.kind_of?(Hash)
base = self.scoped
q.each do |key, search|
key = key.to_sym
next unless ##searchable_attributes.has_key?(key)
base = ##searchable_attributes[key][:condition].call(base, search)
end
base
}
}
end
end
Hope it'll help some others working on the same problem.
Rails provides a helper for class_attribute. This provides inheritable class attributes, but allows subclassess to "change their own value and it will not impact parent class". However a hash which is mutated using []= for example would effect the parent, so you can ensure that a new copy is made when subclassing using rubys inherited method
Therefore you could declare and initialise on the base class like so:
module Searchable
extend ActiveSupport::Concern
included do
class_attribute :searchable_attributes
end
module ClassMethods
def inherited(subclass)
subclass.searchable_attributes = Hash.new({})
end
def search_for(name,opts)
return unless block_given?
searchable_attributes[name] = {
:type => opts[:as],
:condition => Proc.new
}
end
end
end
Note that I used ActiveSupport::Concern to gain the neater syntax for defining stuff directly on the class and also mixing in class methods. Then you can simply add this to active record base:
ActiveRecord::Base.send(:include, Searchable)
now any classes get their own attributes hash:
class Post < ActiveRecord::Base
search_for :name, :as => :string do |b, q|
b.where{name =~ "%#{q}%"}
end
end

How can I create fake has_many relationships, and have each one include class methods as instance methods?

I have a class, with some fake relationships I want to implement:
module FormStack
class Connection
def forms; end
def fields; end
end
end
I have metaprogramically generated classes for both forms, and fields (as they are RESTful resources, they share the same action names and params), and I want to include those methods in my fake relationships in my FormStack::Connection class. can this be done?
I essentially want <FromStack::Connection Instance>.forms to behave as if it were FormStack::Form, so I can do things like <connection>.forms.all or <connection>.forms.find(id).
Is this possible?
Any best practices I should maybe be looking at? (This seems a little strange to me, but I think it's an elegant way to have the methods implemented in a useful way, while still having an ActiveRecord-esque abstraction of the restful resources / objects).
Here is the code I'm working with, if you want to look: https://github.com/TinderBox/formstack/tree/connection_instances
Why not just use simple composition? Pass whatever object has the has_many FormStack::Form relation in when you initialize a new FormStack::Connection instance. Then you can directly invoke the #forms method on the FormStack::Form collection instance, or you can use delegation.
FormStack::Connection.new(FormStack::FormCollection.new(params[:form]) #sample class name -- obviously use whatever has the real has_many :forms
module FormStack
class Connection
def initialize(form_collection)
#form_collection = form_collection
end
def forms
#form_collection.forms
end
def fields
#form_collection.fields
end
end
end
Or
module FormStack
class Connection
extend Forwardable
def_delegators :#form_collection, :forms, :fields
def initialize(form_collection)
#form_collection = form_collection
end
end
end
Unless there is a better way, this is how I've solved my problem for now:
def method_missing(meth, *args, &block)
method_name = meth.to_s
if "forms" == method_name
FormStack::Form.connection = self
FormStack::Form
elsif ...
else
super
end
end
https://github.com/TinderBox/formstack/blob/082793bed97e97cc65c703c8ca3cb382cbdf743a/lib/formstack/connection.rb

What is attr_accessor in Ruby?

I am having a hard time understanding attr_accessor in Ruby.
Can someone explain this to me?
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.
attr_accessor is just a method. (The link should provide more insight with how it works - look at the pairs of methods generated, and a tutorial should show you how to use it.)
The trick is that class is not a definition in Ruby (it is "just a definition" in languages like C++ and Java), but it is an expression that evaluates. It is during this evaluation when the attr_accessor method is invoked which in turn modifies the current class - remember the implicit receiver: self.attr_accessor, where self is the "open" class object at this point.
The need for attr_accessor and friends, is, well:
Ruby, like Smalltalk, does not allow instance variables to be accessed outside of methods1 for that object. That is, instance variables cannot be accessed in the x.y form as is common in say, Java or even Python. In Ruby y is always taken as a message to send (or "method to call"). Thus the attr_* methods create wrappers which proxy the instance #variable access through dynamically created methods.
Boilerplate sucks
Hope this clarifies some of the little details. Happy coding.
1 This isn't strictly true and there are some "techniques" around this, but there is no syntax support for "public instance variable" access.
attr_accessor is (as #pst stated) just a method. What it does is create more methods for you.
So this code here:
class Foo
attr_accessor :bar
end
is equivalent to this code:
class Foo
def bar
#bar
end
def bar=( new_value )
#bar = new_value
end
end
You can write this sort of method yourself in Ruby:
class Module
def var( method_name )
inst_variable_name = "##{method_name}".to_sym
define_method method_name do
instance_variable_get inst_variable_name
end
define_method "#{method_name}=" do |new_value|
instance_variable_set inst_variable_name, new_value
end
end
end
class Foo
var :bar
end
f = Foo.new
p f.bar #=> nil
f.bar = 42
p f.bar #=> 42
attr_accessor is very simple:
attr_accessor :foo
is a shortcut for:
def foo=(val)
#foo = val
end
def foo
#foo
end
it is nothing more than a getter/setter for an object
Basically they fake publicly accessible data attributes, which Ruby doesn't have.
It is just a method that defines getter and setter methods for instance variables. An example implementation would be:
def self.attr_accessor(*names)
names.each do |name|
define_method(name) {instance_variable_get("##{name}")} # This is the getter
define_method("#{name}=") {|arg| instance_variable_set("##{name}", arg)} # This is the setter
end
end
If you are familiar with OOP concept, You must familiar with getter and setter method.
attr_accessor does the same in Ruby.
Getter and Setter in General Way
class Person
def name
#name
end
def name=(str)
#name = str
end
end
person = Person.new
person.name = 'Eshaan'
person.name # => "Eshaan"
Setter Method
def name=(val)
#name = val
end
Getter method
def name
#name
end
Getter and Setter method in Ruby
class Person
attr_accessor :name
end
person = Person.new
person.name = "Eshaan"
person.name # => "Eshaan"
Simple Explanation Without Any Code
Most of the above answers use code. This explanation attempts to answer it without using any, via an analogy/story:
Outside parties cannot access internal CIA secrets
Let's imagine a really secret place: the CIA. Nobody knows what's happening in the CIA apart from the people inside the CIA. In other words, external people cannot access any information in the CIA. But because it's no good having an organisation that is completely secret, certain information is made available to the outside world - only things that the CIA wants everyone to know about of course: e.g. the Director of the CIA, how environmentally friendly this department is compared to all other government departments etc. Other information: e.g. who are its covert operatives in Iraq or Afghanistan - these types of things will probably remain a secret for the next 150 years.
If you're outside the CIA you can only access the information that it has made available to the public. Or to use CIA parlance you can only access information that is "cleared".
The information that the CIA wants to make available to the general public outside the CIA are called: attributes.
The meaning of read and write attributes:
In the case of the CIA, most attributes are "read only". This means if you are a party external to the CIA, you can ask: "who is the director of the CIA?" and you will get a straight answer. But what you cannot do with "read only" attributes is to make changes changes in the CIA. e.g. you cannot make a phone call and suddenly decide that you want Kim Kardashian to be the Director, or that you want Paris Hilton to be the Commander in Chief.
If the attributes gave you "write" access, then you could make changes if you want to, even if you were outside. Otherwise, the only thing you can do is read.
In other words accessors allow you to make inquiries, or to make changes, to organisations that otherwise do not let external people in, depending on whether the accessors are read or write accessors.
Objects inside a class can easily access each other
On the other hand, if you were already inside the CIA, then you could easily call up your CIA operative in Kabul because this information is easily accessible given you are already inside. But if you're outside the CIA, you simply will not be given access: you will not be able to know who they are (read access), and you will not be able to change their mission (write access).
Exact same thing with classes and your ability to access variables, properties and methods within them. HTH! Any questions, please ask and I hope i can clarify.
I faced this problem as well and wrote a somewhat lengthy answer to this question. There are some great answers on this already, but anyone looking for more clarification, I hope my answer can help
Initialize Method
Initialize allows you to set data to an instance of an object upon creation of the instance rather than having to set them on a separate line in your code each time you create a new instance of the class.
class Person
def initialize(name)
#name = name
end
def greeting
"Hello #{#name}"
end
end
person = Person.new("Denis")
puts person.greeting
In the code above we are setting the name “Denis” using the initialize method by passing Dennis through the parameter in Initialize. If we wanted to set the name without the initialize method we could do so like this:
class Person
attr_accessor :name
# def initialize(name)
# #name = name
# end
def greeting
"Hello #{name}"
end
end
person = Person.new
person.name = "Dennis"
puts person.greeting
In the code above, we set the name by calling on the attr_accessor setter method using person.name, rather than setting the values upon initialization of the object.
Both “methods” of doing this work, but initialize saves us time and lines of code.
This is the only job of initialize. You cannot call on initialize as a method. To actually get the values of an instance object you need to use getters and setters (attr_reader (get), attr_writer(set), and attr_accessor(both)). See below for more detail on those.
Getters, Setters (attr_reader, attr_writer, attr_accessor)
Getters, attr_reader: The entire purpose of a getter is to return the value of a particular instance variable. Visit the sample code below for a breakdown on this.
class Item
def initialize(item_name, quantity)
#item_name = item_name
#quantity = quantity
end
def item_name
#item_name
end
def quantity
#quantity
end
end
example = Item.new("TV",2)
puts example.item_name
puts example.quantity
In the code above you are calling the methods “item_name” and “quantity” on the instance of Item “example”. The “puts example.item_name” and “example.quantity” will return (or “get”) the value for the parameters that were passed into the “example” and display them to the screen.
Luckily in Ruby there is an inherent method that allows us to write this code more succinctly; the attr_reader method. See the code below;
class Item
attr_reader :item_name, :quantity
def initialize(item_name, quantity)
#item_name = item_name
#quantity = quantity
end
end
item = Item.new("TV",2)
puts item.item_name
puts item.quantity
This syntax works exactly the same way, only it saves us six lines of code. Imagine if you had 5 more state attributable to the Item class? The code would get long quickly.
Setters, attr_writer: What crossed me up at first with setter methods is that in my eyes it seemed to perform an identical function to the initialize method. Below I explain the difference based on my understanding;
As stated before, the initialize method allows you to set the values for an instance of an object upon object creation.
But what if you wanted to set the values later, after the instance was created, or change them after they have been initialized? This would be a scenario where you would use a setter method. THAT IS THE DIFFERENCE. You don’t have to “set” a particular state when you are using the attr_writer method initially.
The code below is an example of using a setter method to declare the value item_name for this instance of the Item class. Notice that we continue to use the getter method attr_reader so that we can get the values and print them to the screen, just in case you want to test the code on your own.
class Item
attr_reader :item_name
def item_name=(str)
#item_name = (str)
end
end
The code below is an example of using attr_writer to once again shorten our code and save us time.
class Item
attr_reader :item_name
attr_writer :item_name
end
item = Item.new
puts item.item_name = "TV"
The code below is a reiteration of the initialize example above of where we are using initialize to set the objects value of item_name upon creation.
class Item
attr_reader :item_name
def initialize(item_name)
#item_name = item_name
end
end
item = Item.new("TV")
puts item.item_name
attr_accessor: Performs the functions of both attr_reader and attr_writer, saving you one more line of code.
I think part of what confuses new Rubyists/programmers (like myself) is:
"Why can't I just tell the instance it has any given attribute (e.g., name) and give that attribute a value all in one swoop?"
A little more generalized, but this is how it clicked for me:
Given:
class Person
end
We haven't defined Person as something that can have a name or any other attributes for that matter.
So if we then:
baby = Person.new
...and try to give them a name...
baby.name = "Ruth"
We get an error because, in Rubyland, a Person class of object is not something that is associated with or capable of having a "name" ... yet!
BUT we can use any of the given methods (see previous answers) as a way to say, "An instance of a Person class (baby) can now have an attribute called 'name', therefore we not only have a syntactical way of getting and setting that name, but it makes sense for us to do so."
Again, hitting this question from a slightly different and more general angle, but I hope this helps the next instance of class Person who finds their way to this thread.
Simply put it will define a setter and getter for the class.
Note that
attr_reader :v is equivalant to
def v
#v
end
attr_writer :v is equivalant to
def v=(value)
#v=value
end
So
attr_accessor :v which means
attr_reader :v; attr_writer :v
are equivalant to define a setter and getter for the class.
Simply attr-accessor creates the getter and setter methods for the specified attributes
Another way to understand it is to figure out what error code it eliminates by having attr_accessor.
Example:
class BankAccount
def initialize( account_owner )
#owner = account_owner
#balance = 0
end
def deposit( amount )
#balance = #balance + amount
end
def withdraw( amount )
#balance = #balance - amount
end
end
The following methods are available:
$ bankie = BankAccout.new("Iggy")
$ bankie
$ bankie.deposit(100)
$ bankie.withdraw(5)
The following methods throws error:
$ bankie.owner #undefined method `owner'...
$ bankie.balance #undefined method `balance'...
owner and balance are not, technically, a method, but an attribute. BankAccount class does not have def owner and def balance. If it does, then you can use the two commands below. But those two methods aren't there. However, you can access attributes as if you'd access a method via attr_accessor!! Hence the word attr_accessor. Attribute. Accessor. It accesses attributes like you would access a method.
Adding attr_accessor :balance, :owner allows you to read and write balance and owner "method". Now you can use the last 2 methods.
$ bankie.balance
$ bankie.owner
Despite the large number of existing answers, none of them seems to me to explain the actual mechanism involved here. It's metaprogramming; it takes advantage of the following two facts:
You can modify a module / class on the fly
A module / class declaration is itself executable code
Okay, so imagine the following:
class Nameable
def self.named(whatvalue)
define_method :name do whatvalue end
end
end
We are declaring a class method named which, when called with a value, creates an instance method called name which returns that value. That is the metaprogramming part.
Now we'll subclass that class:
class Dog < Nameable
named "Fido"
end
What on earth did we just do? Well, in the class declaration, executable code executes with reference to the class. So the bare word named is actually a call to the class method named, which we inherited from Nameable; and we are passing the string "Fido" as the argument.
And what does the class method named do? It creates an instance method called name, which returns that value. So now, behind the scenes, Dog has a method that looks like this:
def name
"Fido"
end
Don't believe me? Then watch this little move:
puts Dog.new.name #=> Fido
Why did I tell you all that? Because what I just did with named for Nameable is almost exactly what attr_accessor does for Module. When you say attr_accessor you are calling a class method (inherited from Module) that creates instance methods. In particular, it creates a getter and setter method for the instance property whose name you provide as argument, so that you don't have to write those getter and setter methods yourself.
Defines a named attribute for this module, where the name is symbol.id2name, creating an instance variable (#name) and a corresponding access method to read it. Also creates a method called name= to set the attribute.
module Mod
attr_accessor(:one, :two)
end
Mod.instance_methods.sort #=> [:one, :one=, :two, :two=]
To summarize an attribute accessor aka attr_accessor gives you two free methods.
Like in Java they get called getters and setters.
Many answers have shown good examples so I'm just going to be brief.
#the_attribute
and
#the_attribute=
In the old ruby docs a hash tag # means a method.
It could also include a class name prefix...
MyClass#my_method
I am new to ruby and had to just deal with understanding the following weirdness. Might help out someone else in the future. In the end it is as was mentioned above, where 2 functions (def myvar, def myvar=) both get implicitly for accessing #myvar, but these methods can be overridden by local declarations.
class Foo
attr_accessor 'myvar'
def initialize
#myvar = "A"
myvar = "B"
puts #myvar # A
puts myvar # B - myvar declared above overrides myvar method
end
def test
puts #myvar # A
puts myvar # A - coming from myvar accessor
myvar = "C" # local myvar overrides accessor
puts #myvar # A
puts myvar # C
send "myvar=", "E" # not running "myvar =", but instead calls setter for #myvar
puts #myvar # E
puts myvar # C
end
end
Attributes and accessor methods
Attributes are class components that can be accessed from outside the object. They are known as properties in many other programming languages. Their values are accessible by using the "dot notation", as in object_name.attribute_name. Unlike Python and a few other languages, Ruby does not allow instance variables to be accessed directly from outside the object.
class Car
def initialize
#wheels = 4 # This is an instance variable
end
end
c = Car.new
c.wheels # Output: NoMethodError: undefined method `wheels' for #<Car:0x00000000d43500>
In the above example, c is an instance (object) of the Car class. We tried unsuccessfully to read the value of the wheels instance variable from outside the object. What happened is that Ruby attempted to call a method named wheels within the c object, but no such method was defined. In short, object_name.attribute_name tries to call a method named attribute_name within the object. To access the value of the wheels variable from the outside, we need to implement an instance method by that name, which will return the value of that variable when called. That's called an accessor method. In the general programming context, the usual way to access an instance variable from outside the object is to implement accessor methods, also known as getter and setter methods. A getter allows the value of a variable defined within a class to be read from the outside and a setter allows it to be written from the outside.
In the following example, we have added getter and setter methods to the Car class to access the wheels variable from outside the object. This is not the "Ruby way" of defining getters and setters; it serves only to illustrate what getter and setter methods do.
class Car
def wheels # getter method
#wheels
end
def wheels=(val) # setter method
#wheels = val
end
end
f = Car.new
f.wheels = 4 # The setter method was invoked
f.wheels # The getter method was invoked
# Output: => 4
The above example works and similar code is commonly used to create getter and setter methods in other languages. However, Ruby provides a simpler way to do this: three built-in methods called attr_reader, attr_writer and attr_acessor. The attr_reader method makes an instance variable readable from the outside, attr_writer makes it writeable, and attr_acessor makes it readable and writeable.
The above example can be rewritten like this.
class Car
attr_accessor :wheels
end
f = Car.new
f.wheels = 4
f.wheels # Output: => 4
In the above example, the wheels attribute will be readable and writable from outside the object. If instead of attr_accessor, we used attr_reader, it would be read-only. If we used attr_writer, it would be write-only. Those three methods are not getters and setters in themselves but, when called, they create getter and setter methods for us. They are methods that dynamically (programmatically) generate other methods; that's called metaprogramming.
The first (longer) example, which does not employ Ruby's built-in methods, should only be used when additional code is required in the getter and setter methods. For instance, a setter method may need to validate data or do some calculation before assigning a value to an instance variable.
It is possible to access (read and write) instance variables from outside the object, by using the instance_variable_get and instance_variable_set built-in methods. However, this is rarely justifiable and usually a bad idea, as bypassing encapsulation tends to wreak all sorts of havoc.
Hmmm. Lots of good answers. Here is my few cents on it.
attr_accessor is a simple method that helps us in cleaning(DRY-ing) up the repeating getter and setter methods.
So that we can focus more on writing business logic and not worry about the setters and getters.
The main functionality of attr_accessor over the other ones is the capability of accessing data from other files.
So you usually would have attr_reader or attr_writer but the good news is that Ruby lets you combine these two together with attr_accessor. I think of it as my to go method because it is more well rounded or versatile.
Also, peep in mind that in Rails, this is eliminated because it does it for you in the back end. So in other words: you are better off using attr_acessor over the other two because you don't have to worry about being to specific, the accessor covers it all. I know this is more of a general explanation but it helped me as a beginner.
Hope this helped!

Resources