0

What is a better way than this:

X = %w(a b c)
Y = %w()
  X.each do |x|
    Y << "good_" + x
  end

Thanks.

3 Answers 3

6
%w(a b c).map{|x| "good_#{x}"}
Sign up to request clarification or add additional context in comments.

Comments

3

collect method on array will do

Y = X.collect{|e|'good_'+e} 

OR

directly

Y = %w(a b c).collect{|e|'good_'+e}

Comments

1

to have them both defined on same line:

y = ( x = %w[a b c] ).map { |i| 'good_%s' % i }

y
=> ["good_a", "good_b", "good_c"]

x
=> ["a", "b", "c"]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.