D
Daniel Schierbeck
Currently, as far as I know, `if' and `unless' merely check if an object
is of either types NilClass or FalseClass. This seems to contradict the
duck-typing paradigm. I propose that we implement a method Object#to_b
that is called by `if' and `unless'. It should return a boolean value.
That way, a developer can decide whether or not an object should
evaluate to true or false.
class Connection
def open?; end
def closed?; end
def to_b; open?; end
end
connection = Connection.new(...)
if connection
# ...
end
Implementation:
class Object
def to_b
true
end
end
class FalseClass
def to_b
false
end
end
class NilClass
def to_b
false
end
end
One problem I see with this proposal is that it seems like a lot of
people check if a variable has been set by writing `if var ...'. This
change would require that people wrote `unless var.nil?', which I
personally find more correct as well.
Cheers,
Daniel
is of either types NilClass or FalseClass. This seems to contradict the
duck-typing paradigm. I propose that we implement a method Object#to_b
that is called by `if' and `unless'. It should return a boolean value.
That way, a developer can decide whether or not an object should
evaluate to true or false.
class Connection
def open?; end
def closed?; end
def to_b; open?; end
end
connection = Connection.new(...)
if connection
# ...
end
Implementation:
class Object
def to_b
true
end
end
class FalseClass
def to_b
false
end
end
class NilClass
def to_b
false
end
end
One problem I see with this proposal is that it seems like a lot of
people check if a variable has been set by writing `if var ...'. This
change would require that people wrote `unless var.nil?', which I
personally find more correct as well.
Cheers,
Daniel