-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathirbrc
112 lines (94 loc) · 2.48 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
#!/usr/bin/ruby
require 'irb/completion'
require 'rubygems'
begin
require "pry"
Pry.start
exit
rescue LoadError
puts "=> Unable to load pry"
end
begin
require 'awesome_print'
AwesomePrint.irb!
rescue LoadError, ActiveSupport::DeprecationException
puts "=> Unable to load awesome_print"
end
IRB.conf[:SAVE_HISTORY] = 1000
IRB.conf[:HISTORY_FILE] = "#{ENV['HOME']}/.irb_history"
IRB.conf[:PROMPT_MODE] = :SIMPLE
IRB.conf[:USE_READLINE] = true
IRB.conf[:AUTO_INDENT] = true
# don't save duplicates
IRB.conf[:AT_EXIT].unshift Proc.new {
no_dups = []
Readline::HISTORY.each_with_index { |e,i|
begin
no_dups << e if Readline::HISTORY[i] != Readline::HISTORY[i+1]
rescue IndexError
end
}
Readline::HISTORY.clear
no_dups.each { |e|
Readline::HISTORY.push e
}
}
class Object
# list methods which aren't in superclass
def local_methods(obj = self)
(obj.methods - obj.class.superclass.instance_methods).sort
end
# 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
# Show SQL in console
# if ENV.include?('RAILS_ENV') && !Object.const_defined?('RAILS_DEFAULT_LOGGER')
# require 'logger'
# RAILS_DEFAULT_LOGGER = Logger.new(STDOUT)
# end
# From: https://github.com/lucapette/dotfiles/blob/master/irbrc
# method the return the methods not present on basic objects, good for
# investigations
class Object
def interesting_methods
(self.methods - Object.instance_methods).sort
end
end
# toys methods to play with.
# Stealed from https://gist.github.com/807492
class Array
def self.toy(n=10,&block)
block_given? ? Array.new(n,&block) : Array.new(n) {|i| i+1}
end
end
class Hash
def self.toy(n=10)
Hash[Array.toy(n).zip(Array.toy(n){|c| (96+(c+1)).chr})]
end
end
# detects a rails console, cares about version
def rails?(*args)
version=args.first
v2 = ($0 == 'irb' && ENV['RAILS_ENV'])
v3 = ($0 == 'script/rails' && Rails.env)
version == 2 ? v2 : version == 3 ? v3 : v2 || v3
end
# loading rails configuration if it is running as a rails console
load File.dirname(__FILE__) + '/.railsrc' if rails?