[#1551] Hashes as keys — "Nathaniel Talbott" <nathaniel@...>

I was just playing around with Hash#hash and discovered that you can't use a

13 messages 2003/09/23

Re: |rcr|.xv Index Variables ( *_with_index )

From: "daz" <dooby@...10.karoo.co.uk>
Date: 2003-09-03 18:05:50 UTC
List: ruby-core #1508
From: <george.marrows@ps.ge.com>


> Dave provides a C api for setting the index value, so perhaps
> there should be a mechanism/syntax for setting it from Ruby
> code.


$XITER (horrible temporary name chosen by me to avoid clashes:)
gives access to a block's index from a script.

I wasn't expecting any interest in that.

It looks like a global var but it's local to the current scope
like $_ and some of the pattern-matching variables ($&, $' etc.).

Rather than changing the xv, send your value with yield.
If an xv is given, it will contain the default incrementing
value.

def roo
  3.times do .x
    yield 2*x
  end
end

roo {|r1|.x p [x, r1]}

#-> [0, 0]
#-> [1, 2]
#-> [2, 4]


> BTW what about eg 'Hello'.each_byte {|ch;ix| puts ch} for the syntax?
> And of course {|;ix| } for the index only.
> 
> -- George

My thinking was that the index isn't a block parameter [b/p]
(it doesn't affect the arity) so why must it be inside
the bars (which are delimeters for the b/ps) ?

However, your use of a single character is very much in line
with the spirit I was trying to capture.


daz



In This Thread

Prev Next