Ruby2 is at the doors, bringing many presents in form of welcome changes. But then there is the one change I consider a bowdown to stupidity, ignorance and lazyness:
String#[] with a single argument will no longer return the ASCII value of the char at that position.
I wonder why? We have that functionality already, twice even, str[x,1] does it and str[x..x] too.
It seems it's a kneejerk reaction to continuos whining of stupid people and those who can't be bothered with RTFM.
Some percentage of the coders considers it unintuitive, that str[x] returns the ASCII code. Now that part is perfectly fine.
But I wonder what happens when you encounter that. I'd expect anybody with at least a little bit of brain to consult the documentation at that point, where you can clearly read how String#[] behaves. When you do that you commit to memory that str[x] will return the ASCII value and - depending on how often you use it - you'll use it wrongly once, twice or maybe three times.
So who would possibly want it to change and in turn duplicate existing and perfectly fine behaviour, dropping a different behaviour that becomes more difficult to get that way?
My conclusion: whiny stupid bitches (is there a matching male form of bitch?).
2007-09-11
Simple higher order methods
Sometimes there's an itch that you just have to scratch. One of them hit me today, luckily I already had the matching scratch ready. My itch was, that while Ruby has that very nice module Enumerator, it only works with classes that provide a .each method. So you can have enum.map { ... }, which operates on each. But what if you have a class that can iterate over various things? E.g. a String could iterate over bytes, chars, words or lines. A directory could iterate over files, directories or all entries.
My solution to the problem: Iterator.
With that you can enable a method to return an Iterator with a single statement, which in turn allows you to do things like: dir.entries.select { ... }, where dir.entries returns an Iterator.
Example use:
My solution to the problem: Iterator.
class Iterator
include Enumerable
class Iteration
def initialize(&block)
@block = block
end
def yield(*args)
@block.call(*args)
end
end
def initialize(&iterator)
@iterator = iterator
end
def each(&block)
@iterator.call(Iteration.new(&block))
end
end
With that you can enable a method to return an Iterator with a single statement, which in turn allows you to do things like: dir.entries.select { ... }, where dir.entries returns an Iterator.
Example use:
def each_custom
if block_given? then
@custom.each { |element| yield(element) }
else
Iterator.new { |iter| @custom.each { |element| iter.yield(element) } }
end
end
2007-09-10
Progress
"Progress doesn't come from early risers - progress is made by lazy men looking for easier ways to do things."
Robert Heinlein
Robert Heinlein
2007-09-07
‘No Longer Joe Cool’
This article nails it quite well...
The immaturity of consumers (or “I want a refund!”)
(via daringfireball.net)
The immaturity of consumers (or “I want a refund!”)
(via daringfireball.net)
2007-09-03
Black magic
Today when I refactored some of my legacy code, I accidentally ran into the problem that I tried to let the outer class inherit from an inner class, like: class OuterClass < OuterClass::InnerClass. I mentioned in on #ruby-lang, mostly as an anecdotal remark, but LoganCapaldo actually came up with a solution for it:
Now I don't know if I should bow before such ingenuity or run away screaming from this darkest of the black coding magics :)
parent = Class.new
Foo = Class.new(parent)
Foo.class_eval { Foo::Bar = parent }
class Foo < Foo::Bar
class Bar
def a
1
end
end
end
p Foo.new.a
Now I don't know if I should bow before such ingenuity or run away screaming from this darkest of the black coding magics :)
Significance
It doesn't matter from where you come.
Neither where you are.
It's all about where you go.
Neither where you are.
It's all about where you go.
Subscribe to:
Comments (Atom)