Why Ruby?

Ary Borenszweig ary at esperanto.org.ar
Sun Dec 12 18:44:17 PST 2010


On 12/12/2010 02:03 PM, Adam D. Ruppe wrote:
> foobar wrote:
>> D basically re-writes foreach with opApply into the ruby version
> which is why Ruby is *BETTER*
>
> You missed the point: there is no "Ruby version". They are the
> same thing.
>
>> foreach to me is a redundant obfuscation
>
> How can it be redundant? It's got the same elements the same
> number of times.
>
>
> rofl.copter.each |lol|
>      spam
> end
>
>
> foreach(lol; rofl.copter)
>      spam
>
>
> Same elements, just reordered.
>
>
> I don't know about the each() method itself. I've never written
> one, but I suspect it is virtually identical to opApply too.

Similar. In ruby you have the yield keyword. Here's the D example:

class Foo
{
     uint array[2];

     int opApply(int delegate(ref uint) dg)
     {   int result = 0;

	for (int i = 0; i < array.length; i++)
	{
	    result = dg(array[i]);
	    if (result)
		break;
	}
	return result;
     }
}

Here's the same code written in ruby

class Foo
   def each
     yield @array[0]
     yield @array[1]
   end
end

So pretty similar, right? :-P

D should provide a yield keyword that basically rewrites the body of the 
method into the first code. What's that "if (result) break;"? That's 
showing the implementation of foreach. Why do I have to rememebr the 
result of dg? What's that? What if I forget it? I just want to yield two 
elements :-(


More information about the Digitalmars-d mailing list