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
Karan kyanam
7,293 PointsCan anyone clearly explain to me all about ruby classes Especially using self.
So what is the self method?
What does ruby mean by self is implied?
How does self work inside a method in a class?
How does self work outside of a class?
How does self work with child classes?
If anyone could provide some clear examples that would be awesome
4 Answers
Unsubscribed User
3,309 PointsThis is going to be a case of the blind leading the blind, but no one else has answered in 2 weeks, so I'm going to give it a go since 'self' has been plaguing me too.
Warning! Most of this is stolen from other people and I understand it poorly.
definition: 'self' is the receiver object of the current method
For example:
array.size means array is the receiver object and size is the method.
Q1. So what is the self method?
self is not a method, it is the object a method is applied to
Q2. What does ruby mean by self is implied?
self is the implied receiver. The keyword self in Ruby gives you access to the current object – the object that is receiving the current message.
Q3. How does self work inside a method in a class?
In the context of a class, self refers to the current class. Inside of a class, self points to that class. This is critical to understanding self.
class Dog
puts self # => Dog
end
Defining a method on self creates a class method vs. an instance method.
class MyClass
def self.foo
puts 'I am a class method'
end
def bar
puts 'I am an instance method'
end
end
MyClass.foo # => "I am a class method"
MyClass.new.bar # => "I am an instance method"
Another way of defining the same class method and instance method:
class MyClass
class << self
def foo
puts 'I am still a class method'
end
end
def bar
puts 'I am still an instance method'
end
end
MyClass.foo # => "I am still a class method"
MyClass.new.bar # => "I am still an instance method"
Classes group similar objects and describe Attributes and Behaviour. For example: in class Dog, all dogs have a name and breed; they can also bark. The attributes (or properties) are name and breed, the behaviour (or method) is bark. Here are some other resources: https://www.youtube.com/watch?v=SS-9y0H3Si8 http://www.codecademy.com/courses/ruby-beginner-en-MFiQ6/0/1
Q4. How does self work outside of a class?
Q5. How does self work with child classes?
I don't know the answers to this.
Perhaps Maciej Czuchnowski would correct any mistakes I have made and answer the rest of your questions?
Maciej Czuchnowski
36,441 PointsChristine, I don't see any mistakes that would need a correction :)
To understand self, you have to read some explanations and examples given by various people and then experiment on your own in the interpreter. Some of my favorite explanations:
http://stackoverflow.com/a/6671310
http://www.jimmycuadra.com/posts/self-in-ruby
I still go through trial-and-error when dealing with self. It's not something I understand thoroughly and use everyday, but hopefully these links will help you :). And like I said, as with anything in programming, write some weird combinations of class methods, instance methods and local variables and run that code to see how it behaves.
Unsubscribed User
3,309 PointsWell, that's very affirming! I read both of those pages. Repeatedly. I stole from the first.
Karan, I also found two other links. The first is very quick and easy to go through and explains objects, classes and methods simply and clearly. The second has tons of self examples.
http://www.codequizzes.com/learn-ruby/intro-object-oriented-programming
http://www.eriktrautman.com/posts/ruby-explained-classes
Q4. How does self work outside of a class?
self is always defined. Try typing this in and out of irb: in irb self has many irb methods:
self.inspect # => "main"
self.class # => Object
self.methods.sort # (a long list...)
You can use self inside of instance methods, (which inherit from the class Object); however, as you can see, the keyword self is not necessary - it is implicit (vs. explicit). Try running the same program, changing only line2:
def self.bark to def bark - self.bark still works.
def fido
def self.bark
"Arf!"
end
p self # => main
p self.bark # => "Arf!"
end
p fido.bark # => "Arf!"
Q5. How does self work with child classes?
class Dog
def self.bark
"Woof!"
end
p self # => Dog
end
class Snoopy < Dog
def self.speak
"<Silence>"
end
p self # => Snoopy
end
p Snoopy.bark # Woof!
p Snoopy.speak # <Silence>
There's more, but it involves singletons and the eigenclass and I've found it quite challenging. http://madebydna.com/all/code/2011/06/24/eigenclasses-demystified.html
Karan kyanam
7,293 PointsHey! Thanks, guys! I haven't been using tree house. Although I am relearning ruby with a different resource. I will be sure to update what my understanding of self is post that!