[#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: Gavin Sinclair <gsinclair@...>
Date: 2003-09-02 22:43:58 UTC
List: ruby-core #1500
On Wednesday, September 3, 2003, 4:13:48 AM, george wrote:

>> I like the idea, and read nearly every word (skipping some
>> implementation details) of your long post.  I have some concern about
>> the special treatment of the index variable in String#scan that you
>> propose.  A nice rule doesn't look so nice when there are exceptions.
>> 
>> Cheers,
>> Gavin

> What about hashes? Could/should the keys get passed to the index variable?
> That way the scan exceptions start to look like a general rule: the index
> variable is whatever makes most sense for the given method. 

> Isn't it effectively just another arg yielded by an iterator, the difference
> being that the block only needs to bother with it if it's interested. A neat
> kind of optional arg for blocks, in other words ...

> Dave - how would write a method in Ruby which supplied an index variable?

My understanding is that it's an automatic block-argument.  It is
meaningless to attempt to assign values to it.  Any block can use it
or ignore it if it wishes.  For example:

  def transaction(*args)
    start
    yield(*args)
    commit
  end

  transaction(foo) do |f|.i
    puts "Index: #{i}"
  end

  # Output: "Index: 0"

That example does not demonstrate the utility of the proposal; it
demonstrates the automatic way in which the index parameter is set.

Gavin


In This Thread