Skip to content

Instantly share code, notes, and snippets.

Created September 20, 2014 20:30
Show Gist options
  • Save anonymous/7071735bf5b8e29bcbd1 to your computer and use it in GitHub Desktop.
Save anonymous/7071735bf5b8e29bcbd1 to your computer and use it in GitHub Desktop.
finally :)
odds = [1,3,5,7,9]
odds.each do |x|
x *= 2
print "#{x}"
end
@Phrogz
Copy link

Phrogz commented Sep 20, 2014

You can also:

# option 1
odds.each do |x|
  x = x*2
  print "#{x}"
end

# option 2
odds.each do |x|
  print "#{x*2}"
end

#option 3
odds.each do |x|
  print x*2
end

#option 4
odds.each{ |x| print x*2 }

@Phrogz
Copy link

Phrogz commented Sep 20, 2014

However, if you:

odds.each{ |x| print "x*2" }

then you are going to see the output:

x*2x*2x*2x*2x*2x*2x*2x*2

because you are asking it to print a string, a literal value, NOT the result of looking up the variable x and multiplying it by 2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment