Trying to understand what a Method is, how can it be 'part of a class'?
up vote
2
down vote
favorite
I've done some work with functions in Javascript, and thought that a Method was the Ruby name for the same. I recently did a technical interview and the interviewer was trying to help me debug by explaining how Methods were part of a class, and that it's an OOP thing.
I can't spot a functional difference between a Method and an equivalent Function, so I don't see what classes have to do with it.
Can you explain the whole 'Methods are part of a class' thing and why it matters? How can a Method be part of a class? Class as in an integer or a string?
The interviewer believed it would help, but it seems like a tiny technicality more than something useful.
ruby methods
|
show 2 more comments
up vote
2
down vote
favorite
I've done some work with functions in Javascript, and thought that a Method was the Ruby name for the same. I recently did a technical interview and the interviewer was trying to help me debug by explaining how Methods were part of a class, and that it's an OOP thing.
I can't spot a functional difference between a Method and an equivalent Function, so I don't see what classes have to do with it.
Can you explain the whole 'Methods are part of a class' thing and why it matters? How can a Method be part of a class? Class as in an integer or a string?
The interviewer believed it would help, but it seems like a tiny technicality more than something useful.
ruby methods
1
Well, ruby doesn't have functions. Only methods. And they all belong to some class.
– Sergio Tulentsev
Nov 21 at 14:16
2
"and why it matters" - classes are "data + behaviour". Methods are the behaviour part. Without them, classes become just dumb structs and are much less useful (from OOP perspective anyway)
– Sergio Tulentsev
Nov 21 at 14:22
@SergioTulentsev Doesnt ruby lambdas kind of a functions in js?
– Martin Zinovsky
Nov 21 at 14:33
2
@MartinZinovsky: similar, but not quite the same. 1) They are fully-fledged objects themselves and because of that 2) they are not directly invocable (you can't domy_lambda(), onlymy_lambda.callormy_lambda.()or one of 50 other ways)
– Sergio Tulentsev
Nov 21 at 15:04
2
"How can a Method be part of a class?" – by definition, that's essentially what makes it a method.
– Stefan
Nov 21 at 16:18
|
show 2 more comments
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I've done some work with functions in Javascript, and thought that a Method was the Ruby name for the same. I recently did a technical interview and the interviewer was trying to help me debug by explaining how Methods were part of a class, and that it's an OOP thing.
I can't spot a functional difference between a Method and an equivalent Function, so I don't see what classes have to do with it.
Can you explain the whole 'Methods are part of a class' thing and why it matters? How can a Method be part of a class? Class as in an integer or a string?
The interviewer believed it would help, but it seems like a tiny technicality more than something useful.
ruby methods
I've done some work with functions in Javascript, and thought that a Method was the Ruby name for the same. I recently did a technical interview and the interviewer was trying to help me debug by explaining how Methods were part of a class, and that it's an OOP thing.
I can't spot a functional difference between a Method and an equivalent Function, so I don't see what classes have to do with it.
Can you explain the whole 'Methods are part of a class' thing and why it matters? How can a Method be part of a class? Class as in an integer or a string?
The interviewer believed it would help, but it seems like a tiny technicality more than something useful.
ruby methods
ruby methods
asked Nov 21 at 14:15
Jules
324
324
1
Well, ruby doesn't have functions. Only methods. And they all belong to some class.
– Sergio Tulentsev
Nov 21 at 14:16
2
"and why it matters" - classes are "data + behaviour". Methods are the behaviour part. Without them, classes become just dumb structs and are much less useful (from OOP perspective anyway)
– Sergio Tulentsev
Nov 21 at 14:22
@SergioTulentsev Doesnt ruby lambdas kind of a functions in js?
– Martin Zinovsky
Nov 21 at 14:33
2
@MartinZinovsky: similar, but not quite the same. 1) They are fully-fledged objects themselves and because of that 2) they are not directly invocable (you can't domy_lambda(), onlymy_lambda.callormy_lambda.()or one of 50 other ways)
– Sergio Tulentsev
Nov 21 at 15:04
2
"How can a Method be part of a class?" – by definition, that's essentially what makes it a method.
– Stefan
Nov 21 at 16:18
|
show 2 more comments
1
Well, ruby doesn't have functions. Only methods. And they all belong to some class.
– Sergio Tulentsev
Nov 21 at 14:16
2
"and why it matters" - classes are "data + behaviour". Methods are the behaviour part. Without them, classes become just dumb structs and are much less useful (from OOP perspective anyway)
– Sergio Tulentsev
Nov 21 at 14:22
@SergioTulentsev Doesnt ruby lambdas kind of a functions in js?
– Martin Zinovsky
Nov 21 at 14:33
2
@MartinZinovsky: similar, but not quite the same. 1) They are fully-fledged objects themselves and because of that 2) they are not directly invocable (you can't domy_lambda(), onlymy_lambda.callormy_lambda.()or one of 50 other ways)
– Sergio Tulentsev
Nov 21 at 15:04
2
"How can a Method be part of a class?" – by definition, that's essentially what makes it a method.
– Stefan
Nov 21 at 16:18
1
1
Well, ruby doesn't have functions. Only methods. And they all belong to some class.
– Sergio Tulentsev
Nov 21 at 14:16
Well, ruby doesn't have functions. Only methods. And they all belong to some class.
– Sergio Tulentsev
Nov 21 at 14:16
2
2
"and why it matters" - classes are "data + behaviour". Methods are the behaviour part. Without them, classes become just dumb structs and are much less useful (from OOP perspective anyway)
– Sergio Tulentsev
Nov 21 at 14:22
"and why it matters" - classes are "data + behaviour". Methods are the behaviour part. Without them, classes become just dumb structs and are much less useful (from OOP perspective anyway)
– Sergio Tulentsev
Nov 21 at 14:22
@SergioTulentsev Doesnt ruby lambdas kind of a functions in js?
– Martin Zinovsky
Nov 21 at 14:33
@SergioTulentsev Doesnt ruby lambdas kind of a functions in js?
– Martin Zinovsky
Nov 21 at 14:33
2
2
@MartinZinovsky: similar, but not quite the same. 1) They are fully-fledged objects themselves and because of that 2) they are not directly invocable (you can't do
my_lambda(), only my_lambda.call or my_lambda.() or one of 50 other ways)– Sergio Tulentsev
Nov 21 at 15:04
@MartinZinovsky: similar, but not quite the same. 1) They are fully-fledged objects themselves and because of that 2) they are not directly invocable (you can't do
my_lambda(), only my_lambda.call or my_lambda.() or one of 50 other ways)– Sergio Tulentsev
Nov 21 at 15:04
2
2
"How can a Method be part of a class?" – by definition, that's essentially what makes it a method.
– Stefan
Nov 21 at 16:18
"How can a Method be part of a class?" – by definition, that's essentially what makes it a method.
– Stefan
Nov 21 at 16:18
|
show 2 more comments
2 Answers
2
active
oldest
votes
up vote
3
down vote
Can you explain the whole 'Methods are part of a class' thing and why it matters? How can a Method be part of a class? Class as in an integer or a string?
Let's say you have two classes, Apple and Cake. Let's assume that when you sell an apple, it has a tax rate of 10%, and cake 20%. By splitting the methods into individual classes, we can define a different method for 'price_with_tax' to each class:
class Apple < ApplicationRecord
def price_with_tax
self.price * 1.1
end
end
class Cake < ApplicationRecord
def price_with_tax
self.price * 1.2
end
end
In javascript we wouldn't be able to do this, and would need to have 2 methods, 'add 10% tax' and 'add 20% tax'. By structuring the methods as we have, we're able to do:
apple = Apple.find(1)
cake = Cake.find(1)
cake.price_with_tax
apple.price_with_tax
I think I get it, part of the problem was my understanding of what a class was. So is it that a Method requires a class, that is to say some information that can be contained within or without of the Method to work, and that the information is considered an object in itself?
– Jules
Nov 21 at 15:49
2
I think your confusions comes from using functions in JS which don't require a class. You can call any function you've defined from anywhere in your code. Class orientated design is pretty much the opposite of this - you want the same method (function name) to do different things to different classes. A ruby method has to operate on a class (and where it looks like you aren't calling it on a class it's in fact happening without you realising - like when you call a method in a controller, the method is called on an instance of that controller class)
– Mark
Nov 21 at 16:52
add a comment |
up vote
2
down vote
Methods are generally something that a class can do,
class MailClient(for example) might have methods such as sendMail, getMail, forwardMail, etc. In OOP, methods should for the most part be something that a class can do.
MailClient.getMail();
The above code can be conceptualized as telling the class to invoke its getMail() behavior.
You may think of this in real-life terms such as:
Dog.bark();
Objects have behavior and attributes, the behaviors are the methods.
So my understanding now is that you can create some information as a class, and then apply a Method to it. Does a Method always require a class, and would you say that Methods are built around classes?
– Jules
Nov 21 at 15:54
2
Methods/Functions in JavaScript do not require a class, although it helps greatly with code organization, and can improve readability by logically grouping a class's specific functions together. I would suggest reading more on JavaScript classes as they work differently than some OOP languages today. A lot of it boils down to personal preference, however, following known JavaScript coding conventions will make your code more easily understandable by a wider array of audiences.
– Dustin R
Nov 21 at 16:34
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
Can you explain the whole 'Methods are part of a class' thing and why it matters? How can a Method be part of a class? Class as in an integer or a string?
Let's say you have two classes, Apple and Cake. Let's assume that when you sell an apple, it has a tax rate of 10%, and cake 20%. By splitting the methods into individual classes, we can define a different method for 'price_with_tax' to each class:
class Apple < ApplicationRecord
def price_with_tax
self.price * 1.1
end
end
class Cake < ApplicationRecord
def price_with_tax
self.price * 1.2
end
end
In javascript we wouldn't be able to do this, and would need to have 2 methods, 'add 10% tax' and 'add 20% tax'. By structuring the methods as we have, we're able to do:
apple = Apple.find(1)
cake = Cake.find(1)
cake.price_with_tax
apple.price_with_tax
I think I get it, part of the problem was my understanding of what a class was. So is it that a Method requires a class, that is to say some information that can be contained within or without of the Method to work, and that the information is considered an object in itself?
– Jules
Nov 21 at 15:49
2
I think your confusions comes from using functions in JS which don't require a class. You can call any function you've defined from anywhere in your code. Class orientated design is pretty much the opposite of this - you want the same method (function name) to do different things to different classes. A ruby method has to operate on a class (and where it looks like you aren't calling it on a class it's in fact happening without you realising - like when you call a method in a controller, the method is called on an instance of that controller class)
– Mark
Nov 21 at 16:52
add a comment |
up vote
3
down vote
Can you explain the whole 'Methods are part of a class' thing and why it matters? How can a Method be part of a class? Class as in an integer or a string?
Let's say you have two classes, Apple and Cake. Let's assume that when you sell an apple, it has a tax rate of 10%, and cake 20%. By splitting the methods into individual classes, we can define a different method for 'price_with_tax' to each class:
class Apple < ApplicationRecord
def price_with_tax
self.price * 1.1
end
end
class Cake < ApplicationRecord
def price_with_tax
self.price * 1.2
end
end
In javascript we wouldn't be able to do this, and would need to have 2 methods, 'add 10% tax' and 'add 20% tax'. By structuring the methods as we have, we're able to do:
apple = Apple.find(1)
cake = Cake.find(1)
cake.price_with_tax
apple.price_with_tax
I think I get it, part of the problem was my understanding of what a class was. So is it that a Method requires a class, that is to say some information that can be contained within or without of the Method to work, and that the information is considered an object in itself?
– Jules
Nov 21 at 15:49
2
I think your confusions comes from using functions in JS which don't require a class. You can call any function you've defined from anywhere in your code. Class orientated design is pretty much the opposite of this - you want the same method (function name) to do different things to different classes. A ruby method has to operate on a class (and where it looks like you aren't calling it on a class it's in fact happening without you realising - like when you call a method in a controller, the method is called on an instance of that controller class)
– Mark
Nov 21 at 16:52
add a comment |
up vote
3
down vote
up vote
3
down vote
Can you explain the whole 'Methods are part of a class' thing and why it matters? How can a Method be part of a class? Class as in an integer or a string?
Let's say you have two classes, Apple and Cake. Let's assume that when you sell an apple, it has a tax rate of 10%, and cake 20%. By splitting the methods into individual classes, we can define a different method for 'price_with_tax' to each class:
class Apple < ApplicationRecord
def price_with_tax
self.price * 1.1
end
end
class Cake < ApplicationRecord
def price_with_tax
self.price * 1.2
end
end
In javascript we wouldn't be able to do this, and would need to have 2 methods, 'add 10% tax' and 'add 20% tax'. By structuring the methods as we have, we're able to do:
apple = Apple.find(1)
cake = Cake.find(1)
cake.price_with_tax
apple.price_with_tax
Can you explain the whole 'Methods are part of a class' thing and why it matters? How can a Method be part of a class? Class as in an integer or a string?
Let's say you have two classes, Apple and Cake. Let's assume that when you sell an apple, it has a tax rate of 10%, and cake 20%. By splitting the methods into individual classes, we can define a different method for 'price_with_tax' to each class:
class Apple < ApplicationRecord
def price_with_tax
self.price * 1.1
end
end
class Cake < ApplicationRecord
def price_with_tax
self.price * 1.2
end
end
In javascript we wouldn't be able to do this, and would need to have 2 methods, 'add 10% tax' and 'add 20% tax'. By structuring the methods as we have, we're able to do:
apple = Apple.find(1)
cake = Cake.find(1)
cake.price_with_tax
apple.price_with_tax
answered Nov 21 at 14:49
Mark
1,5481522
1,5481522
I think I get it, part of the problem was my understanding of what a class was. So is it that a Method requires a class, that is to say some information that can be contained within or without of the Method to work, and that the information is considered an object in itself?
– Jules
Nov 21 at 15:49
2
I think your confusions comes from using functions in JS which don't require a class. You can call any function you've defined from anywhere in your code. Class orientated design is pretty much the opposite of this - you want the same method (function name) to do different things to different classes. A ruby method has to operate on a class (and where it looks like you aren't calling it on a class it's in fact happening without you realising - like when you call a method in a controller, the method is called on an instance of that controller class)
– Mark
Nov 21 at 16:52
add a comment |
I think I get it, part of the problem was my understanding of what a class was. So is it that a Method requires a class, that is to say some information that can be contained within or without of the Method to work, and that the information is considered an object in itself?
– Jules
Nov 21 at 15:49
2
I think your confusions comes from using functions in JS which don't require a class. You can call any function you've defined from anywhere in your code. Class orientated design is pretty much the opposite of this - you want the same method (function name) to do different things to different classes. A ruby method has to operate on a class (and where it looks like you aren't calling it on a class it's in fact happening without you realising - like when you call a method in a controller, the method is called on an instance of that controller class)
– Mark
Nov 21 at 16:52
I think I get it, part of the problem was my understanding of what a class was. So is it that a Method requires a class, that is to say some information that can be contained within or without of the Method to work, and that the information is considered an object in itself?
– Jules
Nov 21 at 15:49
I think I get it, part of the problem was my understanding of what a class was. So is it that a Method requires a class, that is to say some information that can be contained within or without of the Method to work, and that the information is considered an object in itself?
– Jules
Nov 21 at 15:49
2
2
I think your confusions comes from using functions in JS which don't require a class. You can call any function you've defined from anywhere in your code. Class orientated design is pretty much the opposite of this - you want the same method (function name) to do different things to different classes. A ruby method has to operate on a class (and where it looks like you aren't calling it on a class it's in fact happening without you realising - like when you call a method in a controller, the method is called on an instance of that controller class)
– Mark
Nov 21 at 16:52
I think your confusions comes from using functions in JS which don't require a class. You can call any function you've defined from anywhere in your code. Class orientated design is pretty much the opposite of this - you want the same method (function name) to do different things to different classes. A ruby method has to operate on a class (and where it looks like you aren't calling it on a class it's in fact happening without you realising - like when you call a method in a controller, the method is called on an instance of that controller class)
– Mark
Nov 21 at 16:52
add a comment |
up vote
2
down vote
Methods are generally something that a class can do,
class MailClient(for example) might have methods such as sendMail, getMail, forwardMail, etc. In OOP, methods should for the most part be something that a class can do.
MailClient.getMail();
The above code can be conceptualized as telling the class to invoke its getMail() behavior.
You may think of this in real-life terms such as:
Dog.bark();
Objects have behavior and attributes, the behaviors are the methods.
So my understanding now is that you can create some information as a class, and then apply a Method to it. Does a Method always require a class, and would you say that Methods are built around classes?
– Jules
Nov 21 at 15:54
2
Methods/Functions in JavaScript do not require a class, although it helps greatly with code organization, and can improve readability by logically grouping a class's specific functions together. I would suggest reading more on JavaScript classes as they work differently than some OOP languages today. A lot of it boils down to personal preference, however, following known JavaScript coding conventions will make your code more easily understandable by a wider array of audiences.
– Dustin R
Nov 21 at 16:34
add a comment |
up vote
2
down vote
Methods are generally something that a class can do,
class MailClient(for example) might have methods such as sendMail, getMail, forwardMail, etc. In OOP, methods should for the most part be something that a class can do.
MailClient.getMail();
The above code can be conceptualized as telling the class to invoke its getMail() behavior.
You may think of this in real-life terms such as:
Dog.bark();
Objects have behavior and attributes, the behaviors are the methods.
So my understanding now is that you can create some information as a class, and then apply a Method to it. Does a Method always require a class, and would you say that Methods are built around classes?
– Jules
Nov 21 at 15:54
2
Methods/Functions in JavaScript do not require a class, although it helps greatly with code organization, and can improve readability by logically grouping a class's specific functions together. I would suggest reading more on JavaScript classes as they work differently than some OOP languages today. A lot of it boils down to personal preference, however, following known JavaScript coding conventions will make your code more easily understandable by a wider array of audiences.
– Dustin R
Nov 21 at 16:34
add a comment |
up vote
2
down vote
up vote
2
down vote
Methods are generally something that a class can do,
class MailClient(for example) might have methods such as sendMail, getMail, forwardMail, etc. In OOP, methods should for the most part be something that a class can do.
MailClient.getMail();
The above code can be conceptualized as telling the class to invoke its getMail() behavior.
You may think of this in real-life terms such as:
Dog.bark();
Objects have behavior and attributes, the behaviors are the methods.
Methods are generally something that a class can do,
class MailClient(for example) might have methods such as sendMail, getMail, forwardMail, etc. In OOP, methods should for the most part be something that a class can do.
MailClient.getMail();
The above code can be conceptualized as telling the class to invoke its getMail() behavior.
You may think of this in real-life terms such as:
Dog.bark();
Objects have behavior and attributes, the behaviors are the methods.
answered Nov 21 at 14:30
Dustin R
286
286
So my understanding now is that you can create some information as a class, and then apply a Method to it. Does a Method always require a class, and would you say that Methods are built around classes?
– Jules
Nov 21 at 15:54
2
Methods/Functions in JavaScript do not require a class, although it helps greatly with code organization, and can improve readability by logically grouping a class's specific functions together. I would suggest reading more on JavaScript classes as they work differently than some OOP languages today. A lot of it boils down to personal preference, however, following known JavaScript coding conventions will make your code more easily understandable by a wider array of audiences.
– Dustin R
Nov 21 at 16:34
add a comment |
So my understanding now is that you can create some information as a class, and then apply a Method to it. Does a Method always require a class, and would you say that Methods are built around classes?
– Jules
Nov 21 at 15:54
2
Methods/Functions in JavaScript do not require a class, although it helps greatly with code organization, and can improve readability by logically grouping a class's specific functions together. I would suggest reading more on JavaScript classes as they work differently than some OOP languages today. A lot of it boils down to personal preference, however, following known JavaScript coding conventions will make your code more easily understandable by a wider array of audiences.
– Dustin R
Nov 21 at 16:34
So my understanding now is that you can create some information as a class, and then apply a Method to it. Does a Method always require a class, and would you say that Methods are built around classes?
– Jules
Nov 21 at 15:54
So my understanding now is that you can create some information as a class, and then apply a Method to it. Does a Method always require a class, and would you say that Methods are built around classes?
– Jules
Nov 21 at 15:54
2
2
Methods/Functions in JavaScript do not require a class, although it helps greatly with code organization, and can improve readability by logically grouping a class's specific functions together. I would suggest reading more on JavaScript classes as they work differently than some OOP languages today. A lot of it boils down to personal preference, however, following known JavaScript coding conventions will make your code more easily understandable by a wider array of audiences.
– Dustin R
Nov 21 at 16:34
Methods/Functions in JavaScript do not require a class, although it helps greatly with code organization, and can improve readability by logically grouping a class's specific functions together. I would suggest reading more on JavaScript classes as they work differently than some OOP languages today. A lot of it boils down to personal preference, however, following known JavaScript coding conventions will make your code more easily understandable by a wider array of audiences.
– Dustin R
Nov 21 at 16:34
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53414029%2ftrying-to-understand-what-a-method-is-how-can-it-be-part-of-a-class%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Well, ruby doesn't have functions. Only methods. And they all belong to some class.
– Sergio Tulentsev
Nov 21 at 14:16
2
"and why it matters" - classes are "data + behaviour". Methods are the behaviour part. Without them, classes become just dumb structs and are much less useful (from OOP perspective anyway)
– Sergio Tulentsev
Nov 21 at 14:22
@SergioTulentsev Doesnt ruby lambdas kind of a functions in js?
– Martin Zinovsky
Nov 21 at 14:33
2
@MartinZinovsky: similar, but not quite the same. 1) They are fully-fledged objects themselves and because of that 2) they are not directly invocable (you can't do
my_lambda(), onlymy_lambda.callormy_lambda.()or one of 50 other ways)– Sergio Tulentsev
Nov 21 at 15:04
2
"How can a Method be part of a class?" – by definition, that's essentially what makes it a method.
– Stefan
Nov 21 at 16:18