Skip to main content
edited for grammar and readability
Source Link
the Tin Man
  • 160.9k
  • 44
  • 222
  • 308

Enumerable has a grep method whose first argument can be a predicate proc, and whose optional second argument is a mapping function; so the following works:

some_array.grep(proc {|x| x % 2 == 0}) {|x| x*3} 

This isn't as readable as a couple of other suggestions (I like anoiaque's simple .select.mapselect.map or histocrat's comprehend gem), but its strengths are that it's already part of the standard library, and is single-pass (doesn'tand doesn't involve creating temporary intermediate arrays), and doesn't require an out-of-bounds value like nil used in the compact-using suggestions.

Enumerable has a grep method whose first argument can be a predicate proc, and whose optional second argument is a mapping function; so the following works:

some_array.grep(proc {|x| x % 2 == 0}) {|x| x*3} 

This isn't as readable as a couple of other suggestions (I like anoiaque's simple .select.map or histocrat's comprehend gem), but its strengths are that it's already part of the standard library, and is single-pass (doesn't involve creating temporary intermediate arrays), and doesn't require an out-of-bounds value like nil used in the compact-using suggestions.

Enumerable has a grep method whose first argument can be a predicate proc, and whose optional second argument is a mapping function; so the following works:

some_array.grep(proc {|x| x % 2 == 0}) {|x| x*3} 

This isn't as readable as a couple of other suggestions (I like anoiaque's simple select.map or histocrat's comprehend gem), but its strengths are that it's already part of the standard library, and is single-pass and doesn't involve creating temporary intermediate arrays, and doesn't require an out-of-bounds value like nil used in the compact-using suggestions.

Grep's argument can indeed be a predicate proc rather than requiring creating a class with === method
Source Link

Enumerable does havehas a grep method, but it wants an object with a ‘=== method instead ofwhose first argument can be a predicate function. So another possibilityproc, and whose optional second argument is a mapping function; so the following works:

some_array.grep((Class.newproc { def ===(x)|x| x % 2 == 0 end }).new) {|x| x*3} 

Although ugly in syntaxThis isn't as readable as a couple of other suggestions (I like anoiaque's simple .select.map or histocrat's comprehend gem), this versionbut its strengths are that it's already part of the standard library, and is single-pass (doesn't involve creating temporary intermediate arrays), and doesn't require an out-of-bounds value like nil used in the compact-using suggestions.

Today's my first day learning Ruby; please improve or point out problems with this answer if you can.

Enumerable does have a grep method, but it wants an object with a ‘=== method instead of a predicate function. So another possibility is:

some_array.grep((Class.new { def ===(x) x % 2 == 0 end }).new) {|x| x*3} 

Although ugly in syntax, this version is single-pass (doesn't involve creating temporary intermediate arrays), and doesn't require an out-of-bounds value like nil used in the compact-using suggestions.

Today's my first day learning Ruby; please improve or point out problems with this answer if you can.

Enumerable has a grep method whose first argument can be a predicate proc, and whose optional second argument is a mapping function; so the following works:

some_array.grep(proc {|x| x % 2 == 0}) {|x| x*3} 

This isn't as readable as a couple of other suggestions (I like anoiaque's simple .select.map or histocrat's comprehend gem), but its strengths are that it's already part of the standard library, and is single-pass (doesn't involve creating temporary intermediate arrays), and doesn't require an out-of-bounds value like nil used in the compact-using suggestions.

Source Link

Enumerable does have a grep method, but it wants an object with a ‘===’ method instead of a predicate function. So another possibility is:

some_array.grep((Class.new { def ===(x) x % 2 == 0 end }).new) {|x| x*3} 

Although ugly in syntax, this version is single-pass (doesn't involve creating temporary intermediate arrays), and doesn't require an out-of-bounds value like nil used in the compact-using suggestions.

Today's my first day learning Ruby; please improve or point out problems with this answer if you can.