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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:48:22+00:00 2026-06-01T14:48:22+00:00

The method in question that I want to use is gem and sourced here

  • 0

The method in question that I want to use is gem and sourced here (lines 17-42): https://github.com/rails/rails/blob/master/railties/lib/rails/generators/actions.rb

As you can see, name is assigned to the first arg on line 19, then message is assigned to name on line 23 and finally message is mutated on line 26 with <<. This unfortunately means that the string I am passing in as the first argument is mutated outside of the method.

I have a hash of arrays and am iterating over them as follows:

groups = { foo: %w(foo, bar), bar: %w(foobar) }

groups.each do |group, gems|
  gems.each do |name|
    gem(name, "42")
  end
end

Afterwards my hash looks like this due to the mutation inside of gem:

groups => { foo: ["foo (42)", "bar (42)"], bar: ["foobar (42)"] }

How can I prevent these strings (and the hash and its arrays) from being mutated but without breaking the method?

  • 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-06-01T14:48:23+00:00Added an answer on June 1, 2026 at 2:48 pm

    You may call it with name.dup:

    gem(name.dup, "42")
    

    Background: With gem(name) you pass the parameter to the method. Any modification inside the called method, will change also the the original variable.

    With name.dup you make a copy of the object. This copy is modified inside the called method, but the original value is unchanged.


    A warning: dup does not work always, it is depending on the data. dup doesn’t make a deep copy. See this example:

    arr = ['a', 'b']
    arr.dup.map{|x| x << '1'}
    p arr #["a1", "b1"]
    

    Explanation: The array arr is copied, but not the content inside the array.
    Inside the map you modify the data of the copied array. But the elements of the original and the copied array are the same. So you also change the content of the original array.

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

Sidebar

Related Questions

Here is the question: write a method that swaps two variables. These two variables
Just a quick newbie question here. I have a method that calculates a value
I am using the linkedin gem https://github.com/pengwynn/linkedin I authorize using Omniauth and store the
I have a UI question that troubled me on the best method to handle
Fairly simple question: I have an init method on my class that has the
Sort of a methods/best practices question here that I am sure has been addressed,
Here is my dilemma. I have a collection of entities that I want to
I want to use declarative security to guarantee that my app is only run
My question is about controller methods (possibly included from an outside class) that work
i have a quetion about MVC routing. I have a controller method that is

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.