[#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: why the lucky stiff <ruby-core@...>
Date: 2003-09-02 17:01:00 UTC
List: ruby-core #1498
daz (dooby@d10.karoo.co.uk) wrote:
> 
> loop {.x
>   x < 5 or break
>   p x
> }
> 

daz:

A good, exhaustive trip through this particular debacle.  Your citations
were cool.

You know, I think our basic problem concerns metadata.  An index is
merely metadata applicable to an iteration cycle.  And I don't think
that's the only bit of metadata that could be useful in iteration.  The
total number of iterations expected.  Even an available reference to the
current block would be great for simpler recursion.

I think you've done a good job presenting your RCR (although it's really
hard to push syntactical changes with the current parser.)

My concerns:

  1. If we do want to add more metadata, the syntax becomes cumbersome.
     I'm guessing additions would be positional.

       loop {.index.last print "Item #{ index } of #{ last }." }

     If there is no plan for expansion of the syntax, then it become an
     _exception_ to Ruby's syntax rather than an _evolution_ of Ruby's
     syntax.

  2. The dot notation looks like a method call.  Yet, assignment is
     being performed.  It also gives the illusion that the block's
     argument list is an object.

I would love to see a good solution to this problem, though.  So I
wonder:  is there a way we can provide access to block metadata?

Perhaps through a keyword, which provides an instance of metadata within
a block:

  loop {
    x = iter.index
    x < 5 or break
    p x
  }

Or maybe a generic metadata scheme which could be used with various Ruby
constructs:

  class Test
    [dot_net_signature => [String, Int, Int]]
    def run_me( a, b )
      "#{ a } + #{ b }"
    end
  end

  Test.method( :run_me ).meta['dot_net_signature'] => [String, Int, Int]

  # Start our index at 2..
  [index => 2]
  loop {
    x = meta['index']
    x < 5 or break
    p x
  }

Fun ideas, but you have to think about scoping and the various types of
blocks that commonly float around in Ruby.

_why

In This Thread