[Ruby] Handle instance variables with instance methods

Progate learning notes Click here for instance methods and instance variables [Ruby] method, instance method ... [Ruby] classes, instance variables, instances, etc ...


State of handling instance variables in instance method ↓

class Menu
  #Instance variables
  attr_accessor :name
  attr_accessor :price

  #Handle instance variables in instance methods
  # 「self.By using "variable name", you can handle instance variables in the instance method.
  def info
    return "#{self.name}: ¥#{self.price}" 
  end
end

#instance
menu = Menu.new()
menu.name = "Cream bun"
menu.price = 300

puts menu.info

output

Cream bun: ¥300

Reference: Progate

Recommended Posts

[Ruby] Handle instance variables with instance methods
About Ruby instance methods
Ruby variables and methods
[Ruby] methods, instance methods, etc ...
Ruby: Differences between class methods and instance methods, class variables and instance variables
[Ruby] Class methods, instance methods, etc.
Ruby variables and functions (methods)
Handle DatePicker with Ruby / gtk3 + glade3
What happens to instance variables when copying an instance with ruby
Handle application/pdf response as File with ruby
Ruby Learning # 15 Methods
About Ruby variables
About Ruby methods
[Ruby] Basic knowledge of class instance variables, etc.
Read environment variables with ruby ​​file [Super beginner]
[Ruby] Methods that can be used with strings
[Ruby] Difference between methods with and without self in the class. About class methods and instance methods.
Install Ruby 3.0.0 with asdf
[Note] Methods ending with?
Range where variables can be used with ruby [Scope]
Handle files with NIO.2.
Ruby Learning # 31 Object Methods
Handle devise with Rails
Handle JSON with minimal-json
About calling instance methods
[Ruby] About instance generation
11th, Classification with Ruby
Evolve Eevee with Ruby
In Ruby, methods with? Do not always return true/false.
Basic methods of Ruby hashes
Ruby version switching with rbenv
Basic methods of Ruby arrays
Solve Google problems with Ruby
I tried DI with Ruby
GraphQL Client starting with Ruby
(Note) Java classes / variables / methods
[Self-use memo] (Ruby) class, class instance, instance
Leap year judgment with Ruby
Format Ruby with VS Code
Integer check method with ruby
Use ruby variables in javascript.
Difference between variables and instance variables
Three Bit Manipulation Methods (Ruby)
[Ruby] What is an instance?
Ruby Learning # 8 Working With String
What are Ruby class methods?
About Java class variables class methods
[Ruby] problem with if statement
How to handle an instance
Ruby study notes (variables & functions)
[Ruby] Singular methods and singular classes
Studying with CodeWar (ruby) ⑤ proc
Use Ruby with Google Colab
Ruby methods and classes (basic)
[Ruby] REPL-driven development with pry
About instance variables and attr_ *
Getting Started with Ruby Modules
[ruby] Method call with argument
[Ruby] Singular methods and singular classes
Handle dates with Javascript (moment.js)
[Ruby] This is the solution. When should I use instance variables?