#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
Marnen Laibow-Koser
2009-Aug-06 03:09 UTC
Re: i want new Enumerable Methods, "includes, anies"
hiphapis wrote:> #includes > ##before: > enum.each do |e| > if e == "1" > #bla~ > #bla~ > end > end > > ##after: > enum.includes("1"). do > #bla~ > #bla~ > endArray#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~ > endUse Enumerable#select.> > > > my eng is very poor. > so i''m afraid wrong-understandSpend some more time with the Ruby core/stdlib docs. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.
On Aug 5, 10:25 pm, hiphapis <hipha...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> #includes > ##before: > enum.each do |e| > if e == "1" > #bla~ > #bla~ > end > end > > ##after: > 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> #anies > ##before: > enum.each do |e| > if e.id == "1" > #bla~ > #bla~ > end > end > > ##after: > 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 Jones