19 Passing your Proc to a Method


#1

Hello,

I end up with two solutions to the following problem, but I have a problem understanding why one works and the other one doesn't, and the only difference is [ ] and ( ) at end.

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

under_100 = Proc.new { |x| x < 100 }
youngsters = ages.select(&under_100)

and

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

under_100 = Proc.new { |x| x < 100 }
youngsters = ages.select[&under_100]

Code is the same just the parentesis at the end is different, and the first code seems to work but the 2nd doesn't.


#2

Because it's how Ruby's syntax works, methods want either () or a block after them (or another method).

[] are used for arrays (as your code shows in ages).


#3

The "holders" have great importance. { and } are like do and end. and [] brackets are to hold an array


#4

under_100 = proc.new { |x| true if x < 100 }
youngsters = ages.select(&under_100)

do not put any space after ages.select


#5

Also, make sure to capitalize Proc