I'm kind of stumped at the workings of a Ruby class. I understand everything that is going on inside of the class here, but it's the variable assignment and calling of the class that confuses me.
class BankAccount
def initialize(name)
#transactions = []
#balance = 0 #
end
def deposit
print "How much would you like to deposit?"
amount = gets.chomp
#balance += amount.to_f
puts "$#{amount} deposited."
end
def show_balance
puts "Your balance is #{#balance}."
end
end
bank_account = BankAccount.new("James Dean")
bank_account.class
bank_account.deposit
bank_account.show_balance
When we did...
bank_account = BankAccount.new("James Dean")
... it seems as though whenever I call bank_account, it will use the .new method (which I am lead to believe activates the initialize method) to create a new account with the given name.
Then, from what I understand when we call .class on bank_account, it just outputs the class type.
But, next we call bank_account.deposit. Although, I'm under the impression that whenever we call bank_account now, it will create a new account using the BankAccount class... So by doing bank_account.deposit, what I envision it is actually doing is:
BankAccount.new("James Dean").deposit
So if I make two deposits (or even use bank_account.show_balance), then from my understanding, it will be creating a new account with the name James Dean over and over and over, every time I use the bank_account variable. (Because bank_account = BankAccount.new)
Can anyone help me clear this up and understand it a bit better?
Also, just on a side note, could we ever use:
BankAccount.new(gets.chomp)
so that we could create new account with names according to user input?
(Thanks everyone. This is my third question and my former two have apparently been posted in the wrong places. -_- Hoping I got this one right.)
Although, I'm under the impression that whenever we call bank_account now, it will create a new account using the BankAccount class.
That's not right. Your code only calls BankAccount.new once. The result is assigned to bank_account. Each time you access bank_account, you're accessing the constructed object created on the first line.
When you write x = Y.new, you're not assigning the expression Y.new to x, you're evaluating the expression Y.new and assigning the result to x.
Similarly, if you write x = my_function(), the function is evaluated and the result is assigned to x; any references to x from this point are completely decoupled from the function.
Related
Very first post, and very newbie question.
I'm learning Ruby and trying to create a small CYOA game for training.
I want the game to have a number of lives, and each time something happens, that number of lives is changed. But I don't understand how to change the value of a variable with a method.
Here's what I did:
lives = 3
heart = "❤"
total_life = heart * lives
def add_life
return lives + 1
end
add_life
puts "#{total_life}"
The error I get :
1: from ex36.rb:9:in <main>' ex36.rb:6:in add_life': undefined local
variable or method `lives' for main:Object (NameError)
I think my main mistake is that methods create new scopes. But then I don't understand what's the best way to achieve what I want to do. Can you guys point me in the right direction?
Thanks!
Another option is to make a scope that makes sense. You are modeling a player (or possibly a game) - given that Ruby is strongly object-oriented, encapsulating it into a class (with lives as its instance variable) is a natural thing to do.
class Player
HEART = "❤"
def initialize
#lives = 3
end
def life_display
HEART * #lives
end
def add_life
#lives += 1
end
end
player = Player.new
player.add_life
puts player.life_display
In order to get it work you would need to do the following.
lives = 3
HEART = "❤"
def add_life(lives)
return lives + 1
end
def total_lives(lives)
HEART * lives
end
lives = add_life(lives)
puts "#{total_lives(lives)}"
Scope defines where in a program a variable is accessible.
It is all about scopes, local variables are not seen in functions. They have shorter vision of variables only defined in their scope.
In order to access your lives variable you need to pass it to function. I did refactored heart -> HEART making it a constant, constants have a different scope, more like global variables $global_variable, but cannot be reassigned (ruby will nicely say you that it is incorrect)
for further reading I will leave this article
I see that with this code:
# filename: play.rb
class A
attr_reader :a
def initialize(num)
#a=num # I have to use # here
end
def m1
p "---"
p #a
p a
end
end
obj = A.new(1)
obj.m1
I get the output
$ ruby play.rb
"---"
1
1
As you see I can refer to a as either #a or a in the m1 method and both work.
Which should I use when and why?
In this case you don't use a local variable a but a getter method that gives you #a because that's have attr_reader :a. It generates a method #a() used as an getter.
What you really do is:
def m1
p "---"
p #a
p a()
end
If I have the accessor, I use it, not the instance variable. It lets me change the getter later.
I find this an interesting question, one that I had not given much thought to before today. I consulted the Ruby Style Guide, expecting it would provide some good advice, but it is curiously mute on the subject.
I make four suggestions below. These concern setters as well as getters. Others may disagree and have good reasons for doing so. Let's discuss! I look forward to reading comments.
In reading my remarks it may be helpful to put yourself in the place of someone reading code they wrote some time ago or someone reading someone else's code for the first time.
1. Getters used exclusively within a class should be private
I expect this is my least debatable suggestion, as I can see only disadvantages to making any method public when there is no reason to do so.
2. Methods named after instance variables should not have side effects
By this I mean if there is an instance variable #quantity, a method named :quantity should do no more than return the value of #quantity (i.e., be a getter) and a method named :quantity= should do no more than assign a value to #quantity (i.e., be a setter). That is, such methods should not have side effects.
Suppose, for example, a ("pseudo-") setter were defined to assign a value to an instance variable after accounting for a 10% spoilage factor:
def quantity=(q)
0.9 * q
end
If the reader of the code were to miss this definition, but knew there was an instance variable #quantity, the natural assumption would be that :quantity= was a setter, which might mask errors or waste time in testing/debugging. Better, I think, would be to write:
class C
attr_writer :quantity
def initialize
end
#...
def adj_quantity_for_spoilage
self.quantity *= 0.9
end
#...
end
c = C.new
c.quantity = 100
c.adj_quantity_for_spoilage
3. Do not use setters within a class
For example, I suggest writing:
class C
attr_accessor :quantity
def change(x)
#quantity = x
end
end
rather than:
class C
attr_accessor :quantity
def change(x)
self.quantity = x
end
end
The main reason is that it is all-to-easy to inadvertently omit self. in self.quantity = x, in which case x would be incorrectly and silently assigned to a newly-created local variable quantity.
There is a secondary reason in the case where there is no need to access the setter from outside that class. As with getters, we would want setters to be private in this situation, but that is not possible since they must have the explicit receiver self. Recall that private methods, by definition, do not have explicit receivers.
Lastly, I see no argument for using self.quantity over #quantity, particularly in view of the fact that the former requires the keying of four additional characters.1
4. Do not use getters within a class
I expect this to be my most controversial suggestion and I will be the first to admit that if one rejects it the earth will no doubt continue to orbit the sun. Moreover, I concede that using getters in this way does save the typing of one character, the hard-to-reach-without-looking "#".
When I read:
x = m(#quantity)
I know immediately that #quantity is an instance variable, regardless of my familiarity with the code. If I've forgotten (or never knew) what the variable contains or how it is used, the path to my elucidation is clear. True, if I know there is an instance variable #quantity, the above has no advantage over:
x = m(quantity)
If, however, I don't know if there is an instance variable #quantity, I would be faced with three possibilities: quantity is a local variable, a getter or a method that is not a getter. The time required to complete my investigation should not take much longer than if I were tracking down #quantity, but those seconds do add up.
Let's consider another thing: what are the consequences of misspelling the name of an instance variable versus misspelling its getter (something I do frequently):
x = m(#qauntity)
versus:
x = m(qauntity)
#qauntity will return nil, which may lead to an exception being raised, but possibly not soon or not at all.
qauntity will almost certainly raise an "no method or variable" exception immediately, giving it the edge in this situation.
In sum, I am suggesting that it generally is best to use getters and setters outside of class definitions only, and that they have no side effects.
Your thoughts?
1. Over a lifetime of coding, typing those four extra characters could amount to hours of time wasted that could otherwise be used productively (e.g., playing pong).
I have recently learned how to create classes, although I am not ENTIRELY sure where and why I should use them.
I'd use them to create objects, which have similar methods/properties.
I tried making a gag code, but I stumbled upon a question I can't find an answer to.
class Person
def initialize(name,health)
#name = name
#hp = health
end
def kick
#hp -= 1
if (#hp <= 0)
puts "#{#name} got REKT!"
end
end
end
#Friends
michael = Person.new("Michael", 10)
10.times { michael.kick }
Even though this code works, I'm wondering if it is possible to use/call mihchael's hp outside the class? Perhaps sort of like a hash? michael[#hp]? But this doesn't work, even if i set hp to be a global variable.
Should all if/else statements who check object's properties be inside the class?
Thank you very much
The standard way to do this in Ruby is to create an accessor:
class Person
attr_reader :name
attr_reader :hp
end
Then outside the class you can call things like:
puts "#{michael.name} has only #{michael.hp} HP left"
The reason you create objects is to organize your code and data into logical contexts and containers.
I am going through a ruby tutorial and trying to use understand how Virtual Attributes. This is the example shown in the tutorial.
class Spaceship
def destination
#autopilot.destination
end
def destination=(new_destination)
#autopilot.destination = new_destination
end
end
ship = Spaceship.new
ship.destination = "Earth"
puts ship.destination
As per tutorial, this code should ideally return Earth, but I am encountering the below error.
class.rb:7:in `destination=': undefined method `destination=' for nil:NilClass (NoMethodError) from class.rb:12:in `<main>'
I am sorry but unable to identify the missing part.
You need to assign your #autopilot variable something.
Something like this should work:
class Spaceship
def initialize
#autopilot = Struct.new(:destination).new(nil)
end
def destination
#autopilot.destination
end
def destination=(new_destination)
#autopilot.destination = new_destination
end
end
But if you want to add a virtual attribute, then keep the value as a simple instance variable, like so:
class Spaceship
def destination
#destination
end
def destination=(new_destination)
#destination = new_destination
end
end
As humza has pointed out, the code as written will not work.
I suspect the author meant to write something like the following, and wants to make the point that although destination looks like an attribute (and we may send the message destination to an object and get the expected response), there is no corresponding instance variable #destination. We may think of destination as being a virtual attribute.
class Spaceship
def destination
dosomething
end
def destination=(new_destination)
#autopilot = new_destination
end
def dosomething
#autopilot
end
end
ship = Spaceship.new
ship.destination ="Earth"
puts ship.destination
Objects may behave as if the class Spaceship was written as shown in the next example, that is the interface of both classes is the same (and in this case we do have an instance variable #destination).
class Spaceship
def destination
#destination
end
def destination=(new_destination)
#destination = new_destination
end
end
ship = Spaceship.new
ship.destination ="Earth"
puts ship.destination
A message that is send to an object of the Class Spaceship does not need to know (and does not know) about the internal implementation.
Virtual Attributes are treated well here, and a better example is given, where a method durationInMinutes is defined without any corresponding instance variable #durationInMinutes. The explanation given is very concise, and I'll quote it in full:
Here we've used attribute methods to create a virtual instance variable. To the outside world, durationInMinutes seems to be an attribute like any other. Internally, though, there is no corresponding instance variable.
The author(s) continue:
This is more than a curiosity. In his landmark book Object-Oriented Software Construction , Bertrand Meyer calls this the Uniform Access Principle. By hiding the difference between instance variables and calculated values, you are shielding the rest of the world from the implementation of your class. You're free to change how things work in the future without impacting the millions of lines of code that use your class. This is a big win.
I'm thinking in:
class X
def new()
#a = 1
end
def m( other )
#a == other.#a
end
end
x = X.new()
y = X.new()
x.m( y )
But it doesn't works.
The error message is:
syntax error, unexpected tIVAR
How can I compare two private attributes from the same class then?
There have already been several good answers to your immediate problem, but I have noticed some other pieces of your code that warrant a comment. (Most of them trivial, though.)
Here's four trivial ones, all of them related to coding style:
Indentation: you are mixing 4 spaces for indentation and 5 spaces. It is generally better to stick to just one style of indentation, and in Ruby that is generally 2 spaces.
If a method doesn't take any parameters, it is customary to leave off the parantheses in the method definition.
Likewise, if you send a message without arguments, the parantheses are left off.
No whitespace after an opening paranthesis and before a closing one, except in blocks.
Anyway, that's just the small stuff. The big stuff is this:
def new
#a = 1
end
This does not do what you think it does! This defines an instance method called X#new and not a class method called X.new!
What you are calling here:
x = X.new
is a class method called new, which you have inherited from the Class class. So, you never call your new method, which means #a = 1 never gets executed, which means #a is always undefined, which means it will always evaluate to nil which means the #a of self and the #a of other will always be the same which means m will always be true!
What you probably want to do is provide a constructor, except Ruby doesn't have constructors. Ruby only uses factory methods.
The method you really wanted to override is the instance method initialize. Now you are probably asking yourself: "why do I have to override an instance method called initialize when I'm actually calling a class method called new?"
Well, object construction in Ruby works like this: object construction is split into two phases, allocation and initialization. Allocation is done by a public class method called allocate, which is defined as an instance method of class Class and is generally never overriden. It just allocates the memory space for the object and sets up a few pointers, however, the object is not really usable at this point.
That's where the initializer comes in: it is an instance method called initialize, which sets up the object's internal state and brings it into a consistent, fully defined state which can be used by other objects.
So, in order to fully create a new object, what you need to do is this:
x = X.allocate
x.initialize
[Note: Objective-C programmers may recognize this.]
However, because it is too easy to forget to call initialize and as a general rule an object should be fully valid after construction, there is a convenience factory method called Class#new, which does all that work for you and looks something like this:
class Class
def new(*args, &block)
obj = alloc
obj.initialize(*args, &block)
return obj
end
end
[Note: actually, initialize is private, so reflection has to be used to circumvent the access restrictions like this: obj.send(:initialize, *args, &block)]
Lastly, let me explain what's going wrong in your m method. (The others have already explained how to solve it.)
In Ruby, there is no way (note: in Ruby, "there is no way" actually translates to "there is always a way involving reflection") to access an instance variable from outside the instance. That's why it's called an instance variable after all, because it belongs to the instance. This is a legacy from Smalltalk: in Smalltalk there are no visibility restrictions, all methods are public. Thus, instance variables are the only way to do encapsulation in Smalltalk, and, after all, encapsulation is one of the pillars of OO. In Ruby, there are visibility restrictions (as we have seen above, for example), so it is not strictly necessary to hide instance variables for that reason. There is another reason, however: the Uniform Access Principle.
The UAP states that how to use a feature should be independent from how the feature is implemented. So, accessing a feature should always be the same, i.e. uniform. The reason for this is that the author of the feature is free to change how the feature works internally, without breaking the users of the feature. In other words, it's basic modularity.
This means for example that getting the size of a collection should always be the same, regardless of whether the size is stored in a variable, computed dynamically every time, lazily computed the first time and then stored in a variable, memoized or whatever. Sounds obvious, but e.g. Java gets this wrong:
obj.size # stored in a field
vs.
obj.getSize() # computed
Ruby takes the easy way out. In Ruby, there is only one way to use a feature: sending a message. Since there is only one way, access is trivially uniform.
So, to make a long story short: you simply can't access another instance's instance variable. you can only interact with that instance via message sending. Which means that the other object has to either provide you with a method (in this case at least of protected visibility) to access its instance variable, or you have to violate that object's encapsulation (and thus lose Uniform Access, increase coupling and risk future breakage) by using reflection (in this case instance_variable_get).
Here it is, in all its glory:
#!/usr/bin/env ruby
class X
def initialize(a=1)
#a = a
end
def m(other)
#a == other.a
end
protected
attr_reader :a
end
require 'test/unit'
class TestX < Test::Unit::TestCase
def test_that_m_evaluates_to_true_when_passed_two_empty_xs
x, y = X.new, X.new
assert x.m(y)
end
def test_that_m_evaluates_to_true_when_passed_two_xs_with_equal_attributes
assert X.new('foo').m(X.new('foo'))
end
end
Or alternatively:
class X
def m(other)
#a == other.instance_variable_get(:#a)
end
end
Which one of those two you chose is a matter of personly taste, I would say. The Set class in the standard library uses the reflection version, although it uses instance_eval instead:
class X
def m(other)
#a == other.instance_eval { #a }
end
end
(I have no idea why. Maybe instance_variable_get simply didn't exist when Set was written. Ruby is going to be 17 years old in February, some of the stuff in the stdlib is from the very early days.)
There are several methods
Getter:
class X
attr_reader :a
def m( other )
a == other.a
end
end
instance_eval:
class X
def m( other )
#a == other.instance_eval { #a }
end
end
instance_variable_get:
class X
def m( other )
#a == other.instance_variable_get :#a
end
end
I don't think ruby has a concept of "friend" or "protected" access, and even "private" is easily hacked around. Using a getter creates a read-only property, and instance_eval means you have to know the name of the instance variable, so the connotation is similar.
If you don't use the instance_eval option (as #jleedev posted), and choose to use a getter method, you can still keep it protected
If you want a protected method in Ruby, just do the following to create a getter that can only be read from objects of the same class:
class X
def new()
#a = 1
end
def m( other )
#a == other.a
end
protected
def a
#a
end
end
x = X.new()
y = X.new()
x.m( y ) # Returns true
x.a # Throws error
Not sure, but this might help:
Outside of the class, it's a little bit harder:
# Doesn't work:
irb -> a.#foo
SyntaxError: compile error
(irb):9: syntax error, unexpected tIVAR
from (irb):9
# But you can access it this way:
irb -> a.instance_variable_get(:#foo)
=> []
http://whynotwiki.com/Ruby_/_Variables_and_constants#Variable_scope.2Faccessibility