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


#1



https://www.codecademy.com/en/courses/ruby-beginner-en-ET4bU/4/3?curriculum_id=5059f8619189a5000201fbcb

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
end
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
end

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.


#2

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

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

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


#3

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