Di sini, tambahkan ini ke ~ / .irbrc Anda:
require 'ctx'
require 'awesome_print'
module IRB
class Irb
ctx :ap do
def output_value()
ap(@context.last_value)
end
end
ctx :puts do
def output_value()
puts(@context.last_value)
end
end
ctx :p do
def output_value()
p(@context.last_value)
end
end
ctx :quiet do
def output_value()
end
end
end
end
def irb_mode(mode)
ctx(mode) { irb }
end
(Catatan: Anda harus menginstal ctxpermata terlebih dahulu, meskipun awesome_printopsional, tentu saja.)
Sekarang ketika Anda berada di konsol mana pun yang menggunakan irb, Anda dapat melakukan hal berikut:
Mode normal:
irb(main):001:0> { this:'is a complex object', that:[ { will:'probably'}, { be:'good to read' } ], in:{ some:{ formatted:'way'} } }
=> {:this=>"is a complex object", :that=>[{:will=>"probably"}, {:be=>"good to read"}], :in=>{:some=>{:formatted=>"way"}}}
... ya, persis seperti yang Anda harapkan.
awesome_print mode:
irb(main):002:0> irb_mode(:ap)
irb#1(main):001:0> { this:'is a complex object', that:[ { will:'probably'}, { be:'good to read' } ], in:{ some:{ formatted:'way'} } }
=> {
:this => "is a complex object",
:that => [
[0] {
:will => "probably"
},
[1] {
:be => "good to read"
}
],
:in => {
:some => {
:formatted => "way"
}
}
}
... wow, sekarang semuanya dicetak dengan luar biasa! :)
Mode diam:
irb#1(main):002:0> irb_mode(:quiet)
irb#1(main):001:0> { this:'is a complex object', that:[ { will:'probably'}, { be:'good to read' } ], in:{ some:{ formatted:'way'} } }
irb#1(main):002:0>
... whoah, tidak ada keluaran sama sekali? Bagus.
Bagaimanapun, Anda dapat menambahkan mode apa pun yang Anda suka, dan ketika Anda selesai dengan mode itu, baru exitkeluar atau itu, dan Anda akan kembali ke mode sebelumnya.
Semoga bermanfaat! :)
users = User.all; 0