I think pulling from arrays with some filters in Ruby is nice, such as
[1, 2, 3, 4, 2, 3, 1, 3, 4, 5].uniq.compact.select(&:even?)
uniq removes dupes and compact removes nil and .select(&:even?) will limit the output to even numbers.
uniq
compact
nil
.select(&:even?)
what is the syntax &:even??
&:even?
I've heard it called a "symbol to proc" or "symbol proc" and it's short for this:
.select { |foo| foo.even? } # same as .select(&:even?)
It's calling the method even? on each number passed in. Behind the scenes it creates a proc and then passes in each number. Here are the ruby docs for Symbol#to_proc and here's a pretty decent break down of symbol to proc.
even?
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I think pulling from arrays with some filters in Ruby is nice, such as
uniqremoves dupes andcompactremovesniland.select(&:even?)will limit the output to even numbers.what is the syntax
&:even??I've heard it called a "symbol to proc" or "symbol proc" and it's short for this:
It's calling the method
even?on each number passed in. Behind the scenes it creates a proc and then passes in each number. Here are the ruby docs for Symbol#to_proc and here's a pretty decent break down of symbol to proc.