Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Andrew Stelmach
12,583 PointsExtra Credit: Public, Private and Protected Methods
I'm busy working on this, but I've hit a problem that I can't explain. Check out the code below:
class BankAccount
def initialize(first_name, last_name)
@first_name = first_name
@last_name = last_name
@balance = 0
end
def protected_deposit(amount)
@balance += amount
end
protected :protected_deposit
def private_deposit(amount)
@balance += amount
end
private :private_deposit
def call_private_deposit(amount)
private_deposit(amount)
end
def call_protected_deposit(amount)
protected_deposit(amount)
end
#To show that you can't call a private method with a different instance of the same class.
def private_add_to_different_account(account, amount)
account.private_deposit(amount)
end
#To show that you can call a protected method with a different instance of the same class.
def protected_add_to_different_account(account, amount)
account.protected_deposit(amount)
end
end
one_instance = BankAccount.new("Andrew", "Stelmach")
another_instance = BankAccount.new("Someone", "Else")
Now, the last two lines don't work. However, if I delete those two lines and then load the rest of the code into irb and then type one_instance = BankAccount.new("Andrew", "Stelmach") it works.
Why can't I do exactly the same thing within the code, so I don't have to type it into irb??
1 Answer
Jason Zudell
Courses Plus Student 10,163 PointsThis is just a nicety of IRB and other REPLs for that matter.
It is often helpful to see what is being done when you are in the shell, but just for normal execution you don't need all that output.
In fact many people consider the output annoying. If you use more CLIs you'd find that many consider a 'quiet' response best. Because if it was successful, it did what you wanted, so just get out of my way so I can go on with my business. Try "mv"ing a file, if you did it correctly, you'll get no output. Make a mistake and it yells at you.
IRB is one case when you most likely WANT to see what is being done, so it is simply for convenience sake.
If you put some 'puts' in there after you create an instance you could see what was happening. "puts one_instance" or "puts one_instance.name" will show you something.
Andrew Stelmach
12,583 PointsAndrew Stelmach
12,583 PointsOops. Hold on, I've got it.
I was going into irb and loading the code into it. But if I add 'puts one_instance.inspect' to the code, then stay out of irb and run 'ruby visibility.rb', it returns what it should.
One thing I don't understand is WHY this is so! If someone could explain, it would be much appreciated. It's important, because even though I can make it work like this, if do as above but 'puts one_instance' it doesn't show me everything that's 'in' that instance. To see that, I have to add the inspect method to it.
However, if I'm in irb, load the class BankAccount code, and then create my own instances, for example: an_instance = BankAccount.new("Something", "Something")
and then simply type 'an_instance' when I'm in irb it returns the same as 'an_instance.inspect' does when running the whole code outside of irb (and having created an_instance from within the code). I haven't explained that very well, but I hope you know what I mean.