What is the “equals greater than” operator => in Ruby?

前端 未结 1 710
感情败类
感情败类 2020-12-14 18:47

In a Ruby on Rails tutorial, I am asked to type:

class Post < ActiveRecord::Base
    validates :name,  :presence => true  
    validates :title, :prese         


        
相关标签:
1条回答
  • 2020-12-14 19:22

    It is mainly a ruby operator that sets the value of a key inside a hash. Thus :

    { :minimum => 5 }
    

    Is a ruby hash that has the symbol :minimum as a key that maps to the value of 5. A hash with one entry, in this example. Same for :

    :presence => true
    

    Still a hash. However, in ruby, when you have a method, you can omit the {} that surround a hash. That is what happens with the validates method. It's a method and thus the passed hash does not explicitly need {}.

    0 讨论(0)
提交回复
热议问题