Questions tagged [truthiness]

In dynamically typed language, truthiness is a term used to describe a value that might evaluate to boolean true.

99 questions
6
votes
2 answers

Why does truth && "string" return "string"

Let's say I have a something like true && true #=> true Which makes sense, so I try something like: true && "dsfdsf" #=> "dsfdsf" Which surprises me because often times I'll do something like if something && something and I always thought that…
Noah Clark
  • 8,101
  • 14
  • 74
  • 116
5
votes
1 answer

Javascript Undefined String Property Truthiness

I've often used the following pattern in my Javascript: x = couldBeNullThing || valueIfItIsNull; because it beats than: x = couldBeNullThing ? couldBeNullThing : valueIfItIsNull; I also frequently use a slight variant of that same pattern: x = x…
machineghost
  • 33,529
  • 30
  • 159
  • 234
5
votes
1 answer

Returning the truthiness of a variable rather than its value?

Consider this code: test_string = 'not empty' if test_string: return True else: return False I know I could construct a conditional expression to do it: return True if test_string else False However, I don't like testing if a boolean is…
K Engle
  • 1,322
  • 2
  • 11
  • 23
4
votes
3 answers

How to inherit from NilClass or how to simulate similar function

I just want to use Null Object Design Pattern, but I found I can inherit from NilClass. I can write a method "nil?" and return false but what if user write code below if null_object puts "shouldn't be here" end For clarify what I try to do…
allenwei
  • 4,047
  • 5
  • 23
  • 26
4
votes
2 answers

Why does Python evaluate strings/numbers as True in if statements yet myNumber == True returns False?

The following will print 'ok': if 5: print('ok') Yet when I do: print(5 == True) The output is False. The same thing happens with strings. Why?
turnip
  • 2,246
  • 5
  • 30
  • 58
4
votes
3 answers

How can I avoid truthiness in Ruby?

Is there any standard way to avoid truthiness in Ruby, or would I need to roll my own solution, such as class FalseClass def to_bool self end end class TrueClass def to_bool self end end true.to_bool # => true false.to_bool # =>…
Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
4
votes
6 answers

Why is 'false' truthy in javascript?

I understand that an empty string is falsy in javascript and a not-empty string is truthy in javascript. However, why is 'false' truthy in javascript, is there anything explicit in the specification? Is it a performance issue or are there situations…
StuperUser
  • 10,555
  • 13
  • 78
  • 137
4
votes
2 answers

YARD convention for truthiness

Is there a convention to indicate that a parameter in YARD style documentation is only used for its "truthiness" status, that is you only want to know if it's false or nil or is truthy? What is typically put in place of Truthy in the following? #…
Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
3
votes
1 answer

Determining "Truthiness" of an expression?

Possible Duplicates: Check if Ruby object is a Boolean How can I avoid truthiness in Ruby? Given an array like the following (for example): [3, false, "String", 14, "20-31", true, true, "Other String"] Is there an easier way to determine which…
dnch
  • 9,565
  • 2
  • 38
  • 41
3
votes
1 answer

When can self == None

I am looking at a snippet if not self: in an answer to another question which implements __nonzero__(). This gets me wondering: apart from __nonzero__() returning False or the trivial local assignment self = None, are there other situations, in…
Vorac
  • 8,726
  • 11
  • 58
  • 101
3
votes
2 answers

Implementing isNil

I am implementing a seemingly trivial utility function to check if a value is null or undefined. My original implementation looked like this: function isNil(value) { return value === null || value === undefined; } I then looked up Lodash's…
Jonathan.Brink
  • 23,757
  • 20
  • 73
  • 115
3
votes
3 answers

Testing Truthy or Falsy arguments passed through a function into an if statement

I am drawing a blank on this one too. Rather than provide an answer, I would appreciate if someone could help me understand why my code is not printing the expected output: def bool_to_str(bval): if bval is True: mytest = 'Yes' …
theeviininja
  • 157
  • 7
3
votes
1 answer

What does multiple %in% in a row test?

Consider: a <- c("a", "b", "c") b <- c("a", "e", "f") c <- c("a", "h", "i") > a %in% b %in% c [1] FALSE FALSE FALSE I would have expected this to evaluate to TRUE FALSE FALSE, since the first element in each vector is "a". Why is this not the…
histelheim
  • 4,938
  • 6
  • 33
  • 63
3
votes
3 answers

If ([] == false) is true, why does ([] || true) result in []?

Was just doing some testing and I find this odd: [] == false Gives true, this makes sense because double equal only compares contents and not type and tries to do type-coercion. But if its comparing contents and returns true, that means [ ] is…
olive_tree
  • 1,417
  • 16
  • 23
2
votes
1 answer

Can python date/time/datetime objects have a boolean value of False?

I ran into some code recently that was checking datetime objects and figured it could be cleaned up a little: # original if (datetime1 and (datetime2 is None)): do_things() # slightly cleaner version if (datetime1 and not datetime2): …
Sam Zuk
  • 309
  • 1
  • 3
  • 14