To preface, I am a raw Ruby novice, and fairly new to coding as well, so please forgive my ignorance. I'm trying to figure out a hypothetical exercise involving two classes, and passing variables between an instance of one class to the other. I've been searching for answers, but can't seem to find anything that applies directly to this kind of case.
class Person
def initialize(name, age)
#name = name
#age = age
puts "Hi #{#name}, you are #{#age}."
end
end
class Town
def initialize(town_name)
#town_name = town_name
puts "Welcome to #{#town_name}!"
end
def buy_house(person)
puts "#{#name}, at age #{#age}, you bought a house in #{#town_name}!"
end
end
When I instantiate Person and Town, I can see the instance variables being set:
me = Person.new("Daniel", 38)
townville = Town.new("Townville")
My real question is: how do I pass the variables set for the instance of "me" into any methods defined in Town using something like the line below?
townville.buy_house(me)
You can't use puts "#{#name}..." inside an instance of Town to refer to a Person's #name instance variable. #variable always refers to a variable belonging to the current object. If you want to access Person's instance variables, you need to make accessors for them.
You can do so by explicitly defining methods on Person which operate on the instance variables, or using attr_reader/attr_writer/attr_accessor which do so for you:
class Person
attr_reader :name, :age
# the above line is equivalent to defining two methods:
# def name; #name; end
# def age; #age; end
def initialize(name, age)
#name = name
#age = age
puts "Hi #{#name}, you are #{#age}."
end
end
# class Town ...
def buy_house(person)
puts "#{person.name}, at age #{person.age}, you bought a house in #{#town_name}!"
end
If you want to "inject" all the instance variables of one object into another, that is absolutely possible. Whether this is useful or not is up to you to decide:
class Object
def inject_instance_variables(other)
other.instance_variables.each do |var|
self.instance_variable_set(var, other.instance_variable_get(var))
end
end
end
Related
I am using super to pass arguments to the parent initialize method, which is not called by default. This is what it looks like. (Notice the use of super on the last two arguments)
module Pet
def initialize name, is_pet
#is_pet = is_pet
if is_pet
#name = name
else
#name = "Unnamed"
end
end
def pet?
return #is_pet
end
def get_name
return #name
end
end
class Dog
include Pet
def initialize tricks, name, is_pet
#tricks = tricks
super name, is_pet
end
def get_tricks
return #tricks
end
end
Here's what I can do with it:
d = Dog.new ["roll", "speak", "play dead"], "Spots", true
d.pet? #=> true
d.get_tricks #=> ["roll", "speak", "play dead"]
d.get_name #=> "Spots"
It works fine, but I'm just wondering if there's a better way to do this.
It is not a good programming practice to hard code a fixed string like "Unnamed" as the value for #name. In such case, you should assign nil, and do whatever modification to it when you print it. Suppose you do this.
Then is_pet can be deduced from whether name is nil or not, so it is redundant to have that as an instance variable. You can simply apply !! to name in order to get is_pet. Therefore, you should get rid of such instance variable.
You have get_ prefixes as getter methods, but in Ruby, it is a better practice to have the same name as the instance variables (without the atmark) as the getter name.
This will give you:
module Pet
attr_reader :name
def initialize name; #name = name end
end
class Dog
include Pet
attr_reader :tricks
def initialize tricks, name
#tricks = tricks
super(name)
end
end
d = Dog.new ["roll", "speak", "play dead"], "Spots"
d.tricks #=> ["roll", "speak", "play dead"]
d.name #=> "Spots"
!!d.name #=> true (= `is_pet`)
Do not write code that calls super to get into an included module. Don't write modules that will expect children to call super. That's not the point of modules.
It's good object oriented style to not ask about what things are. Look up "tell, don't ask" and duck typing in general.
If you want to provide a default initialize method, you probably want inheritance. But there are occasionally valid use cases for overriding initialize in a module. The idiomatic thing to do here is a hook method:
module Pet
def initialize(options = {})
#name = options[:name]
post_initialize(options)
end
def post_initialize(options = {})
# can be overridden in including modules
end
end
class Dog
include Pet
def post_initialize(options = {})
#tricks = options[:tricks]
end
end
dog = Dog.new(name: "Fido", tricks: ["play dead", "roll over"])
A module is for including some shared behavior among many different things. It's good to consider it like an adjective describing what you might do with a class that includes it. Words that end in "-able" (like Enumerable or Comparable), describing a receiving class, or "-or" (Iterator, Interactor), describing a doing class, are good candidates for being modules.
I know that self.class.name returns name of the class but how about instance?
For example this code
module Selling
def sell
puts "#{self.class.name} has been sold"
end
end
class Shop
include Selling
def initialize(id)
#id=id
end
end
book=Shop.new(1132)
book.sell
prints Shop and what I need is a book
Objects don't have names. They may or may not be referenced by one or more variables, but there is no way to know what variables reference an object and what the names of those variables are.
Modules are a special case, their name method indeed returns the name of the first constant that they have been assigned to, but that is interpreter magic.
Jörg W Mittag already explained that you can't inspect variable names.
Here's an attempt to solve your problem by using a separate Book instance with a name attribute:
module Selling
def sell(item)
puts "#{item.name} has been sold"
end
end
class Shop
include Selling
end
class Book
attr_accessor :name
def initialize(name)
#name = name
end
end
bookstore = Shop.new
book1 = Book.new('Moby-Dick')
book2 = Book.new('Of Mice and Men')
bookstore.sell(book1)
bookstore.sell(book2)
Output:
Moby-Dick has been sold
Of Mice and Men has been sold
Having some trouble when it comes to initializing variables within a class (instance variables etc.) and I was wondering if anybody could clarify the proper syntax for me.
Sample code:
Class Pets
attr_accessor :name
def initialize(name)
#name=name
end
def name=(name)
#name = name
#I believe this is where I change #name instance variable
end
#in this space I could create more <methods> for Class.new.<method>
end
My question is do I need to have attr_accessor as well as def initialize and def name=?
In addition, if I have multiple attr_accessors do I need to add them as arguments to def initialize, e.g.:
Class Pets
attr_accessor :name :age :color
def initialize(name, age, color)
#name = name
#age = age
#color = color
#and if this is the case do I need methods for each (name= age= color= etc.)
end
One last thing:
If someone could confirm or deny my thought process on the name= age= and color= type of methods within the classes. Am I correct in thinking method= is necessary to change the instance variable? I am a bit unsure about what the method= is for and why I cannot change the instance variable within initialize.
attr_accessor :symbol do the same as attr_writer :symbol and attr_reader :symbol, i.e. it creates both reader (def symbol; #symbol; end) and writer (def symbol=(value); #symbol = value; end).
Initialize is a method called every time new instance of the class is being created. It is not the same as new method as some classes may have its own custom factory methods. You don't need to define your initialize method, only problem is that then symbol reader would return nil, as the local variable would not been set.
In ruby everything is a method. In case of objects, object.attr = value is just a short for object.attr=(value) where attr= is just another method. (Similarly << operator is defined as a method on Array class, attr_accessor is a method defined on class "Class").
To piggy back on what what said earlier, recall that if you want your attributes to be accessible outside your class (you want to write over the attribute value or you want to read it) you will need to use the attr_accessor (or attr_writer or attr_reader).
If I had a class like ...
class Calendar
attr_reader :event_name, :all_events
def initialize
#event_name = event_name
#all_events = []
end
def create_event(event_name)
puts "#{event_name} has been added to your calendar."
#all_events << event_name
p #all_events
end
def see_all_events
puts "Here are your events --"
#all_events.each {|event| puts "- #{event}"}
end
end
my_calendar=Calendar.new
my_calendar.create_event("interview")
my_calendar.see_all_events
my_calendar.all_events
I can read all my events either with the method see_all_events or by calling all_events on my class Calendar object. If for some reason I did not want a see_all_events method but instead only wanted it to be seen by calling all_events on my object I can only do this because of attr_reader.
Basically the point here is to remember exactly how you want your users to interact with your object attributes. If it needs to be private and only accessed via methods then you should be weary of using attr_accessor or attr_writer or attr_reader (depending on the situation).
This question already has answers here:
What is attr_accessor in Ruby?
(20 answers)
Closed 8 years ago.
Before I expand my question, let me state that I have read the answers to the questions here, here and here. So requesting you to not mark my question as duplicate as those answers didn't make me understand the purpose of attr_accessor. My question is more to do with the logic and not the syntax.
I've created two sets of code below. The sets are identical to each other except that one set simply doesn't have the attr_accessor line. When I ran both sets, they both gave me the same output. So, logically speaking, what difference does the attr_accessor line make, when both sets of code gave me the same intended output?
Code Set 1:
class Animal
def initialize(name)
#name = name
end
end
class Cat < Animal
def talk
"Meaow!"
end
end
class Dog < Animal
def talk
"Woof!"
end
end
animals = [Cat.new("Flossie"), Dog.new("Clive"), Cat.new("Max")]
animals.each do |animal|
puts animal.talk
end
#Output:
#Meaow!
#Woof!
#Meaow!
Code Set 2:
class Animal
attr_accessor :name #this line is the only difference between the two code sets.
def initialize(name)
#name = name
end
end
class Cat < Animal
def talk
"Meaow!"
end
end
class Dog < Animal
def talk
"Woof!"
end
end
animals = [Cat.new("Flossie"), Dog.new("Clive"), Cat.new("Max")]
animals.each do |animal|
puts animal.talk
end
#Output:
#Meaow!
#Woof!
#Meaow!
Both sets of code call the Animal class to create new instances of animal objects WITH names. I stress on "...WITH names." because the attr_accessor (in the 2nd set) is defining the :name attribute. But in the 1st code set, I have deleted the attr_accessor but still managed to create object instances with the name attribute.
attr_accessor :attribute_name is shorthand for:
def attribute_name
#attribute_name
end
def attribute_name=(value)
#attribute_name = value
end
and it's for setting instance variable. In your code snipped, you set instance variable directly in initialize method, so you don't need attr_accessor.
Instance variables can always be read/written inside instance methods, which your code demonstrates. attr_accessor makes instance variables readable/writable outside the class (by defining accessor methods). By adding it to your second example, you allow the following:
cat = Cat.new("Garfield")
puts cat.name
cat.name = "Maru"
which would raise NoMethodError in your first example.
I see code like:
class Person
def initialize(name)
#name = name
end
end
I understand this allows me to do things like person = Person.new and to use #name elsewhere in my class like other methods. Then, I saw code like:
class Person
attr_accessor :name
end
...
person = Person.new
person.name = "David"
I'm just at a loss with these two methods mesh. What are the particular uses of def initialize(name)? I suppose attr_accessor allows me to read and write. That implies they are two separate methods. Yes? Want clarifications on def initialize and attr_accessor and how they mesh.
initialize and attr_accessor have nothing to do with each other. attr_accessor :name creates a couple of methods:
def name
#name
end
def name=(val)
#name = val
end
If you want to set name upon object creation, you can do it in the initializer:
def initialize(name)
#name = name
# or
# self.name = name
end
But you don't have to do that. You can set name later, after creation.
p = Person.new
p.name = "David"
puts p.name # >> "David"
Here is the answer you are looking for Classes and methods. Read it carefully.
Here is a good documentation from the link:
Classes and methods
Now we are ready to create our very own Address class. Let's start simple. Let's start with an address that only contains the "street" field.
This is how you define a class:
class Address
def initialize(street)
#street = street
end
end
Let's go through this:
The class keyword defines a class.
By defining a method inside this class, we are associating it with this class.
The initialize method is what actually constructs the data structure. Every class must contain an initialize method.
#street is an object variable. Similar to the keys of a hash. The # sign distinguishes #street as an object variable. Every time you create an object of the class Address, this object will contain a #street variable.
Let's use this class to create an address object.
address = Addres.new("23 St George St.")
That's it. address is now an object of the class Address
Reading the data in an object
Suppose that we want to read the data in the address object. To do this, we need to write a method that returns this data:
class Address
def initialize(street)
#street = street
end
# Just return #street
def street
#street
end
end
Now the method Address#street lets you read the street of the address. In irb:
>> address.street
=> "23 St George St."
A property of an object, which is visible outside, is called an attribute. In this case, street is is an attribute. In particular, it is a readable attribute. Because this kind of attribute is very common, Ruby offers you a shortcut through the attr_reader keyword:
class Address
attr_reader :street
def initialize(street)
#street = street
end
end
Changing the data in an object
We can also define a method to change the data in an object.
class Address
attr_reader :street
def initialize(street)
#street = street
end
def street=(street)
#street = street
end
end
Ruby is pretty smart in its use of the street= method:
address.street = "45 Main St."
Notice that you can put spaces betten street and =. Now that we can change the address data, we can simplify the initialize method, and have it simply default the street to the empty string "".
class Address
attr_reader :street
def initialize
#street = ""
end
def street=(street)
#street = street
end
end
address = Address.new
address.street = "23 St George St."
This might not seem like much of a simplification, but when we add the city, state and zip fields, and more methods this will make the class definition a bit simpler.
Now, street is also a writable attribute. As before, you can declare it as such with attr_writer:
class Address
attr_reader :street
attr_writer :street
def initialize
#street = ""
end
end
Accessing data
Very often, you have attributes that are both readable and writable attributes. Ruby lets you lump these together with attr_accessor. I guess these would be called "accessible attributes", but I have never seen them be called that.
class Address
attr_accessor :street
def initialize
#street = ""
end
end
With this knowledge, it is now easy to define the entire addressbook structure. As it turns out, attr_accessor and friends all accept multiple arguments.
class Address
attr_accessor :street, :city, :state, :zip
def initialize
#street = #city = #state = #zip = ""
end
end
I think you consider initialize as a constructor. To be precise, it is not. The default constructor is the new method on the class, and initialize is called by that method. If you do not define initialize, you can still create an object with new because initialize is not the constructor itself. In that case, the default initialize does nothing. If you do define initialize, then that is called right after the object creation.
The statement #foo = ... and attr_accessor :foo are different. The former assigns a value to the instance variable #foo, whereas the latter lets you access #foo via methods foo and foo=. Without the latter, you can still access #foo by directly describing so.
Unlike C++,Java instance variables in Ruby are private by default(partially as they can be accessed by using a.instance_variable_get :#x)
eg:
class Dda
def initialize task
#task = task
#done = false
end
end
item = Dda.new "Jogging" # This would call the initializer and task = Jogging would
be set for item
item.task # would give error as their is no function named task to access the instance
variable.
Although we have set the value to item but we won't be able to do anything with it as instace variables are private in ruby.
code for getter:
def task
#task
end
#for getter
def task=task
#task = task
end
Using getter would ensure that item.task returns it's value
And using setter gives us the flexibility to provide values to instance variables at any time.