Browse files

add &block option to set_mag Vec2D

  • Loading branch information...
1 parent 9c67868 commit 3b4c4e68f04c5c6847bdeb65e9dcfecd87731dbe @monkstone monkstone committed Mar 6, 2014
Showing with 8 additions and 2 deletions.
  1. +8 −2 library/vecmath/lib/vec.rb
View
10 library/vecmath/lib/vec.rb
@@ -112,8 +112,14 @@ def normalize!
@x, @y = x / magnitude, y / magnitude
return self
end
-
- def set_mag(scalar)
+
+ # The &block option means we can conditionally set_mag to
+ # a limit value for example, when given &block returns true.
+
+ def set_mag(scalar, &block)
+ if block_given? && yield == false
+ return self
+ end
magnitude = Math.hypot(x, y)
@x, @y = (x * scalar) / magnitude, (y * scalar) / magnitude
return self

0 comments on commit 3b4c4e6

Please sign in to comment.