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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:05:19+00:00 2026-05-30T02:05:19+00:00

In a constructor, it often happens that you want to turn the arguments into

  • 0

In a constructor, it often happens that you want to turn the arguments into instance variables. A naive way to do it is:

class A
    def initialize a, b, c
        @a, @b, @c = a, b, c
    end
end

but an easier way is:

class A
    def initialize *args
        @a, @b, @c = args
    end
end

But I was concerned about the speed of the code since it looks like the latter code is creating an extra array args that was not created in the former code. From the point of view of the speed or efficienty, is the better to stick to the former one and not use the latter one, or is there no difference?

  • 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-30T02:05:20+00:00Added an answer on May 30, 2026 at 2:05 am

    Looks fine from here:

    RUBY_VERSION # => "1.9.3"
    
    def set_each(a,b,c)
      a2, b2, c2 = a, b, c
    end
    
    def set_ary(*args)
      a2, b2, c2 = args
    end
    
    def time(n)
      start_time = Time.now
      n.times { yield }
      Time.now - start_time
    end
    
    n = 1_000_000
    time(n) { set_each 1, 2, 3 } # => 0.301268
    time(n) { set_ary 1, 2, 3 }  # => 0.308298
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Based on constructor of the class. create or destroy new methods. I often requires
When I use static variables in C++, I often end up wanting to initialize
I have a not-so-small class under development (that it changes often) and I need
I often see constructor functions that just call an init() function. Why have an
Quite often I see people explicitly calling super() in a class that doesn't explicitly
I'm a simple programmer. My class members variables most often consists of POD-types and
I often find I have class instances that are descendants of other class instances,
I often find myself writing class constructors like this: class foo: def __init__(self, arg1,
Is there a way in C# to reference a class constructor as a standard
I have often seen an init() within the constructor of AS3 classes, sometimes even

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.