-
-
Notifications
You must be signed in to change notification settings - Fork 64
/
irbrc
110 lines (100 loc) · 2.83 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
require 'irb/completion'
require 'irb/ext/save-history'
IRB.conf[:SAVE_HISTORY] = 1000
IRB.conf[:HISTORY_FILE] = "#{ENV['HOME']}/.irb_history"
IRB.conf[:PROMPT_MODE] = :SIMPLE
IRB.conf[:AUTO_INDENT] = true
if Kernel.const_defined?("Rails")
def show_logs
Rails.logger = Logger.new(STDOUT)
end
end
if defined?(Rails::Console)
def show_mongo
return false unless defined?(Moped)
if Moped.logger == Rails.logger
Moped.logger = Logger.new(STDOUT)
true
else
Moped.logger = Rails.logger
false
end
end
alias show_moped show_mongo
def show_active_record
return false unless defined?(ActiveRecord)
ActiveRecord::Base.logger = Logger.new(STDOUT)
true
end
alias show_ar show_active_record
end
# Simulate HISTCONTROL=erasedups behavior from bash
# https://stackoverflow.com/a/47919632/8985
class EraseDupsInputMethod < IRB::ReadlineInputMethod
def gets
super.tap do # super adds line to HISTORY
HISTORY.pop if HISTORY[-1] == HISTORY[-2]
end
end
end
IRB.conf[:SCRIPT] = EraseDupsInputMethod.new
# 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 pretty_methods(*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
class Object
def local_methods
(methods - Object.instance_methods).sort
end
def local_readers(regex = nil)
methods = local_methods.select do |name|
arity = method(name).arity == 0
match = regex ? name =~ regex : true
arity && match
end
methods.each do |name|
print name.to_s.rjust(20)
print " : "
begin
puts send(name).to_s.truncate(80)
rescue => e
puts e.message
end
end
methods.size
end
end