Methods specific only to an instance? What are they called in Ruby?

Posted by daremarkovic on Stack Overflow See other posts from Stack Overflow or by daremarkovic
Published on 2013-11-03T15:50:35Z Indexed on 2013/11/03 15:53 UTC
Read the original article Hit count: 155

Filed under:

I know there are "instance methods", "class methods" but what are these types of methods called, for eg:

s1 = "This is my STRING!"

def s1.m1
  downcase
end

p s1     # => "This is my STRING!"
p s1.m1   # => "this is my string!"

What type of method is the "m1" method called on the s1 "instance" of the "string" class? It's really weird because I didn't know this was possible at all if I try:

s2 = "This is ANOTHER string"
s2.m1 # => Won't work!

Which kind of makes sense, but not sure why defining methods like m1 on instances on a class are useful at all.

© Stack Overflow or respective owner

Related posts about ruby