coalesce 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,55 @@
1
+ # Coalesce
2
+
3
+ ## Ruby, as a community, unfairly discriminates against `false`, and it needs to stop.
4
+
5
+ Do you write code like this? `val = maybe_nil || fallback`
6
+
7
+ If so, **you are part of the problem!**
8
+
9
+ `false` is a perfectly non-nil object, and you're excluding it out of convenience!
10
+
11
+ ## C#'s ?? operator
12
+
13
+ The null coalescing operator `??` is a C# idiom that provides a concise fallback mechanism for expressions that return `null`.
14
+
15
+ Object maybeObject = null;
16
+ Object fallbackObject = new Object();
17
+ Object obj = maybeObject ?? fallbackObject;
18
+
19
+ In Ruby, we generally write this:
20
+
21
+ maybe_object = nil
22
+ fallback_object = Object.new
23
+ obj = maybe_object || fallback_object
24
+
25
+ However, this idiom is only an approximation, and introduces subtle bugs when we try to allow false values:
26
+
27
+ maybe_val = false # assumed to be a valid value
28
+ fallback_val = 5
29
+ val = maybe_val || fallback_val # => 5, whoops!
30
+
31
+ ## Meet the _? method
32
+
33
+ With `coalesce`, you can write this instead:
34
+
35
+ maybe_val = false # assumed to be a valid value
36
+ fallback_val = 5
37
+ val = maybe_val._? fallback_val # => false, like we expected!
38
+
39
+ And if you still need the short-circuiting behavior of the `||` operator, you can pass a block instead:
40
+
41
+ val = maybe_val._? { generate_fallback(data) }
42
+
43
+ ## Benefits
44
+
45
+ I've noticed a positive side-effect of using `_?` is that I explicitly call out when I'm counting on the short-circuiting behavior, and that awareness of the short-circuiting behavior is causing me to use it in ways I would never use `||`.
46
+
47
+ For example, using it to assert an object's existence before returning it.
48
+
49
+ def give_me_the_data
50
+ @data_finder.try_to_find_data._? { raise "Oh no, we suck at finding data!" }
51
+ end
52
+
53
+ I probably wouldn't think to use a `||` there, and it might look weird, especially with a more complex expression.
54
+
55
+ And, of course, I'm grateful to be reunited with my long lost friend, `false`.
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.name = 'coalesce'
5
+ gem.description = "Adds a nil-coalescing method to Ruby, similar to C#'s ?? null-coalescing operator"
6
+ gem.homepage = "https://github.com/kibiz0r/#{gem.name}"
7
+ gem.version = '0.0.1'
8
+
9
+ gem.authors = ['Michael Harrington']
10
+ gem.email = ['kibiz0r@gmail.com']
11
+
12
+ gem.files = `git ls-files`.split($\)
13
+ gem.require_paths = ['lib']
14
+
15
+ gem.summary = <<-END.gsub(/^ +/, '')
16
+ foo || bar is a nice idiom for returning bar if foo is nil. Unfortunately, it also returns bar if foo is false.
17
+ foo._? bar is a clean alternative that respects false values.
18
+ foo._? { bar } allows you to keep the short-circuiting behavior of the || operator.
19
+ END
20
+ end
@@ -0,0 +1,3 @@
1
+ require 'bubble-wrap/loader'
2
+
3
+ BubbleWrap.require 'lib/coalesce.rb'
@@ -0,0 +1,16 @@
1
+ class Object
2
+ def _?(x = nil)
3
+ self
4
+ end
5
+ end
6
+
7
+ class NilClass
8
+ def _?(x = nil)
9
+ if block_given?
10
+ yield
11
+ else
12
+ x
13
+ end
14
+ end
15
+ end
16
+
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: coalesce
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Michael Harrington
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2012-09-11 00:00:00 -04:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: Adds a nil-coalescing method to Ruby, similar to C#'s ?? null-coalescing operator
22
+ email:
23
+ - kibiz0r@gmail.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - README.md
32
+ - Rakefile
33
+ - coalesce.gemspec
34
+ - lib/bubble-wrap/coalesce.rb
35
+ - lib/coalesce.rb
36
+ has_rdoc: true
37
+ homepage: https://github.com/kibiz0r/coalesce
38
+ licenses: []
39
+
40
+ post_install_message:
41
+ rdoc_options: []
42
+
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ segments:
50
+ - 0
51
+ version: "0"
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ segments:
57
+ - 0
58
+ version: "0"
59
+ requirements: []
60
+
61
+ rubyforge_project:
62
+ rubygems_version: 1.3.6
63
+ signing_key:
64
+ specification_version: 3
65
+ summary: foo || bar is a nice idiom for returning bar if foo is nil. Unfortunately, it also returns bar if foo is false. foo._? bar is a clean alternative that respects false values. foo._? { bar } allows you to keep the short-circuiting behavior of the || operator.
66
+ test_files: []
67
+