-
Notifications
You must be signed in to change notification settings - Fork 0
/
irbrc
61 lines (53 loc) · 1.34 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
require 'rubygems'
require 'pp'
=begin
Tips
_ (an underscore) is always set to the result of the last successful expression
(so you can't capture errors)
But this won't work:
%w{a b c}; _.map{|x| 'x'}. You have to do it in 2 lines.
=end
def is_rails_3?
Object.const_defined?(:Rails) && Rails::VERSION::STRING.to_i == 3
end
def is_rails_2?
Object.const_defined?(:Rails) && Rails::VERSION::STRING.to_i == 2
end
# To install all of these gems:
# gem install awesome_print map_by_method what_methods wirble hirb
%w{ap map_by_method what_methods wirble hirb}.each do |pkg|
begin
require pkg
rescue LoadError => err
$stderr.puts "Couldn't load something for irb: #{err}"
end
end
if is_rails_3?
unless Rails.logger
Rails.logger = ActiveSupport::BufferedLogger.new(STDOUT)
end
# Now you can do new_user_path inside `rails console`
include Rails.application.routes.url_helpers
elsif is_rails_2? and ! Object.const_defined?('RAILS_DEFAULT_LOGGER')
require 'logger'
Object.const_set('RAILS_DEFAULT_LOGGER', Logger.new(STDOUT))
end
# Monkeypatching ahoy!
class Class
# Get an object's "own methods" - methods that only it (not Object) has
def ownm
methods - Object.methods
end
end
class Array
def self.toy
[1, 2, 3] + %w{a b c}
end
end
class Hash
def self.toy
{'a' => 'b',
'foo' => 'bar',
1 => 2}
end
end