4 Replies - 1173 Views - Last Post: 14 May 2014 - 12:30 PM Rate Topic: -----

#1 konami_code  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 11-May 14

I am see my solution but do not understand the code

Posted 14 May 2014 - 11:51 AM

I have this practice code I am working with from loops & Iterators, I am working through this problem and I am getting the solution as below as I have also commented:

#variable = [ array ].
odds = [1,3,5,7,9]

#each number is to be multiplied by 2, Then the multiplied numbers are shown.
odds.each do |x|
    x *= 2
    print "#{x}"
end



I am seeing most of this and getting it. However, the "||" confused me. where
odds.each do |x|
I understand
odds.each do
but the
|x|
, I am not sure what I am declaring, is it telling each number to be first sent through the x before print? So I declare
|x|
then show the computer to make
x *= 2
which I am assuming in English is saying that "x" will be a multiplier of two. So then when
print "#{x}"
is stated, I am gathering that it is now going to output the odd numbers after they have been multiplied by the
x *= 2
. the output is also confusing:

26101418[1, 3, 5, 7, 9]


I see the end result of the
x *= 2
but then it also shows [1, 3, 5, 9] I can't understand why it also displayed this.

Thank you for any help in understanding this. I want to really get it before I decided to move forward.

Is This A Good Question/Topic? 0
  • +

Replies To: I am see my solution but do not understand the code

#2 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 916
  • View blog
  • Posts: 3,208
  • Joined: 12-May 09

Re: I am see my solution but do not understand the code

Posted 14 May 2014 - 12:06 PM

You use || to show the placeholder variables that are used during the iteration. Each time we iterate over the loop, a value from the array is placed into x. x takes on the values from the array in each, first 1, then 3, then 5, 7, and 9.

The return value of the each loop is probably the item it iterates over, so if you're running this in irb, it shows the output from the prints and then it outputs the "value" of the expressions you ran.
Was This Post Helpful? 2
  • +
  • -

#3 konami_code  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 11-May 14

Re: I am see my solution but do not understand the code

Posted 14 May 2014 - 12:16 PM

Ok this really helps me understand this. Thank you, So what I am seeing here then is the line
odds.each do |x|
its saying, take each number from the odds variable and do whatever x is equating to which is to multiply each number by 2 before its printed. is that correct?
Was This Post Helpful? 0
  • +
  • -

#4 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 916
  • View blog
  • Posts: 3,208
  • Joined: 12-May 09

Re: I am see my solution but do not understand the code

Posted 14 May 2014 - 12:28 PM

That line is saying:
iterate over odds, setting the value of x to an element of odds each time. The following lines of code will be executed during each iteration, with x available to them.
Was This Post Helpful? 2
  • +
  • -

#5 konami_code  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 11-May 14

Re: I am see my solution but do not understand the code

Posted 14 May 2014 - 12:30 PM

Thank xclite, This makes sense I will to play around with it till I feel comfortable with this. Thanks again!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1