Permalink
Browse files

Add some methods from HoboSupport.

  • Loading branch information...
1 parent 5562acc commit cd325f345d9e220ace040f650c20bb7272f9cfee @jgoizueta committed Apr 30, 2012
View
@@ -6,10 +6,13 @@
require 'modalsupport/enumerable'
require 'modalsupport/file'
require 'modalsupport/array'
+require 'modalsupport/hash'
+require 'modalsupport/basic_object'
module ModalSupport
end
require 'modalsupport/recursive_map'
require 'modalsupport/mixins/state_equivalent'
require 'modalsupport/mixins/bracket_constructor'
+require 'modalsupport/methodcall'
@@ -0,0 +1,24 @@
+module ModalSupport
+ if defined? ActiveSupport::BasicObject
+ BasicObject = ActiveSupport::BasicObject
+ elsif defined? ::BasicObject
+ class BasicObject < ::BasicObject
+ undef_method :==
+ undef_method :equal?
+ def raise(*args)
+ ::Object.send(:raise, *args)
+ end
+ end
+ else
+ class BasicObject #:nodoc:
+ begin
+ old_verbose, $VERBOSE = $VERBOSE, nil # just like Rails silence_warnings: suppress "warning: undefining `object_id' may cause serious problem"
+ instance_methods.each do |m|
+ undef_method(m) if m.to_s !~ /(?:^__|^nil\?$|^send$|^object_id$)/
+ end
+ ensure
+ $VERBOSE = old_verbose
+ end
+ end
+ end
+end
@@ -44,27 +44,27 @@ def each_product_pair(other)
# Paralell iteration through multiple enumerable objects
# (1..3).paralell_each([:a,:b,:c],(10..12)).to_a
- # => [[1, :a, 10], [2, :b, 11], [3, :c, 12]]
- #
+ # => [[1, :a, 10], [2, :b, 11], [3, :c, 12]]
+ #
# e.paralell_each(*enums) = e.zip(*enums).each
def paralell_each(*enums, &blk)
# Note that to implement a lazy iterator we'd need
# external iterators which don't work in Rubinius, are
# implemented differently in Ruby < 1.8.7 and are slow.
# So, for the time being we generate an array and then
- # iterate it.
+ # iterate it.
if block_given?
zip(*enums).each(&blk)
else
enum_for(:paralell_each, *enums)
end
end
-
+
# Equivalent to paralell_each(*enums).to_a, but more efficient
def paralell_array(*enums)
zip(*enums)
end
-
+
# Cross-product iteration through multiple enumerable objects
# (1..4).cross_each([:a,:b,:c],(11..12)).to_a
# => [[1, :a, 11], [1, :a, 12], [1, :b, 11], [1, :b, 12], [1, :c, 11], [1, :c, 12],
@@ -89,9 +89,9 @@ def cross_each(*enumerables)
enum_for(:cross_each, *enumerables)
end
end
-
+
# equivalent to cross_each(*enumerables).to_a, but more efficient
- def cross_array(*enumerables)
+ def cross_array(*enumerables)
# return to_a.product(*enumerables.map{|e| e.to_a})
enumerables.unshift self
result = [[]]
@@ -106,6 +106,34 @@ def cross_array(*enumerables)
end
result
end
-
+
+ unless defined? ::HoboSupport
+
+ def map_with_index(res=[])
+ each_with_index {|x, i| res << yield(x, i)}
+ res
+ end
+
+ def build_hash(res={})
+ each do |x|
+ pair = block_given? ? yield(x) : x
+ res[pair.first] = pair.last if pair
+ end
+ res
+ end
+
+ def map_hash(res={})
+ each do |x|
+ v = yield x
+ res[x] = v
+ end
+ res
+ end
+
+ def rest
+ self[1..-1] || []
+ end
+
+ end # Hobosupport
end
@@ -1,4 +1,3 @@
-require 'modalsupport'
begin
require 'active_support'
rescue LoadError
@@ -9,3 +8,4 @@
rescue LoadError
puts "HoboSupport not available. Install it with: gem install hobosupport"
end
+require 'modalsupport'
@@ -0,0 +1,14 @@
+unless defined? ::HoboSupport
+
+ class Hash
+
+ def map_hash(&b)
+ res = {}
+ each {|k,v| res[k] = b.arity == 1 ? yield(v) : yield(k, v) }
+ res
+ end
+
+ end
+
+
+end
@@ -0,0 +1,31 @@
+# HoboSupport-style ._?
+# ._? calls a method if the receiver is not nil, returns nil
+# otherwise.
+require 'singleton'
+
+unless nil.respond_to?(:_?)
+
+ class Object
+ def _?()
+ self
+ end
+ end
+
+ class NilClass
+ def _?()
+ ModalSupport::SafeNil.instance
+ end
+ end
+
+ module ModalSupport
+
+ class SafeNil < ModalSupport::BasicObject
+ include ::Singleton
+ def method_missing(method, *args, &b)
+ return nil
+ end
+ end
+
+ end
+
+end
@@ -0,0 +1,14 @@
+require 'helper'
+
+class TestBuildHash < Test::Unit::TestCase
+
+ context 'Enumerable#build_hash' do
+
+ should "returns a hash where each element is given by the block unless nil" do
+ assert_equal({'some'=>4, 'words'=>5}, %w(some short words).build_hash { |s| [s, s.length] unless s == "short" })
+ assert_equal({10=>3, 20=>6, 30=>9}, (1..3).build_hash{|v| [v*10,v*3]})
+ end
+
+ end
+
+end
View
@@ -0,0 +1,29 @@
+require 'helper'
+
+class TestMapHash< Test::Unit::TestCase
+
+ context 'Enumerable#map_hash' do
+
+ should "returns a hash where keys are the enumerated elements and the values are given by the block" do
+ assert_equal({'some'=>4, 'words'=>5, 'short'=>nil}, %w(some short words).map_hash { |s| s.length unless s == "short" })
+ assert_equal({1=>3, 2=>6, 3=>9}, (1..3).map_hash{|v| v*3})
+ end
+
+ end
+
+ context 'Hash#map_hash' do
+
+ should "applies a function with the key and value as arguments to each value of the hash" do
+ assert_equal({1=>true, 3=>true, 6=>false}, {1=>2, 3=>4, 6=>5}.map_hash { |key, value| key < value })
+ assert_equal({1=>3, 2=>6, 3=>9}, (1..3).map_hash{|v| v*3})
+ end
+
+ should "applies a function with the value as arguments to each value of the hash" do
+ assert_equal({1=>true, 3=>true, 6=>false}, {1=>2, 3=>4, 6=>5}.map_hash { |key, value| key < value })
+ assert_equal({1=>200, 3=>400, 6=>500 }, {1=>2, 3=>4, 6=>5}.map_hash { |value| value * 100 })
+ end
+
+ end
+
+
+end
@@ -0,0 +1,13 @@
+require 'helper'
+
+class TestMapWithIndex< Test::Unit::TestCase
+
+ context 'Enumerable#map_with_index' do
+
+ should "maps elements passing also de index to the block" do
+ assert_equal ["", "short", "wordswords"], %w(some short words).map_with_index { |s, i| s * i }
+ end
+
+ end
+
+end
@@ -0,0 +1,34 @@
+require 'helper'
+
+class TestMethodcall < Test::Unit::TestCase
+
+ context "Given a non-nil object" do
+
+ should "Return itself from ._?" do
+ assert_same 3, 3._?
+ o = "xxx"
+ assert_same o, o._?
+ o = Object.new
+ assert_same o, o._?
+ o = false
+ assert_same o, o._?
+ o = 0
+ assert_same o, o._?
+ end
+
+ end
+
+ context "Given a nil object" do
+
+ should "Return something that returns nil to any message" do
+
+ assert_nil nil._?.to_s
+ assert_nil nil._?.inspect
+ assert_nil nil._?.xxxx
+
+ end
+
+ end
+
+
+end
View
@@ -0,0 +1,16 @@
+require 'helper'
+
+class TestRest < Test::Unit::TestCase
+
+ context 'Enumerable#rest' do
+
+ should "returns the rest of elements having removed the first one" do
+ assert_equal [2,3,4,5], [1,2,3,4,5].rest
+ assert_equal [2], [1,2].rest
+ assert_equal [], [1].rest
+ assert_equal [], [].rest
+ end
+
+ end
+
+end

0 comments on commit cd325f3

Please sign in to comment.