Code Monkey home page Code Monkey logo

andand2's People

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

andand2's Issues

Value returned by #andand does not evaluate to false

It looks like you meant for the .andand method chain to return nil when a method does not exist, but that is not the case. It returns and AndAnd wrapper object. This means you can't use .andand naked on conditional statement. You must compare the result of the method chain to something, and that something can't be nil, since that is not what .andand returns.

rb(main):037:0> x = "".andand.foo
=> nil
irb(main):038:0> x
=> nil
irb(main):039:0> x ? 1 : 2
=> 1
irb(main):040:0> nil ? 1 : 2
=> 2
irb(main):041:0> nil == x
=> false

The problem is that a .andand method chain with a missing method never returns nil. It only ever returns another AndAnd wrapper object. Ruby internally checks whether the value of a condition is nil when evaluating the truth of the condition. And since AndAnd objects are not nil, then the condition is never false.

Also, storing internally nil in @@and_and_nil and then overriding the instance's method_missing method is both unnecessary and dangerous. Its unnecessary because nil is a singleton, thus you can always refer the single instance with nil and @@and_and_nil is never actually returned. And its dangerous because you are overriding method_missing in the singleton.

irb(main):001:0> require 'andand2'
=> true
irb(main):002:0> nil.foo
=> nil

The problem with not returning nil can probably be solved by adding an .andend method to Object bookend the method chain. If the method is invoked on an AndAnd instance, then it would return nil, otherwise it would return self.

Thus "".andand.foo.andend would return nil, whereas "ABC".andand.downcase.andend would return abc.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.