[Ruby] Difference between symbol variables and character string variables. About the difference between [: a] and ['a'].

This is a personal memo.

Depending on how to get the value of the object, the structure of the target data is different between obj [: a] and obj ['a'].

Depends on whether the object is created with a ** symbol ** or a ** string **.

Objects that use strings are called ** hashes **.


## Objects using symbols

If obj = {a: 1, b: 2}, the property name is a symbol. The symbol is also used to specify when acquiring data.

object


obj = {a:1, b:2}
=> {:a=>1, :b=>2}

Data acquisition


##OK
obj[:a]
=> 1

##NG
obj['a']
=> nil

Specifying the property name of a symbolic object with ''' will result in an error.


## Object using a string

Data acquisition of an object created with a character string such as obj2 = {"a" => 1, "b" => 2} is specified with a character string.

python


##Object definition
obj2 = {"a" => 1, "b" => 2}
=> {"a"=>1, "b"=>2}

##Data acquisition
obj2['a']
=> 1
obj2["a"]
=> 1


##NG
obj2[:a]
=> nil

Using a symbol will result in an error.

Recommended Posts

[Ruby] Difference between symbol variables and character string variables. About the difference between [: a] and ['a'].
[Ruby] I thought about the difference between each_with_index and each.with_index
About the difference between classes and instances in Ruby
About the difference between "(double quotation)" and "single quotation" in Ruby
[Ruby] About the difference between 2 dots and 3 dots of range object.
About the difference between irb and pry
[Rails / ActiveRecord] About the difference between create and create!
[Rails] I learned about the difference between resources and resources
[Java] Difference between equals and == in a character string that is a reference type
What is the difference between a class and a struct? ?? ??
Calculate the difference between numbers in a Ruby array
Difference between variables and instance variables
[Ruby] Difference between methods with and without self in the class. About class methods and instance methods.
[Java] Note about the difference between equivalence judgment and equality judgment when comparing String classes
The nth and n + 1st characters of a Ruby string
The difference between programming with Ruby classes and programming without it
[Ruby] Difference between is_a? And instance_of?
Understand the difference between each_with_index and each.with_index
About the difference between gets and gets.chomp (other than line breaks)
[Ruby] Relationship between parent class and child class. The relationship between a class and an instance.
About the relationship between the Java String equality operator (==) and initialization. Beginners
What is the difference between a web server and an application server?
Easy to understand the difference between Ruby instance method and class method.
Note: Difference between Ruby "p" and "puts"
Difference between Ruby instance variable and local variable
Difference between "|| =" and "instance_variable_defined?" In Ruby memoization
[Ruby] Difference between print, puts and p
About Ruby variables
[Java] Understand the difference between List and Set
[iOS] Understand the difference between frame and bounds
[Delete the first letter of the character string] Ruby
Understand the difference between abstract classes and interfaces!
What is the difference between SimpleDateFormat and DateTimeFormatter? ??
[Ruby] Difference between puts and return, output and return value
A note about the Rails and Vue process
Ruby: Differences between class methods and instance methods, class variables and instance variables
Let's explain the difference between an interpreter and a compiler using a Venn diagram
[Ruby] I want to make an array from a character string with the split method. And vice versa.
Count the number of occurrences of a string in Ruby
[Ruby] Difference between receiver and object. Differences between Ruby objects and JS objects
What is the difference between System Spec and Feature Spec?
Is there a performance difference between Oracle JDK and OpenJDK at the end of 2017?
A rough note about Ruby arrays and hash objects
About the relationship between HTTP methods, actions and CRUD
[Java] Difference between static final and final in member variables
[Rails] What is the difference between redirect and render?
Compare the difference between dockerfile before and after docker-slim
[JAVA] What is the difference between interface and abstract? ?? ??
Ruby variables and methods
Understand in 3 minutes! A very rough explanation of the difference between session and cookie
What is the difference between skip and pending? [RSpec]
[Technical memo] About the advantages and disadvantages of Ruby
Difference between vh and%
[Rails] I investigated the difference between redirect_to and render.
Difference between i ++ and ++ i
About the [ruby] operator
What is the difference between Java EE and Jakarta EE?
[Java] Cut out a part of the character string with Matcher and regular expression
[Swift] UITextField taught me the difference between nil and ""
Note No. 6 "Calculate the formula for the one-digit sum difference received as a character string" [Java]
Memorandum No.4 "Get a character string and decorate it" [Java]