Skip to content

undefined.empty? is not true or false #609

Closed

Description

@mikebridge

I don't understand why this test is not returning "false":

    assert_template_result('else',
      '{% case a.empty? %}{% when true %}true{% when false %}false{% else %}else{% endcase %}',
      {})

The test is asserting that it's neither true nor false. Shouldn't this eval to false since nil != empty....?

Activity

  1. pushrax commented on Jul 6, 2015

    @pushrax
    Contributor

    a is not defined, so its value is nil. nil does not have an empty? property, so the result of the whole keypath is nil, which doesn't match any of the cases so the else is hit.

    It's a weird test though, since empty? is not actually a thing in Liquid (unless you expose it specifically in a drop or hash).

  2. mikebridge commented on Jul 6, 2015

    @mikebridge
    Author

    @pushrax Ah, so you're saying that the case statement does not actually compare based on truthiness, correct? I see that the following does not use truthiness as a basis for comparison either:

        {% case nil %}{% when false %}FALSE{% when true %}TRUE{%else%}NOT TRUE OR FALSE{% endcase %}
        --> NOT TRUE OR FALSE
    
        {% case 0 %}{% when true %}TRUE{% when false %}FALSE{%else%}NOT TRUE OR FALSE{% endcase %}
        --> NOT TRUE OR FALSE
    

    Without knowing that, I would have expected those to return "FALSE" and "TRUE", respectively....

  3. pushrax commented on Jul 6, 2015

    @pushrax
    Contributor

    It works similarly to ruby when using equality rather than truthiness (in ruby nil != false).

  4. mikebridge commented on Jul 6, 2015

    @mikebridge
    Author

    Ok, thanks for the clarification!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions