On 6/7/04 2:00 PM, "David A. Black" <dblack / wobblini.net> wrote:

> Hi --
> 
> On Tue, 8 Jun 2004, Richard Kilmer wrote:
> 
>> On 6/7/04 12:10 PM, "David A. Black" <dblack / wobblini.net> wrote:
>> 
>>> Hi --
>>> 
>>> On Tue, 8 Jun 2004, Richard Kilmer wrote:
>>> 
>>>> Moreover, if you could do that syntactically (ie. Change Ruby to support
>>>> this kind of thing) like the following (love it or hate it) syntax:
>>>> 
>>>> class Foo
>>>> 
>>>>   def {Hash}[](key)
>>>>     @mymap[key]
>>>>   end
>>>> 
>>>>   def {Hash}[]=(key, value)
>>>>     @mymap[key] = value
>>>>   end
>>>> 
>>>> end
>>>> 
>>>> f = Foo.new
>>>> f{Hash}['name']="rich"
>>>> 
>>>> ...but you don't always need the {...}
>>>> 
>>>> p f['name'] => 'rich'
>>>> 
>>>> ...and if you do something that's naughty...
>>>> 
>>>> p f{Array}[1] => raise Exception...
>>> 
>>> It would have to be something slightly different from that, though,
>>> because of:
>>> 
>>>   irb(main):008:0> def f; [1,2,3]; end
>>>   => nil
>>>   irb(main):009:0> f{Array}[1]
>>>   => 2
>>> 
>>> 
>>> David
>> 
>> Actually no...I don't think so.  Ruby's scoping handles whether something is
>> a method or a variable.  If its a method, then:
>> 
>>  p f.class => Array
>> 
>>  p f{Array}[1] => 2
>> 
>> is correct because the [] method on the Array instance is correct (by
>> default) in that f returns an array and you are calling:
>> 
>>   {Array}[1] 
>> 
>> On that object.  Its a question of what you are sending the message to...in
>> other words, the {...} is appended to the method that follows the } as
>> 'semantic metadata', whereas with:
>> 
>>   f = Foo.new
>> 
>>   f{Array}[1]
>> 
>> Is sending the message to the object pointed to by the variable f.
>> 
>> So, where f is a variable or a method name, that is up to Ruby, but the
>> {Array} data is attached to the method that follows, regardless.
> 
> My point, though, was that the syntax you're proposing already means
> something to Ruby; in the case of my example:
> 
>        f      {    Array   } [1]
>     method    { code block } [ subscript version of calling #[1] ]
> 
> so in order not to baffle the parser, I think you'd have to come up
> with something different for your new technique, or come up with rules
> about having it only work for variables but not methods (to avoid
> interpreting the {} as a code block), which I think would be a rocky
> road.  
> 
> (BTW I know it's sort of pointless to have a code block that just
> consists of {Array} :-) but it's valid Ruby, and I wanted to keep it
> looking like your example.)


Oops...sorry...you are right...it would have to be:

  f.{Array}['rich']

to make it explicit that you are grouping the {} with the method name.  This
is only an issue for the operator'ish methods because methods require the
dot in them already.

e.g.

  f.firstname
    =>
  f.{Person}firstname

Not as pretty...well...I leave that to the syntax guru (aka Matz) ;-)

You could always leave that calling syntax out, and just access the semantic
metadata with respond_to? or something...

  f.respond_to?(:firstname, Person)

-rich