FAQ: Blocks, Procs, and Lambdas - Why Procs?

This community-built FAQ covers the “Why Procs?” exercise from the lesson “Blocks, Procs, and Lambdas”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Ruby

FAQs on the exercise Why Procs?

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Hi there!
I got stuck with this exercise.

group_1 = [4.1, 5.5, 3.2, 3.3, 6.1, 3.9, 4.7]

group_2 = [7.0, 3.8, 6.2, 6.1, 4.4, 4.9, 3.0]

group_3 = [5.5, 5.1, 3.9, 4.3, 4.9, 3.2, 3.2]

over_4_feet = Proc.new do |height|

height >= 4

end

print ints = group_1.select(&over_4_feet)

print ints = group_2.select(&over_4_feet)

print ints = group_3.select(&over_4_feet)

I’am able to run it but I can find the mistake that won’t let me pass the exercise.
Thanks in advance.

Not sure that Ruby permits an assignment in a print statement. The three groups could be getting their own variable.

can_ride_1 = group_1.select(&over_4_feet)
puts can_ride_1
# etc.
1 Like

Hi, yes! I’m getting the same and my code looks exactly like the one that the “Solution” posted. I keep getting a question saying "Did you put a “Proc.new” block. even though i have. I’ve also tried to change “puts” into “print” like mtf replied but that didn’t do anything. Also, in the Solution, puts stayed as puts. I’m getting a syntax error in the console for all three can_ride_ groups.
the image is my code

Hey there @katrinaamorroblesros, welcome to the forums!

The syntax doesn’t look quite right.
Are you sure that you want to use curly braces with your .select method?

3 Likes

:weary:

AH! My eyes are getting crossed. Thank you !

1 Like

I was doing this excercise, but I still don’t get the need for proc as the problem is perfetly solvable with method as well … See the method test I have written to copy functionality of proc

group_1 = [4.1, 5.5, 3.2, 3.3, 6.1, 3.9, 4.7]
group_2 = [7.0, 3.8, 6.2, 6.1, 4.4, 4.9, 3.0]
group_3 = [5.5, 5.1, 3.9, 4.3, 4.9, 3.2, 3.2]

# Complete this as a new Proc
over_4_feet = Proc.new {
  |h| h >= 4
}

# this one is using a method and it work perfetly, why is ther a need for proc?
def test(h)
  return h >= 4
end

can_ride_1 = group_1.select { |x| test(x) }

# Change these three so that they use your new over_4_feet Proc
can_ride_1 = group_1.select(&over_4_feet)
can_ride_2 = group_2.select(&over_4_feet)
can_ride_3 = group_3.select(&over_4_feet)

puts can_ride_1
puts can_ride_2
puts can_ride_3

class Proc - Documentation for Ruby 2.0.0

looks like its not working

solution was exactly the same, why does it do this?

A do loop requires an end keyword.

do |x|
  # something x
end
1 Like

The idea is to make it clear, right?
So we can skip the step of defining can_ride_1 before puts.

Example, instead of

can_ride_1 = group_1.select(&over_4_feet)

puts can_ride_1

You can only place:

puts group_1.select(&over_4_feet)

here is the solution I found:

group_1 = [4.0, 5.5, 3.2, 3.3, 6.1, 3.9, 4.7]
group_2 = [7.0, 3.8, 6.2, 6.1, 4.4, 4.9, 3.0]
group_3 = [5.5, 5.1, 3.9, 4.3, 4.9, 3.2, 3.2]

over_4_feet = Proc.new { |n| n >= 4 }

puts group_1.select(&over_4_feet)
puts group_2.select(&over_4_feet)
puts group_3.select(&over_4_feet)

Although we would miss the demonstration of assignment, you make a good point. When the variable is never going to be used more than the one time, it is better memory management to print the expression, directly.

Nice that you pointed it out. Better to turn it into a variable whenever it would be used more than once :slight_smile:

1 Like

you forgot the =====> end

Can’t get this one to work. Any ideas?

(have gone through solutions above and don’t think they apply to mine)

Also codecademy’s ‘code solution’ isn’t working for this one. It just shows the original unedited code.

Ah i worked it out. It was the gap between .select and the brackets!

1 Like

Solution:

Here at the amusement park, you have to be four feet tall

or taller to ride the roller coaster. Let’s use .select on

each group to get only the ones four feet tall or taller.

group_1 = [4.1, 5.5, 3.2, 3.3, 6.1, 3.9, 4.7]

group_2 = [7.0, 3.8, 6.2, 6.1, 4.4, 4.9, 3.0]

group_3 = [5.5, 5.1, 3.9, 4.3, 4.9, 3.2, 3.2]

Complete this as a new Proc

over_4_feet = Proc.new do |h| h>=4

end

Change these three so that they use your new over_4_feet Proc

can_ride_1 = group_1.select(&over_4_feet)

can_ride_2 = group_2.select(&over_4_feet)

can_ride_3 = group_3.select(&over_4_feet)

puts can_ride_1

puts can_ride_2

puts can_ride_3