[Ruby on Rails] Read try (: [],: key)

Introduction

user.try(:[],:name)

The first time I saw this code "Why are you putting an empty array in the method part ...?" I thought. It's a bad thing that I skipped the Ruby (Rails) grammar ...

What is Rails try in the first place?

Methods included in Active Support (cannot be used only in Ruby) Use the specified method only when the target object is not nil Give the method you want to use as the first argument and its argument (if necessary) as the second argument

user = {name: "test"}

# user[:group]Causes a method error because is nil
user[:group].replace("A")
# => NoMethodError: undefined method 'replace' for nil:NilClass

#If you use try on a nil object, no error will occur, only nil will be returned.
user[:group].try(:replace,"A")
=> nil

Is an empty array a method?

That's the main subject, but ruby's Hash has a [] method. Takes the key name as an argument and returns its value.

user.[] :name
=> "test"
#This is user[:name]Same as

By the way, "+" etc. can also be used as a method.

user[:name].+ 2
=> "test2"

In this case, it's a combination of try and this method.

user.try(:[],:name)
=> "test"

The result will be returned.

Summary

It was a little difficult to understand because I didn't hit [] easily. Re-study ruby / rails grammar ...

Recommended Posts

[Ruby on Rails] Read try (: [],: key)
Ruby on Rails Elementary
Ruby on Rails basics
Ruby On Rails Association
Ruby on rails learning record -2020.10.03
Portfolio creation Ruby on Rails
Ruby on rails learning record -2020.10.04
[Ruby on Rails] Debug (binding.pry)
Ruby on rails learning record -2020.10.05
Ruby on rails learning record -2020.10.09
Ruby on Rails config configuration
Ruby on Rails basic learning ①
[Ruby on Rails] about has_secure_password
Ruby on rails learning record-2020.10.07 ②
Commentary on partial! --Ruby on Rails
Ruby on rails learning record-2020.10.07 ①
Cancel Ruby on Rails migration
Ruby on rails learning record -2020.10.06
Ruby on Rails validation summary
Ruby on Rails Basic Memorandum
[Introduction] Try to create a Ruby on Rails application
Try using the query attribute of Ruby on Rails
Ruby on Rails Overview (Beginner Summary)
[Ruby on Rails] yarn install --check-files
Ruby on Rails variable, constant summary
Installing Ruby + Rails on Ubuntu 18.04 (rbenv)
[Ruby on Rails] Introduced paging function
Basic knowledge of Ruby on Rails
Progate Ruby on Rails5 Looking Back
[Ruby on Rails] Add a column with a foreign key constraint
How to use Ruby on Rails
[Ruby on Rails] Add / Remove Columns
Ruby on Rails Japanese-English support i18n
(Ruby on Rails6) "Erase" posted content
[Ruby on Rails] CSV output function
Ruby on Rails 6.0 environment construction memo
[Ruby on Rails] What is Bcrypt?
[Ruby on Rails] Confirmation page creation
Ruby On Rails devise routing conflict
[Ruby on Rails] Comment function implementation
[Ruby on Rails] DM, chat function
[Ruby on Rails] Convenient helper method
[Ruby on Rails] Stop "looping until ..."
[Ruby on Rails] Introduction of initial data
[Ruby on Rails] Creating an inquiry form
Ruby on Rails6 Practical Guide cp13 ~ cp15 [Memo]
[Ruby on Rails] View test with RSpec
[Ruby on Rails] How to use CarrierWave
[Ruby on Rails] Code check using Rubocop-airbnb
[Ruby on Rails] 1 model CRUD (Routing Main)
Ruby on Rails installation method [Mac edition]
[Ruby on Rails] model, controller terminal command
Let's summarize "MVC" of Ruby on Rails
Ruby on Rails model creation / deletion command
[Ruby on Rails] About bundler (for beginners)
Try deploying a Rails app on EC2-Part 1-
part of the syntax of ruby ​​on rails
Ruby on Rails6 Practical Guide cp7 ~ cp9 [Memo]
Ruby on Rails in Visual Studio Codespaces
[Ruby on Rails] Follow function implementation: Bidirectional
Notes on using FCM with Ruby on Rails