burke / andand2 Goto Github PK
View Code? Open in Web Editor NEW[abandoned] Just like andand, only better.
License: MIT License
[abandoned] Just like andand, only better.
License: MIT License
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
.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.