0

I have a following array

Bot = Struct.new(:name, :age)

bots = %w(foo bar baz).map do |name|
  Bot.new(name, rand(10))
end

p bots
[ #<struct Bot name="foo", age=3>, 
  #<struct Bot name="bar", age=8>, 
  #<struct Bot name="baz", age=0> ]

I want to get a new array from bots, where age attribute converted to_s, but I don't want to change the real objects in the array bots. How can I do this?

2 Answers 2

1

This will preserve bots:

new_bots = bots.map {|bot| Bot.new(bot.name, bot.age.to_s) }

This will not preserve bots:

new_bots = bots.map! {|bot| Bot.new(bot.name, bot.age.to_s) }

map! modifies the object it is called on, as do most methods that end in !. It is the mutable version of map.

map does not modify the contents of the object it is called on. Most array methods are immutable, except those that end in ! (but is only a convention, so be careful).

Sign up to request clarification or add additional context in comments.

Comments

1
Bot = Struct.new(:name, :age)

bots = %w(foo bar baz).map do |name|
  Bot.new(name, rand(10))
end
#=> [#<struct Bot name="foo", age=4>,
#    #<struct Bot name="bar", age=5>,
#    #<struct Bot name="baz", age=8>]

bots.map { |bot| Bot.new(bot.name, bot.age.to_s)}
#=> [#<struct Bot name="foo", age="4">,
#    #<struct Bot name="bar", age="5">,
#    #<struct Bot name="baz", age="8">]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.