[#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: george.marrows@...
Date: 2003-09-03 14:14:12 UTC
List: ruby-core #1506
(Mail originally sent privately by mistake - sorry for any confusion &
apologies for Outlook wrecking the threading.)

> > 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.

But the scan and gsub (and possibly the hash key) examples show that maybe
it doesn't have to just be the loop count. Dave provides a C api for setting
the index value, so perhaps there should be a mechanism/syntax for setting
it from Ruby code. It'd be very annoying to have library methods which can
set the index variable in an interesting way but not to be able to set
yourself without C programming.

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

-- George

In This Thread

Prev Next