Skip to content

Instantly share code, notes, and snippets.

module Kernel
def ai(options = {})
ap = AwesomePrint::Inspector.new(options)
awesome = ap.awesome self
if options[:html]
awesome = "<pre>#{awesome}</pre>"
awesome = awesome.html_safe if defined? ActiveSupport
end
awesome
def format(object, type = nil)
core_class = cast(object, type)
awesome = if core_class != :self
send(:"awesome_#{core_class}", object) # Core formatters.
else
awesome_self(object, type) # Catch all that falls back to object.inspect.
end
awesome
end
IRB::Irb.class_eval do
def output_value
puts 'cow'
end
end
module IRB
class Irb
def output_value
puts 'cow'
end
end
end
'dog'
=> cow
# lib/awesome_print/inspector.rb (26)
def irb!
return unless defined?(IRB)
unless IRB.version.include?("DietRB")
IRB::Irb.class_eval do
def output_value
ap @context.last_value
end
end
...
class C
@color1 = 'red'
puts @color1
end
=> red
=> nil
class C
@color2 = 'blue'
puts @color1
# case 1
class B
def method1
1
end
end
# case 2
class B
def method2
class A
@color = 'red'
puts @color
end
=> red
=> nil
class A
@color = 'blue'
puts @color
[1, 2, 3].to_s
=> "[1, 2, 3, 4]"
class Array
def to_s
'[]'
end
end
[1, 2, 3].to_s
@nassredean
nassredean / lookahead.rb
Created October 23, 2015 15:54
Prawn Lookahead Height
require "prawn"
@pdf = Prawn::Document.new(margin: 0)
@text = "Oh hai text rect. " * 10
@options = { :document => @pdf }
text_box = Prawn::Text::Box.new(@text, @options)
p "cursor before dry run: #{@pdf.cursor}"
p "whats the height before dry run?"
p "....who knows????" if text_box.height == 0