By reference parameters come in handy.
In ruby, you can't do this:
def incby2(VAR i1, i2, i3)
i1 = i1 + 2
i2 = i2 + 2
i3 = i3 + 2
end
Sorry folks.
In modern pascal
procedure incby2(var i1, i2, i3: integer);
begin
i1:= i1 + 2;
i2:= i2 + 2;
i2:= i3 + 2; // or you could use inc()
end;
So of course being a total Ruby newbie boobie, you look up 'ruby by reference' on the internet on google and what do you find?
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/127057
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/127139
Someone suggesting that maybe a regex could offer the functionality of the 'by reference' feature available in real programming languages.
Excuse my Blueberry Pie Mrs. Flintstone, but?
A regular expression and a string, to offer 'by reference' capabilities in a language for simply modifying some freaking integers in a function? Can someone send me that bag of green smelly leafy stuff they are smoking, because I've never tried it and would really like to?
Okay, so let's reinvent By Reference then:
class Reference
def initialize(var_name, vars)
@getter = eval "lambda { #{var_name} }", vars
@setter = eval "lambda { |v| #{var_name} = v }", vars
end
def value
@getter.call
end
def value=(new_value)
@setter.call(new_value)
end
end
def ref(&block)
Reference.new(block.call, block.binding)
end
def incby2(i)
i.value += 2
end
x = 3
puts(incby2(ref{:x}))
(thank Jim Weirich for the idea).
And there you have it, we just reinvented 'by reference' in Ruby. It makes by reference hacking look extremely uglier and more complicated than it has to be, and induces nonsensical class overhead. I'd rather use a GOTO statement I think.
Ugly incrementing
Also, what is more obvious:
inc(i)
or
i += 1
I guess the problem is that we can't increment 'i' because 'i' is an object.. and objects can't be incremented, because, objects are objects are objects and instances of objects don't increment, and.. yes, purism really helps keep languages clean and simple without stupid workarounds that look stupid too.
But you know what? Our brains naturally convert
i += 1
to mean "increment i"
Yes, that is what our brains convert it to. So why not just write:
increment i
or
increment(i)
or
inc(i)
instead of beating around the bush with confusing stuff that doesn't even save keystrokes (if you count the spaces they are the same).
Warning: This article contains bitching, moaning, and complaining. Please take with a grain of salt. Lars may become more familiar with Ruby with time and stop being such an ass after he discovers more features in the language that offer what he needs, but it is also quite possible that this will not be so if he has to do complex things to make simple things work that should be simple and consistent in the first place.
|