P
Phlip
Todd said:Darn. I was hoping to get a #to_binary added to Ruby with all the
#to_b talk.
Puh-leeze. "Binary" is soo Last Millenium.
Todd said:Darn. I was hoping to get a #to_binary added to Ruby with all the
#to_b talk.
Phlip said:Puh-leeze. "Binary" is soo Last Millenium.
2007/9/10 said:If we are going for really cool new tech, how about
/set bugs OFF
or even a
require 'do_what_i_mean
p.s. I know I left off the closing single quote, but it knows what I
mean.
This makes more sense as #to_bool, and certainly not default
conditional behavior. Yet, a #to_b to dictate conditions could be
useful, allowing us to create our own variants of Nil. However, the
only alternate to Nil I've ever found useful is Null (nil that returns
itself on method_missing) --so I'd be pretty happy if we just had
that. Also, I believe there's a efficiency issue with using #to_b for
conditions, which is why it is never considered.
everybody knows
if "false"
returns true.
class Object
def to_bool
case self
when true, false: self
when nil: false
else
to_i != 0
end
end
end
class String
TRUE_REGEXP = /^(yes|true|on|t|1|\-1)$/i.freeze
FALSE_REGEXP = /^(no|false|off|f|0)$/i.freeze
def to_bool
case self
when TRUE_REGEXP: true
when FALSE_REGEXP: false
else
to_i != 0
end
end
end
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.