Skip to content

Commit 8a5ad01

Browse files
committed
add block option to min and max
1 parent a258a7b commit 8a5ad01

2 files changed

Lines changed: 18 additions & 4 deletions

File tree

lib/jruby_art/helper_methods.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,17 @@ def thread(&block)
7777
# explicitly provide 'processing.org' min instance method
7878
# to return a float:- a, b and c need to be floats
7979

80-
def min(*args)
81-
args.min # { |a,b| a <=> b } optional block not reqd
80+
def min(*args, &block)
81+
args.min unless block_given?
82+
args.min(&block) # { |a, b| a.value <=> b.value }
8283
end
8384

8485
# explicitly provide 'processing.org' max instance method
8586
# to return a float:- a, b and c need to be floats
8687

87-
def max(*args)
88-
args.max # { |a, b| a <=> b } optional block not reqd
88+
def max(*args, &block)
89+
args.max unless block_given?
90+
args.max(&block) # { |a, b| a.value <=> b.value }
8991
end
9092

9193
# explicitly provide 'processing.org' dist instance method

test/helper_methods_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
Dir.chdir(File.dirname(__FILE__))
1414

1515
class HelperMethodsTest < Minitest::Test
16+
17+
ARRAY = %w(albatross dog horse)
1618
def test_hex_color
1719
col_double = 0.5
1820
hexcolor = 0xFFCC6600
@@ -47,4 +49,14 @@ def test_dist
4749
ax, ay, bx, by, cx, cy = 0, 0, 1.0, 0.0, 0, 0
4850
assert_in_epsilon(dist(ax, ay, bx, by, cx, cy), 1.0, epsilon = 0.0001, msg = 'when x and z dimension are zero')
4951
end
52+
53+
def test_min
54+
assert_equal(min(*ARRAY), 'albatross')
55+
assert_equal(min(*ARRAY) { |a, b| a.length <=> b.length }, 'dog')
56+
end
57+
58+
def test_max
59+
assert_equal(max(*ARRAY), 'horse')
60+
assert_equal(max(*ARRAY) { |a, b| a.length <=> b.length }, 'albatross')
61+
end
5062
end

0 commit comments

Comments
 (0)