-
Notifications
You must be signed in to change notification settings - Fork 23
/
README
356 lines (217 loc) · 13.7 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
== Utility Belt
by Giles Bowkett (and many others)
http://utilitybelt.rubyforge.org
== DESCRIPTION
Utility Belt is a grab-bag of tricks, tools, techniques, trifles, and toys for IRB, including convenience methods, language patches, and useful extensions. It also includes a couple command-line widgets. Its primary inspirations were an awesome gem called Wirble and a blog post by Amy Hoy called "Secrets Of The Rails Console Ninjas".
(http://slash7.com/articles/2006/12/21/secrets-of-the-rails-console-ninjas)
== INSTALL
sudo gem install utility_belt
== FEATURES
* Interactively edit IRB code in your preferred text editor
* Read from and write to OS X clipboard
* Post your code to Pastie with one command (OS X only)
* Kick-ass Unix-style history buffer
* Write command history to file or vi
* Grep classes and methods for strings
* Verbosity controls for regular IRB and Rails console
* Finder shortcuts for Rails console
* Upload shortcut for Amazon S3
* Command-line Amazon S3 upload script
* Command-line Google shortcut (OS X only)
* Auto-indentation
* _ special variable (like Unix shell var !!)
* Extremely basic themes for Wirble syntax coloring
* Pascal/JavaScript-style "with" statement
* String#to_proc
* Add and subtract Hashes with + and -
* Grammatically-correct is_an? method - no more "is_a? Array" statements
* One-character exit command
== COMMUNITY EFFORT
The majority of this code was written by other people and compiled, nicked, nabbed, herded, gathered, copied, or pilfered by me (Giles Bowkett). See copyright notice for the credits. A lot of this code comes from blogs. Check out the source code for numerous links to the original posts the code came from. Thanks also to Bob Hutchison, Avdi Grimm, Marcus Derencius, Markus Prinz, Ben Bleything, Reginald Braithwaite-Lee, and Robert Berger for bug fixes, new features, documentation, and bug reports.
== SYNOPSIS
Utility Belt gives you a ton of new options and techniques in IRB. (You may find yourself putting its language patches in actual projects as well.) The way to use Utility Belt is to edit, or create, your .irbrc file. This is analogous to a Unix .bashrc, .tcshrc, .profile, or similar file.
Windows users have a couple options. The easiest is to define an environment variable called IRBRC and set it to the full path of your irbrc (e.g. "C:\Documents and Settings\<username>\_irbrc"). Presto, .irbrc for Windows. You could also set your HOME-Variable to any directory you want and put your ".irbrc" in there. (It needs to be called ".irbrc", "_irbrc" won't work). Since Windows Explorer won't let you create a file with a dot in the beginning, use any text editor (Notepad will do just fine) and "Save As" to save it as ".irbrc". (The quotation marks make the magic happen.) We don't recommend this if you're also using MSYS or Cygwin, as they use the same variable for their own purposes.
Adding Utility Belt to your .irbrc is easy:
require 'rubygems'
require 'utility_belt'
To use the Amazon shortcuts, you also need to set some environment variables. See the section on S3 under "Usage" for more. Also, if your terminal has a light background, you'll need to enable the light-background syntax coloring theme - see the section on themes under "Usage" for more.
== USAGE
Using each of Utility Belt's tools is different:
== Interactively edit IRB code in your preferred text editor
If your preferred text editor is vi(m), emacs, or TextMate, there are built-in commands: vi, emacs, and mate, respectively. To use another editor, use this command:
edit_interactively(:the_name_of_my_editor)
or
edit_interactively("the name of my editor")
or
edit_interactively("/the/path/to/my/editor")
If you're wondering why the method name is so ridiculously long, there's a story there:
http://gilesbowkett.blogspot.com/2007/12/utility-belt-vs-rails.html
== Read from and write to OS X or Windows clipboard
In order to use Clipboard on Windows you must install the 'win32-clipboard' gem.
To read:
Clipboard.read
To write:
Clipboard.write("something")
== Post your code to Pastie with one command (OS X and Windows only)
You must first equip the UtilityBelt with pastie in your .irbrc:
UtilityBelt.equip(:pastie)
This line will also automatically equip the Clipboard, which is required to use pastie.
First copy some code into your clipboard, or put it there with the Clipboard.write() class method. Then use the command:
pastie
or the shortcut version:
pst
Note that this method currently auto-formats all code as Ruby. Support for other languages is possible but not implemented yet. Also, this code is OS X and Windows only because it uses the Clipboard. (A platform-agnostic version without the clipboard aspect is certainly possible.)
== Unix-style history buffer
Use "h" or "history" to view your command history, and "h! 123" to re-invoke command #123 in your buffer. "h!" with no args will run the last command.
== Write command history to file or vi
To write your history to a file, use "history_write('/path/to/file')". To edit your history in vi, use "history_to_vi" or its shortcut alias "hvi".
== Grep classes and methods for strings
Classes:
>> grep_classes("ixnum")
#=> ["Fixnum"]
>> grep_classes("num")
#=> ["Bignum", "Fixnum", "Numeric", "REXML::SyncEnumerator"]
Methods:
>> "any arbitrary string".grep_methods("pretty")
#=> ["pretty_inspect", "pretty_print_instance_variables", "pretty_print_inspect", "pretty_print", "pretty_print_cycle"]
>> 32.grep_methods("*")
#=> ["*", "**"]
At some point it'll probably be a good idea to add regex support for these methods.
== Verbosity controls for regular IRB and Rails console
regular IRB: "verbose" or "v" / "quiet" or "q"
Rails: "log" method turns verbosity on, there isn't actually any method for turning it off. (Might add one at some point.)
== Finder shortcuts for Rails console
user(:all) / user(1)
instead of
User.find(:all) / User.find(1)
Supports arbitrarily complex finds.
== Upload shortcut for Amazon S3
First, define your secret keys in your environment variables:
export AMAZON_ACCESS_KEY_ID='foofoofoofoofoo'
export AMAZON_SECRET_ACCESS_KEY='barbarbarbarbar'
(Obviously, replace those with actual values.)
Then, you can use
aws_upload("bucket_name","filename")
To easily upload public-access files. This method will automatically connect to Amazon S3 for you, if your keys are defined in your environment variables. It also writes the resulting Amazon URL to your clipboard, if you're on OS X, so you can easily share the file with others. Note that you have to create the initial buckets manually - see amazon.rubyforge.org for details on that.
A good way to use this is actually to have a default bucket, and create a further method in your .irbrc file:
def my_bucket_name(filename)
aws_upload("my_default_bucket",filename)
end
== Command-line Amazon S3 upload script
Utility Belt also includes amazon, a simple command-line uploader for S3. It requires that you set your Amazon environment variables (see above). The usage is:
amazon -b my_bucket_name filename other_filename
(For any arbitrary number of filenames.)
Anytime you want to upload a file to your default bucket on S3, for example, the "images" bucket for your blog - or the "mp3" bucket if you're running a blog like Projectionist - all you have to do is
amazon -b blog_files some_file.name
Or
amazon --bucket blog_files some_file.name
And you're good to go. Of course, if that were too many characters to type, you could always take it one step further with a shell script. For instance, in bash:
alias b1="amazon --bucket my_main_bucket $1"
alias b2="amazon --bucket my_other_bucket $1"
And then you would only have to type:
b1 file.name
And of course tab completion will handle all the typing of the file name, assuming you choose your file names deliberately ahead of time to make tab completion effective, in which case all you have to type is "b1 f[tab]" and bam.
== Command-line Google shortcut (OS X only)
To pop open a new window in your default browser with Google search results for a given search term, do:
google "any given term"
For one-word terms, you can omit the quotes:
google term
If you already have the search term in your clipboard, you can just type:
google
(The last approach is useful for unusual command-line error messages.)
== Auto-indentation and tab completion
Just type as normally. This just enables built-in but underused functionality in IRB. (The IRB default auto-indentation isn't perfect, but it clobbers the living hell out of no indentation at all.)
== _ special variable (like Unix shell var !!)
More activation of built-in but underused functionality in IRB. _ functions like !! in Unix, with one subtle difference. Instead of re-issuing the previous command in your history, it gives you the last result of evaluating a command. This means when you start a new IRB session, _ won't go to your history, it'll just give you nil, and it means that some commands can't be repeated exactly. Nine out of ten times, however, it's pretty much the same thing.
Note that when combined with the h! in the command history code, you basically have two synonyms for !! here. Neither one is an exact replica, but between the two of them, you should have what you need.
== Extremely basic themes for Wirble syntax coloring
Utility Belt incorporates Wirble as a dependency. Wirble provides syntax coloring. Utility Belt allows you to choose between two syntax coloring themes: Wirble's default, and an alternate color set for terminals with light backgrounds. The default theme is enabled by default (duh), but if you have a terminal with a light background, you can enable the light-background theme just by putting the following line of code in your .irbrc:
UtilityBelt::Themes.background(:light)
To revert back to the default, just use this:
UtilityBelt::Themes.background(:dark)
To specify an entirely new syntax color scheme, see the source code for this gem or for Wirble.
== Pascal/JavaScript-style "with" statement
I use this with RSpec's spec_helper. Instead of:
Spec::Runner.configure do |config|
config.use_transactional_fixtures = true
config.use_instantiated_fixtures = false
config.fixture_path = RAILS_ROOT + '/spec/fixtures'
config.mock_with :flexmock
end
I do this:
Spec::Runner.configure do |config|
with(config) do
use_transactional_fixtures = true
use_instantiated_fixtures = false
fixture_path = RAILS_ROOT + '/spec/fixtures'
mock_with :flexmock
end
end
Note however that this blew up once and I have no idea why. Caveat emptor, et cetera.
== String#to_proc
Syntactic sugar for functional programming.
(1..3).map(&'*2') => [2, 4, 6]
(1..3).map(&'[-1, _, 0]') => [[-1, 1, 0], [-1, 2, 0], [-1, 3, 0]]
(1..3).map(&'x -> y -> x * y').map(&'[2]') => [2, 4, 6]
(1..5).select(&'>2') => [3, 4, 5]
(1..3).map(&'x -> y -> x * y').map(&'.call(2)') => [2, 4, 6]
[5].map(&"(1.._).inject(&'*')") => [120]
For more detail see: http://weblog.raganwald.com/2007/10/stringtoproc.html
== Add and subtract Hashes with + and -
{:a => :b} + {:c => :d} = {:a => :b, :c => :d}
{:a => :b, :c => :d} - {:c => :d} = {:a => :b}
{:a => :b, :c => :d} - :c = {:a => :b}
You could already do this with Arrays; now you can do it with Hashes as well. (I was kinda surprised the first time I tried to do this and realized I'd have to write it myself.)
== Grammatically-correct is_an? method - no more "is_a? Array" statements
>> [].is_an? Array
#=> true
Most people don't care, but personally, "is_a? Array" drives me completely insane.
== Object#not
Replace this:
if !response.incomplete? && !response.invalid? && response.total > 0
With this:
if response.not.incomplete? && response.not.invalid? && response.total > 0
Syntactic sugar for boolean conditions.
More detail here:
http://blog.jayfields.com/2007/08/ruby-adding-not-method-for-readability.html
== One-character exit command
x
Because life is too short to type whole words.
== REQUIREMENTS
* ReadLine
* Wirble
* Amazon S3
* Platform
* Some features require OS X
== LICENSE
(The MIT License)
Copyright (c) 2006-2007 Giles Bowkett
Portions Copyright (c) 2006-2007 Ben Bleything
Portions Copyright (c) 2006-2007 Reginald Braithwaite
Portions Copyright (c) 2006-2007 Gregory Brown
Portions Copyright (c) 2006-2007 Jamis Buck
Portions Copyright (c) 2006-2007 Mike Clark
Portions Copyright (c) 2006-2007 Jay Fields
Portions Copyright (c) 2005 David Heinemeier Hansson
Portions Copyright (c) 2006-2007 Ara T. Howard
Portions Copyright (c) 2006-2007 Pat Maddox
Portions Copyright (c) 2006-2007 Marcel Molina, Jr.
Portions Copyright (c) 2006-2007 Rick DeNatale
Portions Copyright (c) 2006-2007 Projectionist (Marcel Molina? Chad Fowler?)
Portions Copyright (c) 2006-2007 Dan Yoder
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.