ifelse 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c7d4796475dd02819829cd329f5dcdc739a0a114
4
+ data.tar.gz: 0b22f61f3a82a23ad5909fb746db0a7a3d4f6310
5
+ SHA512:
6
+ metadata.gz: 748f230f79de2900a8f3f8e4ac5344513537671a5dcb7f33899ad4cd8af73f9366ef49b80c49c988c7e78a651e891d6adf9f19a3c46ebc66231ea29dd745e228
7
+ data.tar.gz: eca45115ca0d77196bec9ee28b463d108fd726ab616c5f74b23b755f504a0af89d32d73b12b13030454c6469da8c34b9d1957620946e08c944459129ed629a97
@@ -0,0 +1,23 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'autotest/restart'
4
+
5
+ # Autotest.add_hook :initialize do |at|
6
+ # at.extra_files << "../some/external/dependency.rb"
7
+ #
8
+ # at.libs << ":../some/external"
9
+ #
10
+ # at.add_exception 'vendor'
11
+ #
12
+ # at.add_mapping(/dependency.rb/) do |f, _|
13
+ # at.files_matching(/test_.*rb$/)
14
+ # end
15
+ #
16
+ # %w(TestA TestB).each do |klass|
17
+ # at.extra_class_map[klass] = "test/test_misc.rb"
18
+ # end
19
+ # end
20
+
21
+ # Autotest.add_hook :run_command do |at|
22
+ # system "rake build"
23
+ # end
File without changes
@@ -0,0 +1,4 @@
1
+ === 0.6.0 / 2013-12-17
2
+
3
+ * Initial release
4
+
@@ -0,0 +1,8 @@
1
+ .autotest
2
+ History.txt
3
+ Manifest.txt
4
+ README.rdoc
5
+ Rakefile
6
+ bin/ifelse
7
+ lib/ifelse.rb
8
+ test/test_ifelse.rb
@@ -0,0 +1,171 @@
1
+ = ifelse
2
+
3
+ https://github.com/jimwise/ifelse
4
+
5
+ Author:: Jim Wise (mailto:jwise@draga.com)
6
+ Copyright:: Copyright (c) 2013 Jim Wise
7
+ License:: 2-clause BSD-Style (see LICENSE.txt)
8
+
9
+ == DESCRIPTION:
10
+
11
+ IfElse is an implementation of the pure object-oriented conditional syntax
12
+ found in languages of the SmallTalk family, including Self. Those languages
13
+ distinguish themselves by taking the "everything is an object / everything
14
+ is a method" approach to a further extreme than Ruby, and getting rid of
15
+ almost all cases of special syntax other than object definition and method
16
+ call.
17
+
18
+ Ruby, of course, already works this way for some purposes -- thus most Ruby
19
+ developers prefer to write
20
+
21
+ [1, 17, 39].each {|x| puts x}
22
+
23
+ rather than
24
+
25
+ for x in [1, 17, 39]
26
+ puts x
27
+ end
28
+
29
+ and
30
+
31
+ 3.times {|n| puts n}
32
+
33
+ instead of
34
+
35
+ i = 1
36
+ while i <= 3
37
+ puts i
38
+ i += 1
39
+ end
40
+
41
+ This module extends that same preference to conditional statements,
42
+ providing replacements for the Ruby keywords +if+, and +unless+:
43
+
44
+ x = 1
45
+ (x >= 0).if {puts 'positive'}
46
+ (x < 0).unless {puts 'positive'}
47
+
48
+ Note that as with the built-in special forms these methods replace, these
49
+ methods are available on any Ruby Object, and obey the usual rules of which
50
+ values are considered "Truthy" and "Falsey".
51
+
52
+ <b>Note that the primary purpose of this gem is to demonstrate that the
53
+ built-in (special form) versions of conditionals provided with Ruby are
54
+ mostly syntactic sugar -- as with the +for+ keyword, there is no real need
55
+ for these to be built into the language. With that said, the gem is
56
+ fully tested, has no particular performance penalty (beyond the usual cost
57
+ of method dispatch), and should be fully useable in general purpose
58
+ code.</b>
59
+
60
+ <b>Note also that while Smalltalk-family languages also provide an
61
+ equivalent to the Ruby +else+ keyword, this depends on the more general
62
+ block/lambda capability of those languages, which allow a method to take
63
+ multiple blocks as arguments. This could be imitated with a syntax like:</b>
64
+
65
+ # NOT A REAL EXAMPLE
66
+ (x > 42).if then: lambda {|x| :big }, else: lambda {|x| :small}
67
+
68
+ <b>which is true to the SmallTalk original, but feels less Ruby-ish to me, so I
69
+ didn't implement this -- perhaps in a later version.</b>
70
+
71
+ == REQUIREMENTS:
72
+
73
+ IfElse has been tested on Ruby 1.9.3 and above (including 2.0.0), though it
74
+ should work fine on earlier versions (drop me an email[mailto:jwise@draga.com]
75
+ if you try it out there). IfElse has no other dependencies.
76
+
77
+ == DETAILS:
78
+
79
+ === Basic Usage
80
+
81
+ This class provides two methods, implemented on every ruby Object (but see
82
+ NOTES, below):
83
+
84
+ +#if+:: This method takes a block, which will be executed only if the object
85
+ it is called on has a "Truthy" value (neither +false+ nor +nil+ --
86
+ but see below). The block may take one argument, which will be
87
+ passed the value which was tested, if present. The return value of
88
+ this method is the value of the block, if it is invoked, or the
89
+ original (Falsey) value the method was called on otherwise. For
90
+ example:
91
+
92
+ h[:foo].if {|x| puts x} # outputs the value of h[:foo], if present
93
+
94
+ +#unless+:: This method takes a block, which will be executed only if the
95
+ object it is called on has a "Falsey" value (either +false+ or +nil+
96
+ -- but see below). The block may take one argument, which will be
97
+ passed the value which was tested, if present. The return value of
98
+ this method is the value of the block, if it is invoked, or the
99
+ original (Truthy) value the method was called on otherwise. For
100
+ example:
101
+
102
+ h[:bar].unless {|x| puts 'missing'} # outputs 'missing' if key :bar
103
+ # is not present in h
104
+
105
+ === Advanced Usage
106
+
107
+ As a demonstration of the added flexibility provided by replacing special
108
+ syntax with an object-oriented approach, this gem provides one additional
109
+ feature: <b>when defining a class, you may decide for yourself whether values
110
+ of that class should be considered "Truthy" or "Falsey"</b>.
111
+
112
+ This is done by including the Modules +IfElse::TrueIfBehavior+ or
113
+ +IfElse::FalseIfBehavior+ in your class (of course, values other than
114
+ 'false' and 'nil' are true by default, so you will normally only need to use
115
+ the latter. For example, after:
116
+
117
+ class List
118
+ ...
119
+ end
120
+
121
+ class Empty List < List
122
+ include IfElse::FalseIfBehavior
123
+
124
+ ...
125
+ end
126
+
127
+ Values of class +EmptyList+ are now Falsey for the purposes of #if and
128
+ #unless, while values of type +List+ remain Truthy.
129
+
130
+ === NOTES:
131
+
132
+ [*] That is, any subclass of Object -- these forms are not available on
133
+ classes which only subclass BasicObject, to ease implementation.
134
+
135
+ == INSTALL:
136
+
137
+ $ gem install ifelse
138
+
139
+ == DEVELOPERS:
140
+
141
+ After checking out the source, run:
142
+
143
+ $ rake newb
144
+
145
+ This task will install any missing dependencies, run the tests/specs,
146
+ and generate the RDoc.
147
+
148
+ == LICENSE:
149
+
150
+ (The MIT License)
151
+
152
+ Copyright (c) 2013 FIX
153
+
154
+ Permission is hereby granted, free of charge, to any person obtaining
155
+ a copy of this software and associated documentation files (the
156
+ 'Software'), to deal in the Software without restriction, including
157
+ without limitation the rights to use, copy, modify, merge, publish,
158
+ distribute, sublicense, and/or sell copies of the Software, and to
159
+ permit persons to whom the Software is furnished to do so, subject to
160
+ the following conditions:
161
+
162
+ The above copyright notice and this permission notice shall be
163
+ included in all copies or substantial portions of the Software.
164
+
165
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
166
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
167
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
168
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
169
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
170
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
171
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,20 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'rubygems'
4
+ require 'hoe'
5
+
6
+ # Hoe.plugin :compiler
7
+ # Hoe.plugin :gem_prelude_sucks
8
+ # Hoe.plugin :inline
9
+ # Hoe.plugin :minitest
10
+ # Hoe.plugin :racc
11
+ # Hoe.plugin :rcov
12
+ # Hoe.plugin :rubyforge
13
+
14
+ Hoe.spec 'ifelse' do
15
+ developer 'Jim Wise', 'jwise@draga.com'
16
+ license 'BSD'
17
+ self.readme_file = 'README.rdoc'
18
+ end
19
+
20
+ # vim: syntax=ruby
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ abort "you need to write me"
@@ -0,0 +1,40 @@
1
+ module IfElse
2
+ VERSION = '0.6.0'
3
+
4
+ module TrueIfBehavior
5
+ def if
6
+ raise "Syntax Error: if without block" unless block_given?
7
+ yield self
8
+ end
9
+
10
+ def unless
11
+ raise "Syntax Error: unless without block" unless block_given?
12
+ false
13
+ end
14
+ end
15
+
16
+ module FalseIfBehavior
17
+ def if
18
+ raise "Syntax Error: if without block" unless block_given?
19
+ self
20
+ end
21
+
22
+ def unless
23
+ raise "Syntax Error: unless without block" unless block_given?
24
+ yield self
25
+ end
26
+ end
27
+ end
28
+
29
+
30
+ class Object
31
+ include IfElse::TrueIfBehavior
32
+ end
33
+
34
+ class FalseClass
35
+ include IfElse::FalseIfBehavior
36
+ end
37
+
38
+ class NilClass
39
+ include IfElse::FalseIfBehavior
40
+ end
@@ -0,0 +1,32 @@
1
+ require "test/unit"
2
+ require "ifelse"
3
+
4
+ class TestIfElse < Test::Unit::TestCase
5
+
6
+ def test_if
7
+ assert_equal 42, true.if {42}
8
+ assert_equal 42, 3.if {42}
9
+ assert_false false.if {42}
10
+ end
11
+
12
+ def test_unless
13
+ assert_false true.unless {42}
14
+ assert_false 3.unless {42}
15
+ assert_equal 42, false.unless {42}
16
+ end
17
+
18
+ def test_errors
19
+ assert_raise { true.if }
20
+ assert_raise { false.if }
21
+ end
22
+
23
+ def test_readme
24
+ x = 1
25
+ assert_equal 'positive', (x >= 0).if {'positive'}
26
+ assert_equal 'positive', (x < 0).unless {'positive'}
27
+
28
+ h = {a: 42}
29
+ assert_equal 42, h[:a].if {|n| n}
30
+ assert_equal 'missing', h[:b].unless {'missing'}
31
+ end
32
+ end
metadata ADDED
@@ -0,0 +1,148 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ifelse
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.6.0
5
+ platform: ruby
6
+ authors:
7
+ - Jim Wise
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-12-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rdoc
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: hoe
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.7'
41
+ description: |-
42
+ IfElse is an implementation of the pure object-oriented conditional syntax
43
+ found in languages of the SmallTalk family, including Self. Those languages
44
+ distinguish themselves by taking the "everything is an object / everything
45
+ is a method" approach to a further extreme than Ruby, and getting rid of
46
+ almost all cases of special syntax other than object definition and method
47
+ call.
48
+
49
+ Ruby, of course, already works this way for some purposes -- thus most Ruby
50
+ developers prefer to write
51
+
52
+ [1, 17, 39].each {|x| puts x}
53
+
54
+ rather than
55
+
56
+ for x in [1, 17, 39]
57
+ puts x
58
+ end
59
+
60
+ and
61
+
62
+ 3.times {|n| puts n}
63
+
64
+ instead of
65
+
66
+ i = 1
67
+ while i <= 3
68
+ puts i
69
+ i += 1
70
+ end
71
+
72
+ This module extends that same preference to conditional statements,
73
+ providing replacements for the Ruby keywords +if+, and +unless+:
74
+
75
+ x = 1
76
+ (x >= 0).if {puts 'positive'}
77
+ (x < 0).unless {puts 'positive'}
78
+
79
+ Note that as with the built-in special forms these methods replace, these
80
+ methods are available on any Ruby Object, and obey the usual rules of which
81
+ values are considered "Truthy" and "Falsey".
82
+
83
+ <b>Note that the primary purpose of this gem is to demonstrate that the
84
+ built-in (special form) versions of conditionals provided with Ruby are
85
+ mostly syntactic sugar -- as with the +for+ keyword, there is no real need
86
+ for these to be built into the language. With that said, the gem is
87
+ fully tested, has no particular performance penalty (beyond the usual cost
88
+ of method dispatch), and should be fully useable in general purpose
89
+ code.</b>
90
+
91
+ <b>Note also that while Smalltalk-family languages also provide an
92
+ equivalent to the Ruby +else+ keyword, this depends on the more general
93
+ block/lambda capability of those languages, which allow a method to take
94
+ multiple blocks as arguments. This could be imitated with a syntax like:</b>
95
+
96
+ # NOT A REAL EXAMPLE
97
+ (x > 42).if then: lambda {|x| :big }, else: lambda {|x| :small}
98
+
99
+ <b>which is true to the SmallTalk original, but feels less Ruby-ish to me, so I
100
+ didn't implement this -- perhaps in a later version.</b>
101
+ email:
102
+ - jwise@draga.com
103
+ executables:
104
+ - ifelse
105
+ extensions: []
106
+ extra_rdoc_files:
107
+ - History.txt
108
+ - Manifest.txt
109
+ - README.rdoc
110
+ files:
111
+ - ".autotest"
112
+ - History.txt
113
+ - Manifest.txt
114
+ - README.rdoc
115
+ - Rakefile
116
+ - bin/ifelse
117
+ - lib/ifelse.rb
118
+ - test/test_ifelse.rb
119
+ - ".gemtest"
120
+ homepage: https://github.com/jimwise/ifelse
121
+ licenses:
122
+ - BSD
123
+ metadata: {}
124
+ post_install_message:
125
+ rdoc_options:
126
+ - "--main"
127
+ - README.rdoc
128
+ require_paths:
129
+ - lib
130
+ required_ruby_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ required_rubygems_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ requirements: []
141
+ rubyforge_project: ifelse
142
+ rubygems_version: 2.0.14
143
+ signing_key:
144
+ specification_version: 4
145
+ summary: IfElse is an implementation of the pure object-oriented conditional syntax
146
+ found in languages of the SmallTalk family, including Self
147
+ test_files:
148
+ - test/test_ifelse.rb