1

I've been stuck on this problem. I'd appreciate any help.

The instructions "Use the .each method on the odds array to print out double the value of each item of the array. In other words, multiply each item by 2.

Make sure to use print rather than puts, so your output appears on one line."

My code

odds = [1,3,5,7,9]

odds.each do |placeholder|
    odds *= 2
    print odds
end
2
  • print placeholder, not odds Commented Jul 9, 2015 at 18:55
  • Try inserting puts "placeholder=#{placeholder]" as the first line of the block. Commented Jul 9, 2015 at 19:24

5 Answers 5

5
odds = [1,3,5,7,9]
odds.each do |placeholder|
  odds *= 2
  print odds
end

Your usage of #each is correct here, but remember that #each receives an ::Enumerable object, the block passed in uses the variable placeholder to encapsulate the value in the ::Array at the current point of the iteration. Therefore you would need to use placeholder to retrieve the value you want to double, and not odds because odds would still be an ::Array within the ::Enumerable function #each.

This code can be written in two lines as follows:

odds = [1,3,5,7,9]
odds.each {|placeholder| print placeholder * 2 }

Strictly speaking, #map would be the preferred method for doing this.

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

3 Comments

Why do you (and other posters) think map is to be preferred? I don't agree. [1,3,5,7,9].map{|placeholder| print placeholder * 2 } creates a useless array of nils, which is referenced by nothing . The whole purpose of map is to create an array - but in this case nobody asked for an array. each is fine.
@steenslag this would work nicely though print *odds.map{|x| x * 2} though I do agree since the OP said using each although I guess technically pretty much all Enumerable methods use each
ruby-doc.org/core-2.2.0/Array.html Refer to the ::Array #map function in ruby docs.
3
odds.each { |x| print "#{x*2}" }

ruby-ish way:

print odds.map {|x| x*2}.join(' ')

2 Comments

each is not needed for map. each will create an Enumerator which will allow map to work because Enumerator includes the Enumberable module but since Array also includes Enumerable just map would be sufficient.
awesome. :) even better
1

You are trying to multiply the Array odds by two in each iteration in your code. The do |placeholder| means 'for each item in the array, give me the single element and call it placeholder'.

So you would need to use placeholder *= 2.

However, since you aren't mutating the original value in odds, you can shorten your inner block by just print placeholder * 2.

Additionally, while the question might be saying to use :each, :map is much more canonical to what you are doing. Using map would allow you to double each element like so:

odds.map{ |i| i*2 }

Comments

0

odds is your array. placeholder represents a value from the array. Without explicitly providing you the answer, what you want to do is multiply the value by 2, not the array itself.

Comments

0

Here Iam done that on succesfully

odds = [1,3,5,7,9]
odds.each do |x|
    x *= 2
    print x
end

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.