Darn. I was hoping to get a #to_binary added to Ruby with all the
#to_b talk.
Todd
Darn. I was hoping to get a #to_binary added to Ruby with all the
#to_b talk.
Todd
Todd B. wrote:
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.
Phlip wrote:
Todd B. wrote:
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.
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.
2007/9/10, Lloyd L. [email protected]:
/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.
… but only after successful requiring, so you actually have to
properly close the quote.
robert
On Sep 8, 7:54 am, Trans [email protected] wrote:
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.
Sorry, I momentarily had gotten the convention backward. You are
right, #to_b is the correct method, not #to_bool. #to_bool would make
sense for effecting conditionals though, not #to_b.
T.
On Sep 8, 5:01 am, “Giles B.” [email protected] wrote:
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
“false”.to_bool #=> false
“on”.to_bool #=> true
1.to_bool #=> true
0.to_bool #=> false
“1”.to_bool #=> true
nil.to_bool #=> false
enjoy
Sharon
On Sep 10, 11:16 am, Sharon R. [email protected] wrote:
case self
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
Ah. This is a good example for demonstrating what I meant. To fit into
Ruby convention, only TrueClass, FalseClass and NilClass (and things
like NullClass) should respond to to_bool. The String method should be
#to_b, because it is not a true “Boolean” type.
T.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs