ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: "daz" <dooby@d10.karoo.co.uk>
To: <ruby-core@ruby-lang.org>
Subject: Re: |rcr|.xv  Index Variables ( *_with_index )
Date: Thu, 4 Sep 2003 03:05:50 +0900	[thread overview]
Message-ID: <00f001c37245$fcff3740$f2aff9d5@vanna> (raw)
In-Reply-To: 6192367D59F8904CA553579EF41FEEA001B03E71@ukcbgx01psge.geips.ge.com


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

  reply	other threads:[~2003-09-03 17:59 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-09-03 14:14 |rcr|.xv Index Variables ( *_with_index ) george.marrows
2003-09-03 18:05 ` daz [this message]
  -- strict thread matches above, loose matches on Subject: below --
2003-09-04  8:59 george.marrows
2003-09-02 18:13 george.marrows
2003-09-02 22:43 ` Gavin Sinclair
2003-09-03 13:39 ` daz
2003-09-02 14:48 daz
2003-09-02 15:11 ` Gavin Sinclair
2003-09-02 15:58   ` daz
2003-09-02 17:01 ` why the lucky stiff
2003-09-03 13:50   ` daz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='00f001c37245$fcff3740$f2aff9d5@vanna' \
    --to=ruby-core@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).