Difference between Ruby instance variable and local variable

Instance method

If you define a method in a class, that method becomes an instance method. Instance methods are methods that you can call on an instance of that class. (Example)

class User
  def emotion
     "Happy!"
  end
end

user = User.new
user.emotion

result

"Happy!"

Instance variables

Within a class, you can use instance variables (variables shared within the same instance). Variable names start with @. (Example)

class User
  def initialize(emotion)
    @emotion = emotion
  end

  def happy
    "I am #{@emotion}."
  end
end
user = User.new('Happy')
user.happy

result

"I am Happy."

Local variables

A variable created within a method or block. Start with a lowercase letter, an underscore. Local variables must always be created by assigning a value with = before referencing.

(Example)

class User
  def initialize(emotion)
    @emotion = emotion
  end

  def happy
    shuffled_emotion = @emotion.chars.shuffle.join
    "I am #{shuffled_emotion}."
  end
end
user = User.new('Happy')
user.happy

In this example, the local variable would be shuffled_emotion.

result

"I am ayppH."

Referenced literature

Introduction to Ruby for those who want to become professionals

Recommended Posts

Difference between Ruby instance variable and local variable
Difference between instance variable and class variable
Difference between variables and instance variables
Difference between class and instance
[Ruby] Difference between get and post
Difference between instance method and class method
[Ruby] Difference between is_a? And instance_of?
Note: Difference between Ruby "p" and "puts"
Difference between "|| =" and "instance_variable_defined?" In Ruby memoization
[Ruby] Difference between print, puts and p
Easy to understand the difference between Ruby instance method and class method.
Difference between vh and%
Difference between i ++ and ++ i
[Ruby] Difference between puts and return, output and return value
Ruby: Differences between class methods and instance methods, class variables and instance variables
[Java] Difference between "final variable" and "immutable object"
[Ruby] I thought about the difference between each_with_index and each.with_index
[Ruby] Difference between receiver and object. Differences between Ruby objects and JS objects
[Java] Difference between == and equals
Rails: Difference between resources and resources
Difference between puts and print
Difference between redirect_to and render
About the difference between classes and instances in Ruby
Difference between CUI and GUI
Difference between mockito-core and mockito-all
Difference between bundle and bundle install
[Ruby] Difference between methods with and without self in the class. About class methods and instance methods.
Difference between ArrayList and LinkedList
Difference between render and redirect_to
Difference between List and ArrayList
[Ruby] Difference between match / scan
Difference between .bashrc and .bash_profile
Difference between StringBuilder and StringBuffer
Difference between render and redirect_to
Difference between render and redirect_to
[Docker-compose] Difference between env_file and environment. Priority of environment variable application
About the difference between "(double quotation)" and "single quotation" in Ruby
[Ruby] About the difference between 2 dots and 3 dots of range object.
[Java] Difference between assignment of basic type variable and assignment of reference type variable
What is the difference between an action and an instance method?
The difference between programming with Ruby classes and programming without it
Difference between render method and redirect_to
Difference between == operator and equals method
[Java] Difference between Hashmap and HashTable
[Terminal] Difference between irb and pry
JavaServlet: Difference between executeQuery and executeUpdate
Difference between == operator and eqals method
Rough difference between RSpec and minitest
[Rails] Difference between find and find_by
Understand the difference between each_with_index and each.with_index
[JAVA] Difference between abstract and interface
Difference between Thymeleaf @RestController and @Controller
Difference between Stream map and flatMap
[Java] Difference between array and ArrayList
Difference between primitive type and reference type
Difference between string.getByte () and Hex.decodeHex (string.toCharaArray ())
[Java] Difference between Closeable and AutoCloseable
[Java] Difference between StringBuffer and StringBuilder
[Java] Difference between length, length () and size ()
[rails] Difference between redirect_to and render
[Android] Difference between finish (); and return;