1. Conclusion </ b>
2. What is a method </ b>
3. How to use </ b>
4. Another solution </ b>
Use instance variables and instance methods!
"A fixed way to achieve the purpose. Method. Method"
However, in simple terms, it can be used by attaching it to a variable (instance method), or it refers to an original method such as an equation (length, each, times, etc.)!
When using it outside, the concept of scope comes out. I wrote a little in the article below!
Then, what actually happens is as follows.
Relationship between instance methods and instance variables
class Student
def initialize(gender, height, personality)
@gender = gender
@height = height
@personality = personality
end
def gender
@gender
end
def height
@height
end
def personality
@personality
end
end
student = Student.new("Man", "175cm", "friendly")
puts "sex:#{student.gender}height:#{student.height}Personality:#{student.personality}"
By doing this, you can freely attach methods to variables. It can also be used outside the "def end" (beyond the scope)!
You can write the same in the method!
Completed within the method
class student
def initialize(gender, height, personality)
@gender = gender
@height = height
@personality = personality
puts "sex:#{@gender}height:#{@height}#Personality:#{@personality}"
end
student = Student.new("Man", "175cm", "friendly")
You can still use it inside the method!
Recommended Posts