7 . Proc Syntax


#1

does anyone know what is wron with my code:

floats = [1.2, 3.45, 0.91, 7.727, 11.42, 482.911].round_down
round_down = Proc.new do |x|
x.floor
end

ints = floats.collect(&round_down)

on the editor turns the following:
Oops, try again. Your code doesn't look quite right. Check out the examples in the instructions if you need help!

Thanks


#2

you got .roud_down at the top next to your float


#3

I may have cheated ( I'll admit: I'm a ruby nooby)
mine went like this

round_down = Proc.new {|i| i.to_i}


#4

My code got passed by putting everything in the curly brackets after Proc.new. I find that way a little more easy to digest.

Instead of having Proc.new with the do function, I did it like this:

round_down = Proc.new { |n| n.floor }


#5

on line one you don't the .round_down but the rest of your code is fine


#6

floats = [1.2, 3.45, 0.91, 7.727, 11.42, 482.911]

round_down = Proc.new { |x| x.floor }

ints = floats.collect(&round_down)


RESULT ==> [1, 3, 0, 7, 11, 482]


#7

me too... much easier to understand and type


#8

that is helpfull
aaaaaaaaaaaaaaaaaaaaaaaaaa


#9

I've gotten this to pass, but I'm not sure I understand exactly why. I added comments to the code to show how I understand it, but please forgive me if they aren't in Rubyspeak. Any feedback or clarifications would be appreciated!

floats = [1.2, 3.45, 0.91, 7.727, 11.42, 482.911]
# Write your code below this line!

round_down = Proc.new { |i| i.floor }
# Creates round_down proc, which creates an |i| placeholder - 
# which isn't yet associated with the "floats" array, and runs the .floor method 
# (rounding down non-integers to the nearest integer) on each i.

ints = floats.collect(&round_down)
# Creates a new "ints" array by collecting the entire "floats" array 
# and passing it the round_down proc as an argument 
# (which does the work of assigning each element in the new "ints" array 
# the "i" variable and running the .floor method on each i).

#10

This is a really good breakdown of what's happening here. Procs and blocks are two of the most confusing aspects of Ruby, so it's awesome that you're trying to understand the why of what's happening here as well as the how.

Over time, you'll learn more about procs when you come across practical examples of their usage, but for the moment, this is a perfect understanding to move forward with!


#11

Proc do not have "end" . That's syntax error ! . Try this hope it will help