Home Ruby Safenavigationoperatorapple&.color
Post
Cancel

Ruby Safenavigationoperatorapple&.color

refer to:
https://stackoverflow.com/questions/36812647/what-does-ampersand-dot-mean-in-ruby

class Apple 
    attr_accessor :color
end

apple = Apple.new color: 'red'
apple&.color # => red
b = nil
b&.color #=> nil

所以, &.  就相当于 rails 中的try

This post is licensed under CC BY 4.0 by the author.