I want new Enumerable Methods, "includes, anies"

#includes
##before:
enum.each do |e|
if e == “1”
#bla~
#bla~
end
end

##after:
enum.includes(“1”). do
#bla~
#bla~
end

#anies
##before:
enum.each do |e|
if e.id == “1”
#bla~
#bla~
end
end

##after:
enum.anies(id == “1”). do
#bla~
#bla~
end

my eng is very poor.
so i’m afraid wrong-understand

hiphapis wrote:

#includes
##before:
enum.each do |e|
if e == “1”
#bla~
#bla~
end
end

##after:
enum.includes(“1”). do
#bla~
#bla~
end

Array#include? already exists with this exact functionality. I forget
if other Enumerables also have include?.

#anies
##before:
enum.each do |e|
if e.id == “1”
#bla~
#bla~
end
end

##after:
enum.anies(id == “1”). do
#bla~
#bla~
end

Use Enumerable#select.

my eng is very poor.
so i’m afraid wrong-understand

Spend some more time with the Ruby core/stdlib docs.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

On Aug 5, 10:25 pm, hiphapis [email protected] wrote:

enum.includes(“1”). do
#bla~
#bla~
end

You can also do this with:

enum.select { |e| e == “1” }.each do …

so your ‘includes’ function could be written as:

class Enumerable
def includes(o, &block)
select { |e| e == o }.each(&block)
end
end

enum.anies(id == “1”). do
#bla~
#bla~
end

I don’t think this will even parse - passing bare code fragments like
that doesn’t work in Ruby.

–Matt J.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs