Last active
December 11, 2015 23:58
-
-
Save prodis/4680197 to your computer and use it in GitHub Desktop.
Ruby Fundamental - Uma maneira não trivial de acessar valores de hash
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
my_hash[:status] if my_hash | |
# or | |
my_hash[:status] unless my_hash.nil? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyClass | |
attr_accessor :some_value | |
end | |
m = MyClass.new | |
m.some_value = 456 # => 456 | |
m.some_value # => 456 | |
m.try(:some_value) # => 456 | |
m.try(:some_value=, 123) # => 123 | |
m.some_value # => 123 | |
m = nil | |
m.some_value = 123 # => NoMethodError: undefined method `some_value=' for nil:NilClass | |
m.some_value # => NoMethodError: undefined method `some_value' for nil:NilClass | |
m.try(:some_value=, 123) # => nil | |
m.try(:some_value) # => nil |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
my_hash = { one: 111, two: 222, three: 33 } | |
my_hash.try(:[:one]) | |
# SyntaxError: unexpected '[', expecting tSTRING_CONTENT or tSTRING_DBEG or tSTRING_DVAR or tSTRING_END | |
# my_hash.try(:[:one]) | |
# ^ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
my_hash = { one: 111, two: 222, three: 33 } | |
my_hash.try("[:one]".to_sym) | |
# NoMethodError: undefined method `[:one]' for {:one=>111, :two=>222, :three=>33}:Hash |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
my_hash = { one: 111, two: 222, three: 33 } | |
my_hash.try(:[], :one) # => 111 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
my_hash = { one: 111, two: 222, three: 33 } | |
my_hash[:one] # => 111 | |
my_hash.[](:one) # => 111 | |
my_hash.[] :one # => 111 | |
my_hash.[]:one # => 111 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
my_hash = { one: 111, two: 222, three: 33 } | |
my_hash[:two] = 2 | |
my_hash.[]=(:two, 2) | |
my_hash.[]= :two, 2 | |
my_hash.[]=:two, 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment