Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS 

Better than methodphitamine?

Original discussion here: http://jicksta.com/articles/2007/08/04/the-methodphitamine

I think this one is prettier (but maybe not as sophisticated):

module Enumerable
  def method_missing(sym, *args)
    if (sym.to_s =~ /^(.+)_with$/) && (msg = args.shift)
      send($1.to_sym) do |x|
        x.send(msg, *args)
      end
    end
  end
end

[1, 2, 3].map_with :to_s

Enumerable#select_with_index



module Enumerable

   def select_with_index
      index = -1
      (block_given? && self.class == Range || self.class == Array)  ?  select { |x| index += 1; yield(x, index) }  :  self
   end

   def select_with_index_blk(&block)
      index = -1
      (block && self.class == Range || self.class == Array)  ?  select { |x| index += 1; block.call(x, index) }  :  self
   end

end


p ('a'..'n').select_with_index { |x, i| x if i % 2 == 0 }
p ('a'..'n').select_with_index_blk { |x, i| x if i % 2 == 0 }


require 'benchmark' 

num = 1000000

Benchmark.bm(8) do |t| 

  t.report('yield:') do 
    (0..num).select_with_index { |x, i| x } 
    #(0..num).select_with_index { |x, i| x if i % 2 == 0 } 
 end 

 t.report('block:') do
    (0..num).select_with_index_blk { |x, i| x } 
    #(0..num).select_with_index_blk { |x, i| x if i % 2 == 0 } 
 end 

end 


List Comprehensions in Ruby

Taken from: http://www.ruby-forum.com/topic/89416
Author: Phrogz

To avoid reinventing the wheel check out the built-in methods described in:
http://www.ruby-doc.org/core/classes/Array.html and
http://www.ruby-doc.org/core/classes/Enumerable.html.



#class Array 
module Enumerable
  def comprehend(&block) 
    block ? map(&block).compact : self 
  end 
end 


old_data = *(1..5)          # alternatives to the splat operator: (1..5).to_a or (1..5).collect
new_data = *(3..9)


# some reinventing going on here ...
added = new_data.comprehend { |x| x if not old_data.include?(x) } 
removed = old_data.comprehend { |x| x if not new_data.include?(x) } 
same = new_data.comprehend { |x| x if old_data.include?(x) } 
modified = new_data.comprehend { |x| x**2 if not x % 2 == 0 }

p added                                            #=> [6, 7, 8, 9]
p added = new_data - old_data                      #=> [6, 7, 8, 9]

p removed                                         #=> [1, 2]
p removed = old_data - new_data                   #=> [1, 2]

p same                                              #=> [3, 4, 5]
p same = old_data & new_data                        #=> [3, 4, 5]
 
p modified                                        #=> [9, 25, 49, 81]


new_data = [[5, 9], [22, 3], [99, 564]]
bool_vals = new_data.comprehend { |x, y| x <= y }
p bool_vals                                      #=> [true, false, true]


foldr and foldl in Ruby

module Enumerable
  def foldr(o, m = nil)
    reverse.inject(m) {|m, i| m ? i.send(o, m) : i}
  end

  def foldl(o, m = nil)
    inject(m) {|m, i| m ? m.send(o, i) : i}
  end
end

[1, 2, 3, 4, 5].foldl(:+) #=> 15
[1, 2, 3, 4, 5].foldl(:*) #=> 120

[1, 2, 3, 4, 5].foldr(:-, 0) #=> 3
[1, 2, 3, 4, 5].foldl(:-, 0) #=> -15


Advanced iterating in Ruby with 'enumerator'

require 'enumerator'

ary = [ 1, 2, 3, 4 ]

# iterate over two elements at a time
ary.each_slice(2) { |a,b| p [a, b] }

# iterate over every pair of consecutive pair of numbers
ary.each_cons(2) { |a, b| p [a, b] }

# An Enumerable::Enumerator object can be created as well,
# that mixes in Enumerable, for further processing:
ary.enum_for(:each_cons, 2).map { |a,b| a + b } # => [3, 5, 7]
« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS