On Thu, Aug 28, 2008 at 12:32 AM, Joshua M. [email protected] wrote:
Thanks. But isn’t there a way to extend ruby so it can handle “|||”?
In Ruby some “operators” are syntactic sugar for methods, for example
“<<”, or “+”. But there’s no way to go in the other direction. That
is, you cannot define a method in Object named “|||” and tell Ruby it
should be parsed as an operator of such and such arity, etc.
On Thu, Aug 28, 2008 at 12:32 AM, Joshua M. [email protected] wrote:
Thanks. But isn’t there a way to extend ruby so it can handle “|||”?
In Ruby some “operators” are syntactic sugar for methods, for example
“<<”, or “+”. But there’s no way to go in the other direction. That
is, you cannot define a method in Object named “|||” and tell Ruby it
should be parsed as an operator of such and such arity, etc.
I want something like that:
Josh
Like the Numeric#nonzero? that is useful in chained comparisons, you
can define nonblank? to be similar and get this effect:
class String
Allowing a chain like: string_value.nonblank? || ‘default value’
def nonblank?
self unless blank?
end
end
class NilClass
Allowing a chain like: value.nonblank? || ‘default value’
def nonblank?
self
end
so it plays nicely with Numeric#nonzero?
def nonzero?
self
end
end
Since you’re already Rails, you’re already ActiveSupport and blank?
will be well defined.