
Submitted by transami (Wed Aug 10 01:18:48 UTC 2005)
class Numeric
def kilo(type=nil)
case type
when :bit, bits
self * 1024
when :byte, :bytes
self * 1024 * 8
else
self * 1000
end
end
end
This works great, as one can then type things like:
1.kilo #=> 1000 1.kilo:bit #=> 1024 1.kilo:byte #=> 8192
But a problem arises when using this in a method chain:
1.kilo:byte.between?(1,1000) #=> NoMethodError: undefined method `between?' for :byte:Symbol
This error renders an otherwise very useful notation nearly useless.
def kilo(*:flags)
case flags[0]
...
end
Methods could then have multiple flags. They could also be made more readable by allowing a space between the flags and the remaining parameters. Eg.
def foo(::flags,x)
flags.collect { |f|
"#{f}#{x}"
end
end
foo:bar:boo "!!!" #=> ["bar!!!","boo!!!"]
The alternative proposal is just to allow an initial symbol parameter which is pressed directly up against the method name to be tightly bound to it. So,
1.kilo:bytes.even? #=> true 1.kilo :bytes.even? #=> ERROR

| Comments | Current voting | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
|


RCRchive copyright © David Alan Black, 2003-2005.
Powered by .
I'm not getting why the argument's being a symbol is grounds for this:
being automatically parsed like this:
or this:
I can see, of course, that it saves on parentheses. But it would introduce a major exception to the way method calls are parsed, based on the class of the argument (assuming you're not suggesting that this extend generally to all method calls). I'm not seeing a need that would justify that.
David Black
David--
Yet, presently it is the later parsing. I realize that the minor alternate solution is dependent on the type of the first prarmeter --and thus would apply to all methods. But us that really that major a change. There is already a difference with regards to the binding (is that the word?) of blocks when using { vs. do. Would it not be simple enough to set a flag if the 1st argument butted up againt the method name?
trans.
Sorry, "But us that really that major a change." should read "But is that really such a major change?"
I see no need for this, where we can type
or even
works pretty well.
-matz.
Matz,
That's exactly the problem!
or shoud it be SI
And kilo_bytes means A LOT more methods:
trans.
mega:bytes etc. require internal branching anyway. If infinitum happens with mega_bytes, it would happen with mega:bytes as well.
you can even generate combination methods like mega_bytes using metaprogramming feature.
besides that, I'd rather preserve colons inside the identifiers for some other purpose, such as namespace thingy, though I have no concrete idea yet.
-matz.
trans:
-austin
austin,
We learned in school here that kilo = 1000.
I believe we should stick to kilo meaning 1000, else more confusion is spread. The 1024 of it in computer area is a bad idea to ever associate kilo with it, it leads to unneeded confusion. If people would use kibibyte more, others probably would adapt it too.