Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6779321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:22:17+00:00 2026-05-26T16:22:17+00:00

I am learning rails and going back to ruby to understand how methods in

  • 0

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, :presence => true

I get confused. How do you write methods in ruby that accept symbols or hashes. The source code for the validates method is confusing too. Could someone please simplify this topic of using symbols as arguments in ruby class and instance methods for me?

UPDATE:

Good one @Dave! But What I was trying out was something like:

def full_name (:first_name, :last_name)
  @first_name = :first_name
  @last_name = :last_name
  p "#{@first_name} #{last_name}"
end

full_name("Breta", "Von Sustern")

Which obviously raises errors. I am trying to understand: Why is passing symbols like this as arguments wrong if symbols are just like any other value?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T16:22:18+00:00Added an answer on May 26, 2026 at 4:22 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm learning VIM for Rails development and would like to easily navigate methods in
I'm learning Selenium now. I'm going to use it in the Ruby on Rails
I'm learning Rails and it's going well so far. My biggest question at the
I am learning Ruby on Rails so I'm sure I'll find this out sooner
I'm learning Ruby on Rails and doing the contactlist thing, 20 minute blog and
I'm learning Ruby on Rails. I'm using some associations (has_many :through, ...) But i'm
I started learning Ruby on Rails and found myself confounded by the syntax, so
I'm learning Ruby on Rails with the AWDR book and have had to be
I'm working on learning ruby/rails at the moment - The book i'm reading has
I've been learning Ruby on Rails, HTML, CSS. In my opinion, the best way

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.