3 ms·
I'd say that justifying it as because you can still determine the truthiness of a string is missing the point; defined? (http://ruby-doc.org/docs/keywords/1.9/O
by kapowaz 14y ago
I'd say that justifying it as because you can still determine the truthiness of a string is missing the point; defined? (http://ruby-doc.org/docs/keywords/1.9/Object.html#method-i-defined-3F http://ruby-doc.org/docs/keywords/1.9/Object.html#method-i-d...) can return one of a number of string values, or nil, depending on what expression you pass to it. The actual return value can be useful to you depending on what you're doing, or you might be happy enough just accepting that there it returns a value other than nil (which is to say you don't care what it is, just that yes, something is defined which matches the passed expression).
- masklinn 14y ago> defined? (http://ruby-doc.org/docs/keywords/1.9/Object.html#method-i-d... http://ruby-doc.org/docs/keywords/1.9/Object.html#method-i-d...) can return one of a number of string values, or nil, depending on what expression you pass to it. The actual return value can be useful to you depending on what you're doing While the actual return value can be useful, it would be just as useful if the method had a more sensible name than hinting it's a predicate, which it is not.
- kapowaz 14y agoWhat would such a more sensible name be? I look at this as a trade-off between code readability and consistency. Particularly when assuming it is a predicate (without greater knowledge of the method) is not a dangerous assumption, I'd say the trade-off is a good one.
- ibotty 14y agowhat about defined_as (i agree that within ruby's conventions (weak typing) defined? is not that bad)
- masklinn 14y ago> ruby's conventions (weak typing) No. "Weak Typing" doesn't mean jack shit, and there are very few under its thousands of different and incompatible identities which Ruby would match, save for the very least useful of them.
- ibotty 14y agono. weak typing is exactly what i mean. > if a.defined? works, because the returned string type is implicitly converted to a boolean. that's weak typing. at least one (i guess the most common) interpretation of weak typing.
- nirvdrum 14y agotoken_type or lexical_type would make more sense to me. On top of defined? not returning a boolean, it has other confusing semantics. In particular, it's the only keyword I can think of that appears to exhibit call-by-name semantics. I don't know if this is just a special parser case, but it doesn't evaluate your arguments despite looking like a method call. So the return value can be doubly confusing.
- Xylakant 14y agoIt's a special parser case. The reason is that defined? may be followed by an uninitialized constant: jruby-1.6.8 :089 > defined? Foo::Bar => nil jruby-1.6.8 :090 > Foo::Bar NameError: uninitialized constant Foo::Bar You don't want the argument to be evaluated because that could raise a NameError. It's actually an operator.
- nirvdrum 14y agoThanks for the clarification. I was recently surprised by the return value when: z = x.y z == x.y # => true defined?(z) == defined?(x.y) # => false