(start..end) where start > end 
Author Message
 (start..end) where start > end


Quote:
> Hi!

> Is there any reason why Ruby can't handle a
> "backwards" range?

> Example
> ---------
> num = 240
> (7..0).each do
>   |bitindex|
>   print num[bitindex]
> end
> print "\n"
> ---------

How about:

7.downto(0) do
   |bitindex|
   print num[bitindex]
end
print "\n"

__________________________________________________
Do You Yahoo!?
Yahoo! Photos - Share your holiday photos online!
http://www.*-*-*.com/



Thu, 26 Jun 2003 01:10:53 GMT  
 (start..end) where start > end
Hi!

Is there any reason why Ruby can't handle a "backwards" range?

Example
---------
num = 240
(7..0).each do
  |bitindex|
  print num[bitindex]
end
print "\n"
---------

The above doesn't work because Ruby won't create a range where the start is
greater than the end. (I know I can use printf "%b\n", num to print a number
in binary notation, but that is not the issue here).

An ugly way of getting it to work is using unary minus:
---------
num = 240
(-7..0).each do
  |bitindex|
  print num[-bitindex]
end
print "\n"
---------

Is [7,6,5,4,3,2,1,0].each the only way to achieve what I want?

Wouldn't it be desirable to be able to create ranges where the start is
greater than end?

Example:
(3..0).each do |i| print i, " " end # => 3 2 1 0

I think I would be able to make this change if I just got some introductory
help with the procedure of checking out, testing/verifying, auditing etc.

Rob



Thu, 26 Jun 2003 00:56:13 GMT  
 (start..end) where start > end

Quote:
>> (7..0).each do
>Try this:
>   7.downto(0) do |n| puts n end
>   (0..7).to_a.reverse.each do |n| puts n end

Do you agree with me when I say that (7..0) is more beautiful? And it would
probably be more efficient too.

/rob



Thu, 26 Jun 2003 01:17:24 GMT  
 (start..end) where start > end

Quote:

> Hi!

> Is there any reason why Ruby can't handle a "backwards" range?

Oh thou of little faith! :-)

Quote:
> Example
> ---------
> num = 240
> (7..0).each do
>   |bitindex|
>   print num[bitindex]
> end
> print "\n"
> ---------

Try this:

   7.downto(0) do |n| puts n end

or this:

   (0..7).to_a.reverse.each do |n| puts n end

and probably other idioms....

David

--
David Alan Black


Web:  http://pirate.shu.edu/~blackdav



Thu, 26 Jun 2003 01:08:38 GMT  
 (start..end) where start > end

Quote:

> >> (7..0).each do

> >Try this:
> >   7.downto(0) do |n| puts n end
> >   (0..7).to_a.reverse.each do |n| puts n end

> Do you agree with me when I say that (7..0) is more beautiful? And it would
> probably be more efficient too.

It's certainly more beautiful than the to_a.reverse version.  I don't
mind #downto too much -- though (7..0) would be nice.  I wonder if
there's something inherent in the definition of a range (as opposed to
an array) which precludes that.

David

--
David Alan Black


Web:  http://pirate.shu.edu/~blackdav



Thu, 26 Jun 2003 01:36:20 GMT  
 (start..end) where start > end

Quote:


> > >> (7..0).each do

> > >Try this:
> > >   7.downto(0) do |n| puts n end
> > >   (0..7).to_a.reverse.each do |n| puts n end

> > Do you agree with me when I say that (7..0) is more beautiful? And it would
> > probably be more efficient too.

> It's certainly more beautiful than the to_a.reverse version.  I don't
> mind #downto too much -- though (7..0) would be nice.  I wonder if
> there's something inherent in the definition of a range (as opposed to
> an array) which precludes that.

This leads into the discussion (again ;-) about #succ and
why there isn't something like #pred etc.

If someone came up with a good algorithm/method to do the inverse
of #succ, I think matz would be willing to talk about it.

cf. [ruby-talk:5868]

Guy N. Hurst

--
HurstLinks Web Development    http://www.hurstlinks.com/
Norfolk, VA  23510            (757)623-9688 FAX 623-0433
PHP/MySQL - Ruby/Perl - HTML/Javascript



Thu, 26 Jun 2003 04:04:43 GMT  
 (start..end) where start > end

Quote:
> If someone came up with a good algorithm/method to do the inverse
> of #succ, I think matz would be willing to talk about it.

class Numeric; def pred
        self - 1
end end

class String; def pred
        raise NotImplementError
end end

matju



Thu, 26 Jun 2003 04:33:57 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Printing a section of line between start/end strings

2. Reports with START-END date

3. Dynamic start of subvi without waiting for end of subvi

4. array[start..end] gives unexpected results

5. How to end process started in os.popen?

6. Why not find(S, SUB[, START[, END]]) ?

7. start/end date for a given week #

8. Measure Command Lisp selected start end defined - Please

9. Coupling the Start, End, and Length scrollbars.

10. End to end examples?

11. US - BA, DESIGNERS NEEDED !! FRONT-END / BACK-END

12. CAD Engineer *** Front-End and Back-End Tool *** for Silicon Graphics, Mountain View, CA

 

 
Powered by phpBB® Forum Software