From: Anders Nor Berle Date: Tue, 3 Feb 2009 22:30:00 +0000 (+0000) Subject: Add 'to' to Number X-Git-Tag: 0_10~7 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=f0f5341ba85b40345a57e91cccc70957f1164436;p=gitmo%2FMoose-Autobox.git Add 'to' to Number --- diff --git a/Changes b/Changes index 3565811..5c2aa89 100644 --- a/Changes +++ b/Changes @@ -2,6 +2,7 @@ Revision history for Perl extension Moose::Autobox 0.10 - add split, words, lines to String (Sartak) + - add 'to' to Number (Debolaz) 0.09 Thu. Oct 23, 2008 - update Perl6::Junction dependency and support new version diff --git a/lib/Moose/Autobox/Number.pm b/lib/Moose/Autobox/Number.pm index 132c6f3..184f2be 100644 --- a/lib/Moose/Autobox/Number.pm +++ b/lib/Moose/Autobox/Number.pm @@ -1,10 +1,15 @@ package Moose::Autobox::Number; use Moose::Role; -our $VERSION = '0.09'; +our $VERSION = '0.10'; with 'Moose::Autobox::Value'; - + +sub to { + return [ $_[0] .. $_[1] ] if $_[0] <= $_[1]; + return [ reverse $_[1] .. $_[0] ]; +} + 1; __END__ @@ -23,6 +28,21 @@ This is a role to describes a Numeric value. =over 4 +=item B + +Takes another number as argument and produces an array ranging from +the number the method is called on to the number given as argument. In +some situation, this method intentionally behaves different from the +range operator in perl: + + $foo = [ 5 .. 1 ]; # $foo is [] + + $foo = 5->to(1); # $foo is [ 5, 4, 3, 2, 1 ] + +=back + +=over 4 + =item B =back diff --git a/t/009_number.t b/t/009_number.t new file mode 100644 index 0000000..16b9b1f --- /dev/null +++ b/t/009_number.t @@ -0,0 +1,28 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Test::More tests => 4; + +BEGIN { + use_ok('Moose::Autobox'); +} + +use Moose::Autobox; + +is_deeply( +1->to(5), +[ 1, 2, 3, 4, 5 ], +'... got 1 to 5'); + +is_deeply( +5->to(1), +[ 5, 4, 3, 2, 1 ], +'... got 5 to 1'); + +is_deeply( +1->to(1), +[ 1 ], +'... got 1 to 1'); +