I'm new to ruby on rails, there's a code something like:
person_map.try(:[], :name)
What's the meanning of :[] here?
1 : a narrow passageway between fences or hedges. 2 : a relatively narrow way or track: such as. a : an ocean route used by or prescribed for ships. b : a strip of roadway for a single line of vehicles. c : air lane.
to curve or wind in and out; creep in a winding path. a snake sinuating along the ground.
1 : having a large measure across : broad a wide street a wide grin. 2 : opened as far as possible Her eyes were wide with wonder. 3 : covering a very large area a wide expanse of ocean. 4 : measured across or at right angles to length The cloth is three feet wide.
to make a long, low sound of pain, suffering, or another strong emotion: He moaned with pain before losing consciousness. "Let me die," he moaned. SMART Vocabulary: related words and phrases.
:[]
is a symbol. In the context of your line of code, it represents the name of the method which should be try
ed.
You should check the documentation of try
to better understand that line of code. It's basically equal to personMap[:name]
while making sure that no exception will be raised if personMap
does not have a []
method (e.g if personMap
is nil).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With