Skip to content

Instantly share code, notes, and snippets.

@phluid61
Last active December 27, 2015 13:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phluid61/7332168 to your computer and use it in GitHub Desktop.
Save phluid61/7332168 to your computer and use it in GitHub Desktop.
float ranges
diff --git a/range.c b/range.c
index ebd0811..8cb4da8 100644
--- a/range.c
+++ b/range.c
@@ -419,11 +419,21 @@ range_step(int argc, VALUE *argv, VALUE range)
if (!rb_obj_is_kind_of(step, rb_cNumeric)) {
step = rb_to_int(step);
}
- if (rb_funcall(step, '<', 1, INT2FIX(0))) {
- rb_raise(rb_eArgError, "step can't be negative");
+ if (rb_funcall(b, '>', 1, e)) {
+ if (rb_funcall(step, '>', 1, INT2FIX(0))) {
+ rb_raise(rb_eArgError, "step can't be positive for a reverse range");
+ }
+ else if (!rb_funcall(step, '<', 1, INT2FIX(0))) {
+ rb_raise(rb_eArgError, "step can't be 0");
+ }
}
- else if (!rb_funcall(step, '>', 1, INT2FIX(0))) {
- rb_raise(rb_eArgError, "step can't be 0");
+ else {
+ if (rb_funcall(step, '<', 1, INT2FIX(0))) {
+ rb_raise(rb_eArgError, "step can't be negative");
+ }
+ else if (!rb_funcall(step, '>', 1, INT2FIX(0))) {
+ rb_raise(rb_eArgError, "step can't be 0");
+ }
}
}
$ ruby -ve '(1.0..2.0).step(0.1){|f| p f }'
ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-linux]
1.0
1.1
1.2
1.3
1.4
1.5
1.6
1.7000000000000002
1.8
1.9
2.0
$ ruby -ve '(2.0..1.0).step(-0.1){|f| p f }'
ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-linux]
-e:1:in `step': step can't be negative (ArgumentError)
from -e:1:in `<main>'
$
$ ruby21 -ve '(1.0..2.0).step(0.1){|f| p f }'
ruby 2.1.0dev (2013-11-06 trunk 43545) [x86_64-linux]
1.0
1.1
1.2
1.3
1.4
1.5
1.6
1.7000000000000002
1.8
1.9
2.0
$ ruby21 -ve '(1.0..2.0).step(-0.1){|f| p f }'
ruby 2.1.0dev (2013-11-06 trunk 43545) [x86_64-linux]
-e:1:in `step': step can't be negative (ArgumentError)
from -e:1:in `<main>'
$ ruby21 -ve '(2.0..1.0).step(-0.1){|f| p f }'
ruby 2.1.0dev (2013-11-06 trunk 43545) [x86_64-linux]
2.0
1.9
1.8
1.7
1.6
1.5
1.4
1.2999999999999998
1.2
1.1
1.0
$ ruby21 -ve '(2.0..1.0).step(0.1){|f| p f }'
ruby 2.1.0dev (2013-11-06 trunk 43545) [x86_64-linux]
-e:1:in `step': step can't be positive for a reverse range (ArgumentError)
from -e:1:in `<main>'
$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment