-[Reason why you can get the value by. (Dot) + column name when accessing the database instance](# Why you can get the value by. (Dot) + column name when accessing the database instance)
Because it is an instance (object) created by a class that inherits Active Record, it is possible to get the value using the .method. Since it is not the usual Ruby notation, I want to get the value for the hash object, so if I write it with dots, an error will occur.
example.rb
hash = {name: 'hoge', email: '[email protected]'}
hash[:name]
=> "hoge"
# .I get an error when I try to get it with
hash.name
NoMethodError (undefined method `name' for {:name=>"hoge", :email=>"[email protected]"}:Hash)
example.rb
#Assuming that the Hash class has already been defined, Hash.Create an instance with new
hash = Hash.new(name: 'hoge', email: '[email protected]')
hash[:name]
=> "hoge"
hash.name
=> "hoge"
Recommended Posts