forked from vivien/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
irbrc
138 lines (126 loc) · 3.41 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
# ----------------------------------------
# load gems
# ----------------------------------------
%w{rubygems hirb pp ap irb/ext/save-history irb/completion}.each do |lib|
begin
require lib
rescue LoadError => err
$stderr.puts "Couldn't load #{lib}: #{err}"
end
end
# ----------------------------------------
# Wirble - irb enhancements
# ----------------------------------------
begin
# load wirble
require 'wirble'
wirble_opts = {
:skip_prompt => true,
}
Wirble.init
Wirble.colorize
rescue LoadError => err
warn "Couldn't load Wirble: #{err}"
end
# ---------------------------------------
# Local methods for an object
# ---------------------------------------
class Object
# Return a list of methods defined locally for a particular object. Useful
# for seeing what it does whilst losing all the guff that's implemented
# by its parents (eg Object).
def local_methods(obj = self)
begin
(obj.instance_methods - obj.class.superclass.instance_methods).sort
rescue NoMethodError => err
(obj.methods - obj.class.superclass.methods).sort
end
end
end
# ---------------------------------------
# Benchmark block run time
# ---------------------------------------
def benchmark
# From http://blog.evanweaver.com/articles/2006/12/13/benchmark/
# Call benchmark { } with any block and you get the wallclock runtime
# as well as a percent change + or - from the last run
cur = Time.now
result = yield
print "#{cur = Time.now - cur} seconds"
puts " (#{(cur / $last_benchmark * 100).to_i - 100}% change)" rescue puts ""
$last_benchmark = cur
result
end
# Unix-like functions
# List current directory content, or a directory.
# You can give a symbol to be faster :)
def ls(arg = '*')
arg = arg.to_s if arg.is_a? Symbol
if File.directory? arg
Dir.chdir arg do
Dir['*']
end
else
Dir[arg]
end
end
# Change directory to home, or directory given.
# Like ls function, give a symbol to be faster ;)
def cd(arg = nil)
if arg.nil?
Dir.chdir
else
arg = arg.to_s if arg.is_a? Symbol
Dir.chdir arg
end
Dir.pwd
end
# Where am I?
def pwd
Dir.pwd
end
IRB.conf[:SAVE_HISTORY] = 1000
IRB.conf[:HISTORY_FILE] = "#{ENV['HOME']}/.irb-history"
IRB.conf[:AUTO_INDENT] = false
IRB.conf[:USE_READLINE] = true
IRB.conf[:PROMPT_MODE] = :DEFAULT
red = "[31m"
green = "[32m"
yellow = "[33m"
blue = "[34m"
cyan = "[36m"
bold = "[1m"
nobold = "[22m"
reset = "[0m"
IRB.conf[:PROMPT][:COLOR] = {
# normal prompt
:PROMPT_I => "#{reset}#{green}#{bold}%N(%m):#{reset}%03n#{green}:%i>#{reset} ",
:PROMPT_N => "#{reset}#{green}%N(%m):#{reset}%03n#{green}:%i>#{reset} ",
# prompt for continuing strings
:PROMPT_S => "#{reset}#{green}%N(%m):#{reset}%03n#{green}:%i%l#{reset} ",
# prompt for continuing statement
:PROMPT_C => "#{reset}#{green}%N(%m):#{reset}%03n#{green}:%i*#{reset} ",
# format to return value
#:RETURN => "#{reset}#{red}=> %s\n#{reset}"
:RETURN => "#{reset}#{red}=> %s\n#{reset}"
}
IRB.conf[:IRB_RC] = proc do |conf|
conf.prompt_mode = :COLOR
# conf.prompt_i = '>> '
# conf.prompt_c = '-> '
# conf.prompt_s = '-> '
end
def ri arg
puts `ri -Tf ansi #{arg}`
end
class Module
def ri(meth=nil)
if meth
if instance_methods(false).include? meth.to_s
puts `ri -Tf ansi #{self}##{meth}`
end
else
puts `ri #{self}`
end
end
end