-
Notifications
You must be signed in to change notification settings - Fork 6
/
.irbrc
206 lines (177 loc) · 5.03 KB
/
.irbrc
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
require 'rubygems'
require 'irb/completion'
require 'irb/ext/save-history'
# For vim integration
begin
require 'interactive_editor'
rescue LoadError
puts "interactive_editor is not installed. To enable, run: gem install interactive_editor"
end
# http://drnicutilities.rubyforge.org/map_by_method/
begin
require 'map_by_method'
rescue LoadError
puts "map_by_method is not installed. To enable, run: gem install map_by_method"
end
# Dr Nic's gem inspired by
# http://redhanded.hobix.com/inspect/stickItInYourIrbrcMethodfinder.html
begin
require 'what_methods'
rescue LoadError
puts "what_methods is not installed. To enable, run: gem install what_methods"
end
# Pretty Print method
require 'pp'
# Awesome Print gem (gem install awesome_print)
begin
require 'awesome_print'
rescue LoadError
puts "awesome_print is not installed. To enable, run: gem install awesome_print"
end
# Print information about any HTTP requests being made
begin
require 'net-http-spy'
rescue LoadError
puts "net-http-spy is not installed. To enable, run: gem install net-http-spy"
end
# Draw ASCII tables
begin
require 'hirb'
require 'hirb/import_object'
Hirb.enable
extend Hirb::Console
rescue LoadError
puts "hirb is not installed. To enable, run: gem install hirb"
end
begin
# 'lp' to show method lookup path
require 'looksee'
rescue LoadError
puts "looksee is not installed. To enable, run: gem install looksee"
end
IRB.conf[:SAVE_HISTORY] = 1000
IRB.conf[:HISTORY_FILE] = "#{ENV['HOME']}/.irb_history"
# Remove the annoying irb(main):001:0 and replace with >>
IRB.conf[:PROMPT_MODE] = :SIMPLE
# Load the readline module.
IRB.conf[:USE_READLINE] = true
# Automatic Indentation
IRB.conf[:AUTO_INDENT]=true
# Save History between irb sessions
require 'irb/ext/save-history'
IRB.conf[:SAVE_HISTORY] = 100
IRB.conf[:HISTORY_FILE] = "#{ENV['HOME']}/.irb-save-history"
# Wirble is a set of enhancements for irb
# http://pablotron.org/software/wirble/README
# Implies require 'pp', 'irb/completion', and 'rubygems'
begin
require 'wirble'
Wirble.init
# Enable colored output
Wirble.colorize
rescue LoadError
puts "wirble is not installed. To enable, run: gem install wirble"
end
# Clear the screen
def clear
system 'clear'
if ENV['RAILS_ENV']
return "Rails environment: " + ENV['RAILS_ENV']
else
return "No rails environment - happy hacking!";
end
end
# Load / reload files faster
# http://www.themomorohoax.com/2009/03/27/irb-tip-load-files-faster
def fl(file_name)
file_name += '.rb' unless file_name =~ /\.rb/
@@recent = file_name
load "#{file_name}"
end
def rl
fl(@@recent)
end
# Reload the file and try the last command again
# http://www.themomorohoax.com/2009/04/07/ruby-irb-tip-try-again-faster
def rt
rl
eval(choose_last_command)
end
# prevent 'rt' itself from recursing.
def choose_last_command
real_last = Readline::HISTORY.to_a[-2]
real_last == 'rt' ? @@saved_last : (@@saved_last = real_last)
end
# Method to pretty-print object methods
# Coded by sebastian delmont
# http://snippets.dzone.com/posts/show/2916
class Object
ANSI_BOLD = "\033[1m"
ANSI_RESET = "\033[0m"
ANSI_LGRAY = "\033[0;37m"
ANSI_GRAY = "\033[1;30m"
# Print object's methods
def pm(*options)
methods = self.methods
methods -= Object.methods unless options.include? :more
filter = options.select {|opt| opt.kind_of? Regexp}.first
methods = methods.select {|name| name =~ filter} if filter
data = methods.sort.collect do |name|
method = self.method(name)
if method.arity == 0
args = "()"
elsif method.arity > 0
n = method.arity
args = "(#{(1..n).collect {|i| "arg#{i}"}.join(", ")})"
elsif method.arity < 0
n = -method.arity
args = "(#{(1..n).collect {|i| "arg#{i}"}.join(", ")}, ...)"
end
klass = $1 if method.inspect =~ /Method: (.*?)#/
[name, args, klass]
end
max_name = data.collect {|item| item[0].size}.max
max_args = data.collect {|item| item[1].size}.max
data.each do |item|
print " #{ANSI_BOLD}#{item[0].to_s.rjust(max_name)}#{ANSI_RESET}"
print "#{ANSI_GRAY}#{item[1].ljust(max_args)}#{ANSI_RESET}"
print " #{ANSI_LGRAY}#{item[2]}#{ANSI_RESET}\n"
end
data.size
end
end
## http://sketches.rubyforge.org/
#begin
#require 'sketches'
#Sketches.config :editor => 'mvim'
#rescue LoadError
#puts "sketches is not installed. To enable, run: gem install sketches"
#end
# Easily print methods local to an object's class
class Object
def local_methods
(methods - Object.instance_methods).sort
end
end
class Object
# print documentation
#
# ri 'Array#pop'
# Array.ri
# Array.ri :pop
# arr.ri :pop
def ri(method = nil)
unless method && method =~ /^[A-Z]/ # if class isn't specified
klass = self.kind_of?(Class) ? name : self.class.name
method = [klass, method].compact.join('#')
end
puts `ri '#{method}'`
end
end
def copy(str)
IO.popen('pbcopy', 'w') { |f| f << str.to_s }
end
def paste
`pbpaste`
end
load File.dirname(__FILE__) + '/.railsrc' if $0 == 'irb' && ENV['RAILS_ENV']