How are symbols used to identify arguments in ruby methods

后端 未结 5 1125
星月不相逢
星月不相逢 2021-02-01 23:20

I am learning rails and going back to ruby to understand how methods in rails (and ruby really work). When I see method calls like:

validates :first_name, :presen         


        
5条回答
  •  粉色の甜心
    2021-02-01 23:45

    Symbols and hashes are values like any other, and can be passed like any other value type.

    Recall that ActiveRecord models accept a hash as an argument; it ends up being similar to this (it's not this simple, but it's the same idea in the end):

    class User
      attr_accessor :fname, :lname
    
      def initialize(args)
        @fname = args[:fname] if args[:fname]
        @lname = args[:lname] if args[:lname]
      end
    end
    
    u = User.new(:fname => 'Joe', :lname => 'Hacker')
    

    This takes advantage of not having to put the hash in curly-brackets {} unless you need to disambiguate parameters (and there's a block parsing issue as well when you skip the parens).

    Similarly:

    class TestItOut
      attr_accessor :field_name, :validations
    
      def initialize(field_name, validations)
        @field_name = field_name
        @validations = validations
      end
    
      def show_validations
        puts "Validating field '#{field_name}' with:"
        validations.each do |type, args|
          puts "  validator '#{type}' with args '#{args}'"
        end
      end
    end
    
    t = TestItOut.new(:name, presence: true, length: { min: 2, max: 10 })
    t.show_validations
    

    This outputs:

    Validating field 'name' with:
      validator 'presence' with args 'true'
      validator 'length' with args '{min: 2, max: 10}'
    

    From there you can start to see how things like this work.

提交回复
热议问题