Why Ruby?

Stephan Soller stephan.soller at helionweb.de
Mon Dec 13 02:22:27 PST 2010


On 12.12.2010 20:44, foobar wrote:
> 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.
>>
> By "ruby version" I meant the syntax. I agreed already that they are implemented the same.
>
>>> foreach to me is a redundant obfuscation
>>
>> How can it be redundant? It's got the same elements the same
>> number of times.
>>
> incorrect. The difference is that D adds "special" syntax for this whereas it's just a plain method in Ruby. By calling opApply directly you get the direct "ruby version" without the redundant use of a keyword + compiler transformation.
>
>>
>> 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.
>
> opApply *is* the same thing as Ruby's each method.
>
>

Just for the sake of correctness: Ruby too has a for-like loop that gets 
rewritten to the block/delegate version.

	for lol in rofl.copter
		spam
	end

This gets rewritten to

	rofl.copter.each do |lol|
		spam
	end

As far as I know this is a construct for ease transition from C to Ruby 
but is not used very much. Blocks are used very often in Ruby so using a 
for-loop is kind of inconsistent style.

Happy programming
Stephan Soller


More information about the Digitalmars-d mailing list