Mombu the Programming Forum

Go Back   Mombu the Programming Forum > Programming > That's Ruby, Virginia!
User Name
Password
REGISTER NOW! Mark Forums Read




Reply
1 22nd October 19:08
henry savr
External User
 
Posts: 1
Default That's Ruby, Virginia!



Why,
The Programming Ruby suggests using *for* for Array. Unfortunately I
don't have the book with me. I will tell you the page. And it is logical
to use it for array.
Cheers,
Henry

--
Posted via http://www.ruby-forum.com/.
  Reply With Quote


 


2 22nd October 19:10
henry savr
External User
 
Posts: 1
Default That's Ruby, Virginia!



Well,
I bring it to your attention because:
It mentioned in Programming Ruby, that *for* works with array. They gave
the example of song array. All languages, which I am familiar with work
the same way. They
- take an item from Array,
- execute the block,
- and then take another item

So as [0 ... len] is a new Array, I expected the same behaviour.
But it was not so obvious. It took the whole array and executes the
statement ones. I would expect this behaviour if the code were

for i = [0 ... len]

So, my question was to persons, who realy UNDERSTAND Ruby internal
logic, what was it, a bug or undocumented (or may be documented, but I
just missed it) feature?

Regards,
Henry

--
Posted via http://www.ruby-forum.com/.
  Reply With Quote
3 22nd October 19:10
logan capaldo
External User
 
Posts: 1
Default That's Ruby, Virginia!


It's a new array of size 1 though.

0...len is a _single_ object
what you wrote was
x = 0...len

for i in [x]

Ruby is not perl, ... does not construct lists.

It's neither a bug or a feature. You just didn't grasp the syntax.
  Reply With Quote
4 22nd October 19:10
rimantas liubertas
External User
 
Posts: 1
Default That's Ruby, Virginia!


[0...len] is Array. Which holds only one Range object:


=> [1...5]

=> Array

=> 1

=> Range


Regards,
Rimantas
--
http://rimantas.com/
  Reply With Quote
5 22nd October 19:10
henry savr
External User
 
Posts: 1
Default That's Ruby, Virginia!


Thank you very much, Logan Capaldo.

That was the open eyes explanation, I wanted to get.
The case is closed.


--
Posted via http://www.ruby-forum.com/.
  Reply With Quote
6 22nd October 19:10
henry savr
External User
 
Posts: 1
Default That's Ruby, Virginia!


While I was writing, I got another message, thank you, Rimantas

--
Posted via http://www.ruby-forum.com/.
  Reply With Quote
Reply


Thread Tools
Display Modes




666