RUBY 18.18 Add a block after .each that multiplies each item by itself- Trying to understand how it works


my_array = [1, 2, 3, 4, 5]
The problem is to multiply each number by itself

And gives this as a template
numbers = [5, 2, 8]
sum = 0
numbers.each do |n|
sum += n
puts sum

After an hour I finally just looked up the answer

Replace this line with your code. 

my_array = [1, 2, 3, 4, 5]

my_array.each do |x|
x = x**2
puts x

Using the format provided, how the heck was I supposed to know to use the do function? I have seen other ways of solving the problem, but none of them match the format given as an outline above. I am wondering how I could have known to solve this using the format provided.

In all the solutions I found, none followed this format. And the hint given is: object.method { |placeholder| action }
This does not even pertain to the above answer, which does not have the "action". I am wondering if this is poor layout on Code Academy's part, or just me.


One thing is for sure...
you will never forget this piece of ruby-syntax

You could google at the site
( the site is used by a lot of programmers )

== discussions / opinions ==
ruby do end vs braces
read some of the article's...


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.