18.Creating a Proc


#1

Hi,

Could someone tell me, why this code isn't working?

    ages = [23, 101, 7, 104, 11, 94, 100, 121, 101, 70, 44]


under_100 = Proc.new do |x|
    if x < 100
        puts x
    end
end

ages.select(&under_100)

The result is: 
23
7
11
94
70
44
[]

Soit should be fine, but I get error:
"Oops, try again.
It looks like your proc doesn't check whether a number it's passed is < 100. Check the Hint if you need help!"


#2

Probably because you're not returning a boolean to select, you're printing a value instead.

The goal of select is not to apply some code to each element, the goal of select is to return an array with only the elements corresponding to the criteria.

If you really want to print the selection, then you can do it outside of the proc.


#3

We are supposed to handle select in the next part of the section, correct?
Also, I didn't exactly use an if statement, and more of a one-liner


#4

You just have to use Proc here to check for the values <100.Getting the output to the screen is in the next section.


#5

Alright, since some of us learn by seeing here is the code I used to finish this step.
I assume ages.select runs through each array element. And then you just compare
the ages.select to 100. WAM!!!

under_100 = Proc.new {ages.select < 100}

Oh yeah don't forget because it's a proc to have the Proc.new. Which to me is just
having a method in an assignment. I am very confused with Procs and Lambdas.
They seem to do the exact same things.


#6

Odd question. Why is it when I go to section 19 this code doesn't work.
I entered

under_100 = Proc.new {|x| x < 100}

to finally get the code to work the way I wanted it to. With the ending

youngsters = ages.select(&under_100)

to finish it off.


#7

For this exercise, I just put:

ages = [23, 101, 7, 104, 11, 94, 100, 121, 101, 70, 44]
under_100 = Proc.new { |x| x<100 }

I've seem some replies mention using .select to complete this exercise; however that is used in the next exercise, where you add the line:

youngsters = ages.select(&under_100)


#8

can u post the full code?


#9

im just using
under_100 = Proc.new do
under_100 < 100
end
and that is very simple and right


#10

ages = [23, 101, 7, 104, 11, 94, 100, 121, 101, 70, 44]

Add your code below!

under_100 = Proc.new do |x|
if x < 100
ages.select{|x| x<100}
end
end

ages.select(&under_100)

this will work