ick 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,34 @@
1
+ desc 'Release the website and new gem version'
2
+ task :deploy => [:check_version, :website, :release] do
3
+ puts "Remember to create SVN tag:"
4
+ puts "svn copy svn+ssh://#{rubyforge_username}@rubyforge.org/var/svn/#{PATH}/trunk " +
5
+ "svn+ssh://#{rubyforge_username}@rubyforge.org/var/svn/#{PATH}/tags/REL-#{VERS} "
6
+ puts "Suggested comment:"
7
+ puts "Tagging release #{CHANGES}"
8
+ end
9
+
10
+ desc 'Runs tasks website_generate and install_gem as a local deployment of the gem'
11
+ task :local_deploy => [:website_generate, :install_gem]
12
+
13
+ task :check_version do
14
+ unless ENV['VERSION']
15
+ puts 'Must pass a VERSION=x.y.z release version'
16
+ exit
17
+ end
18
+ unless ENV['VERSION'] == VERS
19
+ puts "Please update your version.rb to match the release version, currently #{VERS}"
20
+ exit
21
+ end
22
+ end
23
+
24
+ desc 'Install the package as a gem, without generating documentation(ri/rdoc)'
25
+ task :install_gem_no_doc => [:clean, :package] do
26
+ sh "#{'sudo ' unless Hoe::WINDOZE }gem install pkg/*.gem --no-rdoc --no-ri"
27
+ end
28
+
29
+ namespace :manifest do
30
+ desc 'Recreate Manifest.txt to include ALL files'
31
+ task :refresh do
32
+ `rake check_manifest | patch -p0 > Manifest.txt`
33
+ end
34
+ end
@@ -0,0 +1,7 @@
1
+ task :ruby_env do
2
+ RUBY_APP = if RUBY_PLATFORM =~ /java/
3
+ "jruby"
4
+ else
5
+ "ruby"
6
+ end unless defined? RUBY_APP
7
+ end
@@ -0,0 +1,17 @@
1
+ desc 'Generate website files'
2
+ task :website_generate => :ruby_env do
3
+ (Dir['website/**/*.txt'] - Dir['website/version*.txt']).each do |txt|
4
+ sh %{ #{RUBY_APP} script/txt2html #{txt} > #{txt.gsub(/txt$/,'html')} }
5
+ end
6
+ end
7
+
8
+ desc 'Upload website files to rubyforge'
9
+ task :website_upload do
10
+ host = "#{rubyforge_username}@rubyforge.org"
11
+ remote_dir = "/var/www/gforge-projects/#{PATH}/"
12
+ local_dir = 'website'
13
+ sh %{rsync -aCv #{local_dir}/ #{host}:#{remote_dir}}
14
+ end
15
+
16
+ desc 'Generate and upload website files'
17
+ task :website => [:website_generate, :website_upload, :publish_docs]
@@ -0,0 +1,7 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/../lib/ick/base'
3
+ require File.dirname(__FILE__) + '/../lib/ick/wrapped'
4
+ require File.dirname(__FILE__) + '/../lib/ick/guarded'
5
+ require File.dirname(__FILE__) + '/../lib/ick/sugar'
6
+
7
+ Ick.sugarize
@@ -0,0 +1,128 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class Box
4
+ attr_accessor :value
5
+
6
+ def initialize(_value)
7
+ self.value = _value
8
+ end
9
+
10
+ def == (other)
11
+ other.respond_to?(:value) && self.value == other.value
12
+ end
13
+
14
+ def to_s
15
+ "Box(#{value})"
16
+ end
17
+
18
+ def inspect
19
+ "Box(#{value})"
20
+ end
21
+
22
+ end
23
+
24
+ class TestIck < Test::Unit::TestCase
25
+
26
+ attr_accessor :value
27
+
28
+ def setup
29
+ @one = Box.new(1)
30
+ self.value = 2
31
+ @either_or = returning(Object.new) do |obj|
32
+ def obj.does
33
+ self
34
+ end
35
+ def obj.doesnt
36
+ nil
37
+ end
38
+ def obj.inspect
39
+ "either-or"
40
+ end
41
+ def obj.to_s
42
+ "either-or"
43
+ end
44
+ end
45
+ end
46
+
47
+ def test_let
48
+ assert_equal(1, let(@one) { |box| box.value } )
49
+ assert_equal(2, let(@one) { value } )
50
+ let(@one) { self.value = 3 }
51
+ assert_equal(3, self.value)
52
+ assert_equal(Box.new(1), @one)
53
+ end
54
+
55
+ def test_returning
56
+ assert_equal(Box.new(1), returning(@one) { |box| box.value } )
57
+ assert_equal(Box.new(1), returning(@one) { value } )
58
+ returning(@one) { self.value = 3 }
59
+ assert_equal(3, self.value)
60
+ assert_equal(Box.new(1), @one)
61
+ end
62
+
63
+ def test_my
64
+ assert_equal(1, my(@one) { |box| box.value } )
65
+ assert_equal(1, my(@one) { value } )
66
+ my(@one) { self.value = 3 }
67
+ assert_equal(2, self.value)
68
+ assert_equal(Box.new(3), @one)
69
+ end
70
+
71
+ def test_inside
72
+ assert_equal(Box.new(1), inside(@one) { |box| box.value } )
73
+ assert_equal(Box.new(1), inside(@one) { value } )
74
+ inside(@one) { self.value = 3 }
75
+ assert_equal(2, self.value)
76
+ assert_equal(Box.new(3), @one)
77
+ end
78
+
79
+ def test_maybe
80
+ assert_equal(@either_or, maybe(@either_or) do |e| e; end)
81
+ assert_equal(@either_or, maybe(@either_or) do |obj|
82
+ obj.does.does.does.does
83
+ end)
84
+ assert_nil(maybe(@either_or) do |obj|
85
+ obj.doesnt.doesnt.doesnt.doesnt.doesnt
86
+ end)
87
+ assert_equal(@either_or, maybe(@either_or) do |outer|
88
+ maybe(outer.does.does) do |inner|
89
+ inner.does.does.does
90
+ end
91
+ end)
92
+ end
93
+
94
+ def test_try
95
+ assert_equal(@either_or, try(@either_or) do |obj|
96
+ obj.does.does.does.does
97
+ end
98
+ )
99
+ assert_nil(try(@either_or) do |obj|
100
+ obj.never.never.never.never
101
+ end
102
+ )
103
+ end
104
+
105
+ def test_invocation_super_sugar
106
+ assert_nil(@either_or.maybe.doesnt)
107
+ assert_nil(
108
+ maybe(@either_or) do |obj|
109
+ obj.__send__(:doesnt)
110
+ end
111
+ )
112
+ assert_not_nil(
113
+ maybe(@either_or) do |obj|
114
+ obj.__send__(:does)
115
+ end
116
+ )
117
+ assert_not_nil(@either_or.maybe.does)
118
+ assert_nil(@either_or.try.never)
119
+ assert_not_nil(@either_or.try.does)
120
+ end
121
+
122
+ def test_please
123
+ assert_nil(
124
+ please(:foo) { bar.blitz }
125
+ )
126
+ end
127
+
128
+ end
@@ -0,0 +1,329 @@
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
2
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
4
+ <head>
5
+ <link rel="stylesheet" href="stylesheets/screen.css" type="text/css" media="screen" />
6
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
7
+ <title>
8
+ Invocation Construction Kit
9
+ </title>
10
+ <script src="javascripts/rounded_corners_lite.inc.js" type="text/javascript"></script>
11
+ <style>
12
+
13
+ </style>
14
+ <script type="text/javascript">
15
+ window.onload = function() {
16
+ settings = {
17
+ tl: { radius: 10 },
18
+ tr: { radius: 10 },
19
+ bl: { radius: 10 },
20
+ br: { radius: 10 },
21
+ antiAlias: true,
22
+ autoPad: true,
23
+ validTags: ["div"]
24
+ }
25
+ var versionBox = new curvyCorners(settings, document.getElementById("version"));
26
+ versionBox.applyCornersToAll();
27
+ }
28
+ </script>
29
+ </head>
30
+ <body>
31
+ <div id="main">
32
+
33
+ <h1>Invocation Construction Kit</h1>
34
+ <div id="version" class="clickable" onclick='document.location = "http://rubyforge.org/projects/ick"; return false'>
35
+ <p>Get Version</p>
36
+ <a href="http://rubyforge.org/projects/ick" class="numbers">0.0.2</a>
37
+ </div>
38
+ <h1>&#x2192; &#8216;ick&#8217;</h1>
39
+
40
+
41
+ <h2>What</h2>
42
+
43
+
44
+ <p>The Generalized Greenspun Rule: <em>Any sufficiently complicated platform contains an ad hoc, informally-specified, bug-ridden, slow implementation of half of a functional programming language.</em></p>
45
+
46
+
47
+ <p>While Ruby provides an ad hoc, informally-specified, bug-ridden, slow implementation of half of higher-order functional programming, it lacks an ad hoc, informally-specified, bug-ridden, slow implementation of half of Monads.</p>
48
+
49
+
50
+ <p>Thus, the <strong>Invocation Construction Kit</strong>, or &#8220;Ick!&#8221; Ick provides the tools needed to easily build your own execution abstractions like the &#8220;Maybe&#8221; monad or the four canonical block evaluators, as well as providing some sugar so you can write things like:</p>
51
+
52
+
53
+ <p><pre class='syntax'>
54
+ <span class="ident">please</span><span class="punct">(</span><span class="ident">sir</span><span class="punct">)</span> <span class="punct">{</span> <span class="ident">may</span><span class="punct">.</span><span class="ident">i</span><span class="punct">.</span><span class="ident">have</span><span class="punct">.</span><span class="ident">some</span><span class="punct">.</span><span class="ident">more</span> <span class="punct">}</span>
55
+ </pre></p>
56
+
57
+
58
+ <h2>Installing</h2>
59
+
60
+
61
+ <pre>sudo gem install ick</pre>
62
+
63
+ <h2>Block Structured Ruby</h2>
64
+
65
+
66
+ <p>Although Ruby borrows many of its features from Lisp and its syntax from Algol, it does not have block-local variables. In other words, if you declare a variable anywhere inside of a method, that variable is visible everywhere in that method. This is a problem, because it encourages writing methods where the instance variables create lot of dependencies between different expressions. Those methods can be hard to understand and refactor.</p>
67
+
68
+
69
+ <p>Ick solves this problem by providing four block structure methods: #let, #returning, #my, and #inside. These methods take an expression and bind it to a variable inside of a block. For example, if you want someone&#8217;s phone number only if they are a friend:</p>
70
+
71
+
72
+ <p><pre class='syntax'>
73
+ <span class="ident">let</span><span class="punct">(</span><span class="constant">Person</span><span class="punct">.</span><span class="ident">find</span><span class="punct">(</span><span class="symbol">:first</span><span class="punct">,</span> <span class="punct">...))</span> <span class="punct">{</span> <span class="punct">|</span><span class="ident">person</span><span class="punct">|</span> <span class="ident">person</span><span class="punct">.</span><span class="ident">phone_number</span> <span class="keyword">if</span> <span class="ident">person</span><span class="punct">.</span><span class="ident">friend?</span> <span class="punct">}</span>
74
+ </pre></p>
75
+
76
+
77
+ <p>This code makes it clear that you only need the <code>person</code> variable inside the block. If you want to refactor this code, you know that the entire expression can move without breaking another piece of code. We&#8217;ll elaborate on the differences between #let, #returning, #my, and #inside below.</p>
78
+
79
+
80
+ <p>(The four methods were inspired by <a href="http://blog.rubyenrails.nl/articles/2008/02/18/our-daily-method-10-object-r-rs-ds-s">Michiel de Mare&#8217;s post on the same subject</a>, although Ick&#8217;s nomenclature is not compatible with Michiel&#8217;s. Michiel&#8217;s #with, #tap, and #switch are called #returning, #let, and #inside in Ick. And Ick&#8217;s #my is fairly superfluous, it does exactly the same thing as Object#instance_eval.)</p>
81
+
82
+
83
+ <h2>Guarded Evaluation</h2>
84
+
85
+
86
+ <p>The example above is a common one. Sometimes we want to evaluate a chain of method calls without throwing a <code>NoMethodError</code> if one of the recipients is nil. Sometimes we want to send something a message if and only if it handles the method. There are lots of ad-hoc solutions, including <a href="http://andand.rubyforge.org">Object#andand</a>. What if you don&#8217;t want to install lots of different gems, one for each use?</p>
87
+
88
+
89
+ <p>Ick solves this problem by providing a structure for rolling your own guarded evaluation. You can check for nil, #respond_to?, custom permissions, whatever you like. It looks like this:</p>
90
+
91
+
92
+ <p><pre class='syntax'>
93
+ <span class="keyword">class </span><span class="class">Try</span> <span class="punct">&lt;</span> <span class="constant">Ick</span><span class="punct">::</span><span class="constant">Guarded</span>
94
+ <span class="ident">guard_with</span> <span class="punct">{</span> <span class="punct">|</span><span class="ident">value</span><span class="punct">,</span> <span class="ident">sym</span><span class="punct">|</span> <span class="ident">value</span><span class="punct">.</span><span class="ident">respond_to?</span><span class="punct">(</span><span class="ident">sym</span><span class="punct">)</span> <span class="punct">==</span> <span class="constant">true</span> <span class="punct">}</span>
95
+ <span class="ident">evaluates_in_calling_environment</span> <span class="keyword">and</span> <span class="ident">returns_result</span>
96
+ <span class="ident">belongs_to</span> <span class="constant">Object</span>
97
+ <span class="keyword">end</span>
98
+
99
+ <span class="ident">try</span><span class="punct">(...)</span> <span class="punct">{</span> <span class="punct">|</span><span class="ident">sir</span><span class="punct">|</span> <span class="ident">sir</span><span class="punct">.</span><span class="ident">may</span><span class="punct">.</span><span class="ident">i</span><span class="punct">.</span><span class="ident">have</span><span class="punct">.</span><span class="ident">some</span><span class="punct">.</span><span class="ident">more</span> <span class="punct">}</span>
100
+ </pre></p>
101
+
102
+
103
+ <p>(Try is built into Ick and was inspired by Chris Wanstrath&#8217;s <a href="http://ozmm.org/posts/try.html">try()</a> and Chalain&#8217;s <a href="http://chalain.livejournal.com/66798.html">Turtles</a>)</p>
104
+
105
+
106
+ <p>Maybe does exactly the same thing with checking nil rather than whether an object responds to a message:</p>
107
+
108
+
109
+ <p><pre class='syntax'>
110
+ <span class="ident">maybe</span><span class="punct">(...)</span> <span class="punct">{</span> <span class="punct">|</span><span class="ident">person</span><span class="punct">|</span> <span class="ident">person</span><span class="punct">.</span><span class="ident">manager</span><span class="punct">.</span><span class="ident">authority_level</span><span class="punct">.</span><span class="ident">permissions</span> <span class="punct">}</span>
111
+ </pre></p>
112
+
113
+
114
+ <p>Both #try and #maybe are <em>contagious</em>: everything in the chain inside the block is guarded.</p>
115
+
116
+
117
+ <h2>More sugar!</h2>
118
+
119
+
120
+ <p>If you just want to call a method by name without parameters, the existing blocks work well with Symbol#to_proc:</p>
121
+
122
+
123
+ <p><pre class='syntax'>
124
+ <span class="ident">maybe</span><span class="punct">(</span><span class="constant">Person</span><span class="punct">.</span><span class="ident">find</span><span class="punct">(</span><span class="symbol">:first</span><span class="punct">,</span> <span class="punct">...),</span> <span class="punct">&amp;</span><span class="symbol">:manager</span><span class="punct">)</span>
125
+ </pre></p>
126
+
127
+
128
+ <p>But you can also use these methods the way Object#andand works:</p>
129
+
130
+
131
+ <p><pre class='syntax'>
132
+ <span class="constant">Person</span><span class="punct">.</span><span class="ident">find</span><span class="punct">(</span><span class="symbol">:first</span><span class="punct">,</span> <span class="punct">...).</span>
133
+ <span class="ident">maybe</span><span class="punct">.</span><span class="ident">time_cards</span><span class="punct">.</span>
134
+ <span class="ident">maybe</span><span class="punct">.</span><span class="ident">map</span><span class="punct">(&amp;</span><span class="symbol">:hours_worked</span><span class="punct">).</span>
135
+ <span class="ident">maybe</span><span class="punct">.</span><span class="ident">inject</span><span class="punct">(</span><span class="number">0</span><span class="punct">,</span> <span class="punct">&amp;:+)</span>
136
+ </pre></p>
137
+
138
+
139
+ <p>When you do that, you have to keep calling the method in order to chain them all together, so you might prefer:</p>
140
+
141
+
142
+ <p><pre class='syntax'>
143
+ <span class="ident">maybe</span><span class="punct">(</span><span class="constant">Person</span><span class="punct">.</span><span class="ident">find</span><span class="punct">(</span><span class="symbol">:first</span><span class="punct">,</span> <span class="punct">...))</span> <span class="punct">{</span> <span class="punct">|</span><span class="ident">p</span><span class="punct">|</span>
144
+ <span class="ident">p</span><span class="punct">.</span><span class="ident">time_cards</span><span class="punct">.</span><span class="ident">map</span><span class="punct">(&amp;</span><span class="symbol">:hours_worked</span><span class="punct">).</span><span class="ident">inject</span><span class="punct">(</span><span class="number">0</span><span class="punct">,</span> <span class="punct">&amp;:+)</span>
145
+ <span class="punct">}</span>
146
+ </pre></p>
147
+
148
+
149
+ <p>The Object#andand-style syntax is most useful when you&#8217;re just using it for a single method invocation, such as:</p>
150
+
151
+
152
+ <p><pre class='syntax'>
153
+ <span class="constant">Person</span><span class="punct">.</span><span class="ident">find</span><span class="punct">(</span><span class="symbol">:first</span><span class="punct">,</span> <span class="punct">...).</span><span class="ident">maybe</span><span class="punct">.</span><span class="ident">salary</span> <span class="punct">=</span> <span class="number">42</span><span class="punct">,</span><span class="number">000</span>
154
+ </pre></p>
155
+
156
+
157
+ <h3>(But I heard that Ick is destroying Ruby&#8253;)</h3>
158
+
159
+
160
+ <p>Have no fear of that. Ick will not modify any classes without permission. Out of the box, you cannot call any of Ick&#8217;s built in methods the way you see them in these examples. Instead of <code>please(sir) {...}</code> you actually have to call <code>Ick::Please.instance.invoke(sir) {...}</code>. If you want to install one or more of the built-in methods in to the Object class, you call #belongs_to. For example, to install the Maybe and Let methods but no others:</p>
161
+
162
+
163
+ <p><pre class='syntax'>
164
+ <span class="constant">Ick</span><span class="punct">::</span><span class="constant">Maybe</span><span class="punct">.</span><span class="ident">belongs_to</span> <span class="constant">Object</span>
165
+ <span class="constant">Ick</span><span class="punct">::</span><span class="constant">Let</span><span class="punct">.</span><span class="ident">belongs_to</span> <span class="constant">Object</span>
166
+ </pre></p>
167
+
168
+
169
+ <p>You could also install some or all of the methods into a single class where you think you&#8217;ll be using them a lot but nowhere else:</p>
170
+
171
+
172
+ <p><pre class='syntax'>
173
+ <span class="keyword">class </span><span class="class">MyAwesomeImplementationOfAsteroids</span>
174
+ <span class="punct">[</span><span class="constant">Ick</span><span class="punct">::</span><span class="constant">Let</span><span class="punct">,</span> <span class="constant">Ick</span><span class="punct">::</span><span class="constant">Returning</span><span class="punct">,</span> <span class="constant">Ick</span><span class="punct">::</span><span class="constant">My</span><span class="punct">,</span> <span class="constant">Ick</span><span class="punct">::</span><span class="constant">Inside</span><span class="punct">].</span><span class="ident">each</span> <span class="keyword">do</span> <span class="punct">|</span><span class="ident">clazz</span><span class="punct">|</span>
175
+ <span class="ident">clazz</span><span class="punct">.</span><span class="ident">belongs_to</span> <span class="constant">self</span>
176
+ <span class="keyword">end</span>
177
+ <span class="keyword">end</span>
178
+ </pre></p>
179
+
180
+
181
+ <p>If you simply want everything you see here working exactly as it&#8217;s shown, simply call <code>Ick.sugarize</code> once and all of the built-in methods will be installed into Object for you. You can put that in your environment.rb file if you&#8217;re using Rails.</p>
182
+
183
+
184
+ <p>The point is, <code>try(program) { responsibly }</code>. You choose which classes to open and which methods to add. <a href="http://avdi.org/devblog/2008/02/25/full-disclosure/">All I’m saying is this: before re-opening a class, did you go through the rest of your toolbox first?</a></p>
185
+
186
+
187
+ <h2>More about the four block structures</h2>
188
+
189
+
190
+ <p>There are two binary decisions to be made about every block: First, do you want to evaluate the block in the calling environment (which is how almost every block is evaluated in Ruby), or do you want to evaluate the block in the value&#8217;s context. In other words, does <em>self</em> stay the same, or does it become the value in the block?</p>
191
+
192
+
193
+ <p>The methods #try and #maybe are both implemented as <em>evaluates_in_calling_environment</em>, because that is least surprising. But when you&#8217;re rolling your own, you might want to change that to make things more sugary. For example, here is a different version of #try:</p>
194
+
195
+
196
+ <p><pre class='syntax'>
197
+ <span class="keyword">class </span><span class="class">Please</span> <span class="punct">&lt;</span> <span class="constant">Ick</span><span class="punct">::</span><span class="constant">Guarded</span>
198
+ <span class="ident">guard_with</span> <span class="punct">{</span> <span class="punct">|</span><span class="ident">value</span><span class="punct">,</span> <span class="ident">sym</span><span class="punct">|</span> <span class="ident">value</span><span class="punct">.</span><span class="ident">respond_to?</span><span class="punct">(</span><span class="ident">sym</span><span class="punct">)</span> <span class="punct">==</span> <span class="constant">true</span> <span class="punct">}</span>
199
+ <span class="ident">evaluates_in_value_environment</span> <span class="keyword">and</span> <span class="ident">returns_result</span>
200
+ <span class="ident">belongs_to</span> <span class="constant">Object</span>
201
+ <span class="keyword">end</span>
202
+
203
+ <span class="ident">please</span><span class="punct">(...)</span> <span class="punct">{</span> <span class="ident">may</span><span class="punct">.</span><span class="ident">i</span><span class="punct">.</span><span class="ident">have</span><span class="punct">.</span><span class="ident">some</span><span class="punct">.</span><span class="ident">more</span> <span class="punct">}</span>
204
+ </pre></p>
205
+
206
+
207
+ <p>The method #please executes in the value&#8217;s environment, and thus it can call methods directly.</p>
208
+
209
+
210
+ <p>You already saw #let, it takes your expression and binds it to a parameter, then it evaluates a block in the calling environment, just as #try evaluates and guards its block in the calling environment. If you want something that behaves like #let but evaluates in the value&#8217;s environment just like #please, you can use #my:</p>
211
+
212
+
213
+ <p><pre class='syntax'>
214
+ <span class="ident">my</span><span class="punct">(</span><span class="constant">Person</span><span class="punct">.</span><span class="ident">find</span><span class="punct">(</span><span class="symbol">:first</span><span class="punct">,</span> <span class="punct">...))</span> <span class="keyword">do</span>
215
+ <span class="ident">first_name</span> <span class="punct">=</span> <span class="punct">'</span><span class="string">Charles</span><span class="punct">'</span>
216
+ <span class="ident">last_name</span> <span class="punct">=</span> <span class="punct">'</span><span class="string">Babbage</span><span class="punct">'</span>
217
+ <span class="ident">friends</span> <span class="punct">&lt;&lt;</span> <span class="punct">'</span><span class="string">Ada Lovelace</span><span class="punct">'</span>
218
+ <span class="keyword">end</span>
219
+ </pre></p>
220
+
221
+
222
+ <p>This will return Charles Babbage&#8217;s friends. On the surface, <em>evaluates_in_value_environment</em> is about the syntactic sugar of dropping an instance variable. But with a little thought, you can come up with some really cool way to (mis)use this capability.</p>
223
+
224
+
225
+ <p>So #let and #my both pass an expression to a block and return the result. Given that they both &#8216;declare&#8217; <em>returns_result</em>, this is not surprising. But there is another choice: <em>returns_value</em> instead of <em>returns_result</em>. Ruby on Rails includes the popular #returning method, and it works the same in Ick:</p>
226
+
227
+
228
+ <p><pre class='syntax'>
229
+ <span class="ident">returning</span><span class="punct">(</span><span class="constant">Person</span><span class="punct">.</span><span class="ident">find</span><span class="punct">(</span><span class="symbol">:first</span><span class="punct">,</span> <span class="punct">...))</span> <span class="keyword">do</span> <span class="punct">|</span><span class="ident">p</span><span class="punct">|</span>
230
+ <span class="ident">p</span><span class="punct">.</span><span class="ident">first_name</span> <span class="punct">=</span> <span class="punct">'</span><span class="string">Charles</span><span class="punct">'</span>
231
+ <span class="ident">p</span><span class="punct">.</span><span class="ident">last_name</span> <span class="punct">=</span> <span class="punct">'</span><span class="string">Babbage</span><span class="punct">'</span>
232
+ <span class="ident">p</span><span class="punct">.</span><span class="ident">friends</span> <span class="punct">&lt;&lt;</span> <span class="punct">'</span><span class="string">Ada Lovelace</span><span class="punct">'</span>
233
+ <span class="keyword">end</span>
234
+ </pre></p>
235
+
236
+
237
+ <p>This returns the person record, not the list of friends. The block is evaluated strictly for side effects. And what happens if we want to return the value and also evaluate in the value&#8217;s environment?</p>
238
+
239
+
240
+ <p><pre class='syntax'>
241
+ <span class="ident">inside</span><span class="punct">(</span><span class="constant">Person</span><span class="punct">.</span><span class="ident">find</span><span class="punct">(</span><span class="symbol">:first</span><span class="punct">,</span> <span class="punct">...))</span> <span class="keyword">do</span>
242
+ <span class="ident">first_name</span> <span class="punct">=</span> <span class="punct">'</span><span class="string">Charles</span><span class="punct">'</span>
243
+ <span class="ident">last_name</span> <span class="punct">=</span> <span class="punct">'</span><span class="string">Babbage</span><span class="punct">'</span>
244
+ <span class="ident">friends</span> <span class="punct">&lt;&lt;</span> <span class="punct">'</span><span class="string">Ada Lovelace</span><span class="punct">'</span>
245
+ <span class="keyword">end</span>
246
+ </pre></p>
247
+
248
+
249
+ <p>The method #inside returns the value and evaluates the block in the value&#8217;s environment.</p>
250
+
251
+
252
+ <h2>Under the Hood</h2>
253
+
254
+
255
+ <p>Ick is actually a construction kit. By all means install the gem and go wild with #let, #returning, #my, #inside, #try, and #maybe. But have a look under the hood. It&#8217;s easy to build your own methods.</p>
256
+
257
+
258
+ <h3>Every programming problem can be solved with another layer of abstraction, except the problem of too many layers of abstraction</h3>
259
+
260
+
261
+ <p>Ick uses classes and template methods to replicate what can be done in a few lines of explicit code. For example, Object#returning is implemented in Rails as:</p>
262
+
263
+
264
+ <p><pre class='syntax'>
265
+ <span class="keyword">class </span><span class="class">Object</span>
266
+ <span class="keyword">def </span><span class="method">returning</span><span class="punct">(</span><span class="ident">value</span><span class="punct">)</span>
267
+ <span class="keyword">yield</span><span class="punct">(</span><span class="ident">value</span><span class="punct">)</span>
268
+ <span class="ident">value</span>
269
+ <span class="keyword">end</span>
270
+ <span class="keyword">end</span>
271
+ </pre></p>
272
+
273
+
274
+ <p>So why bother with Ick? Well, Ick is a construction kit. if you want to make a method just like Object#returning, only <em>X</em> (for some value of X), you can&#8217;t do that without copying, pasting, and modifying. Ick&#8217;s classes are included specifically so you can subclass things and make your own new kinds of methods that are variations of the existing methods.</p>
275
+
276
+
277
+ <p>Thus, the extra abstraction is appropriate if you want to use the built-in methods as a starting point for your own exploratory programming. And if you don&#8217;t care, you just want the methods, by all means install the gem and just use them. Don&#8217;t worry about the implementation unless you identify it as a performance problem.</p>
278
+
279
+
280
+ <h3>Where do you want to go today?</h3>
281
+
282
+
283
+ <p>The point behind abstracting invocation and evaluation is that you can <em>separate concerns</em>. For example, which methods to chain is one concern. How to handle nil or an object that does not respond to a method is a separate concern. Should you raise and handle and exception? Return nil? log an error? Why should error handling and logging be intermingled with your code?</p>
284
+
285
+
286
+ <p>With Ick, you can separate the two issues. You can even make the handling pluggable. For example, if instead of calling #let you call your own method, you could sometimes invoke <code>Ick::Let</code> with a block and other times invoke your own handler, perhaps one that logs every method called.</p>
287
+
288
+
289
+ <p>Ick raises how you handle things to the level of first-class objects in Ruby, so you can mix and match and separate concerns as you see fit. Logging, permissions, error handling&#8230; These are some of the places you can take Ick. Have fun.</p>
290
+
291
+
292
+ <h2>Administrivia</h2>
293
+
294
+
295
+ <h3>How to submit patches</h3>
296
+
297
+
298
+ <p>Read the <a href="http://drnicwilliams.com/2007/06/01/8-steps-for-fixing-other-peoples-code/">8 steps for fixing other people&#8217;s code</a>.</p>
299
+
300
+
301
+ <p>The trunk repository is <code>svn://rubyforge.org/var/svn/ick/trunk</code> for anonymous access.</p>
302
+
303
+
304
+ <h3>License</h3>
305
+
306
+
307
+ <p>This code is free to use under the terms of the <a href="http://en.wikipedia.org/wiki/MIT_License"><span class="caps">MIT</span> license</a>.</p>
308
+
309
+
310
+ <h3>Shout Out</h3>
311
+
312
+
313
+ <p><a href="http://mcommons.com/">Mobile Commons</a>. Still Huge After All These Years.</p>
314
+
315
+
316
+ <h3>Contact</h3>
317
+
318
+
319
+ <p>Comments are welcome. Send an email to <a href="mailto:raganwald+rubyforge@gmail.com">Reginald Braithwaite</a>. And you can always visit <a href="http://weblog.raganwald.com/">weblog.raganwald.com</a> to see what&#8217;s cooking.</p>
320
+ <p class="coda">
321
+ <a href="http://weblog.raganwald.com/">Reginald Braithwaite</a>, 7th March 2008<br>
322
+ Theme extended from <a href="http://rb2js.rubyforge.org/">Paul Battley</a>
323
+ </p>
324
+ </div>
325
+
326
+ <!-- insert site tracking codes here, like Google Urchin -->
327
+
328
+ </body>
329
+ </html>