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 986811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:22:44+00:00 2026-05-16T05:22:44+00:00

Take the following class: class Automator def fill_specific_form(fields) fields.each_pair do |key, value| puts Setting

  • 0

Take the following class:

class Automator
  def fill_specific_form(fields)
    fields.each_pair do |key, value|
      puts "Setting '#{key}' to '#{value}'"
    end
  end
end

a = Automator.new
a.fill_specific_form :first_name => "Mads", :last_name => "Mobæk"

# => Setting 'first_name' to 'Mads'
# => Setting 'last_name' to 'Mobæk'

Is it possible to do the same without a hash? Since all parameters are required, I want a method with the following signature:

fill_specific_form(first_name, last_name)

In my mind this would be possible by having the method body reflect and iterate over its parameters, thus achieving the same result.

How would you implement this? Does a pattern/idiom for this exist already? Two obvious benefits would be parameter information in IDEs and not having to check if all hash keys are supplied.

What I want to avoid is:

puts "Setting first_name to #{first_name}"
puts "Setting last_name to #{last_name}"
# and so on
  • 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-16T05:22:45+00:00Added an answer on May 16, 2026 at 5:22 am

    If you set no other local variables inside the method, local_variables will give you a list of the method’s parameter names (if you do set other variables you can just call local_variables first thing and remember the result). So you can do what you want with local_variables+eval:

    class Automator
      def fill_specific_form(first_name, last_name)
        local_variables.each do |var|
          puts "Setting #{var} to #{eval var.to_s}"
        end
      end
    end
    
    Automator.new().fill_specific_form("Mads", "Mobaek")
    

    Be however advised that this is pure evil.

    And at least for your example

    puts "Setting first_name to #{first_name}"
    puts "Setting last_name to #{last_name}"
    

    seems much more sensible.

    You could also do fields = {:first_name => first_name, :last_name => last_name} at the beginning of the method and then go with your fields.each_pair code.

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

Sidebar

Related Questions

Take the following code: class ChallengesController < ApplicationController def update @challenge = Challenge.find(params[:id]) @challenge.update!(params[:challenge])
Take the following scenario: Public Class Store Public Overridable Property Areas As List(Of Area)
Take the following example, I have a class public class SomeItem { public string
Take the following linq query: var summary = results.Select(r => new { TotalPopulation =
take following class and two object definitions: class Rect{ public: enum centimeter; enum meter;
take two following classes: class Test1{ public: Test1()=default; Test1(char in1,char in2):char1(in1),char2(in2){} char char1; char
Based on my question - take the following code: class Nevermore60Customer: GenericCustomer { public
I am having a very odd error occurring in the following: class ServeHandler(blobstore_handlers.BlobstoreDownloadHandler): def
Take the following simple object model for example: class Course has_many :enrollments has_many :students,
Take the following code example: <?php class A { public function aa() { $output

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.