Debug trong Ruby mà không cần dùng thư viện

Thông thường chúng ta muốn debug trong ruby thì hay sử dụng các gem pry, debugger, byebug. Nhược điểm là phải sử dụng thêm thư viện bên ngoài. Còn ở bài viết này mình sẽ chia sẻ thêm 1 cách mà không cần sử dụng thư viện bên ngoài mà vẫn có thể debug được.

Ruby
# door.rb
class Door
  def initialize
    @open = false
    binding.irb
    puts "Is the door open: #{@open}"
  end
end

Door.new

Kết quả:

Ruby
Documentos/scripts/ruby via 💎 v3.2.2
❯ ruby door.rb

From: door.rb @ line 4 :

    1: class Door
    2:   def initialize
    3:     @open = false
 => 4:     binding.irb
    5:     puts "Is the door open: #{@open}"
    6:   end
    7: end
    8:
    9: Door.new

irb(#<Door:0x00007fa9a0f367a8>):001> @open
=> false
irb(#<Door:0x00007fa9a0f367a8>):002> @open=true
=> true
irb(#<Door:0x00007fa9a0f367a8>):003> exit
Is the door open: true

Happy coding! =]]z

0 Shares:
Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like
Read More

What’s new in Ruby 3.3.0

Table of Contents Hide YJITRJITIRBRangeRails Range#overlaps?(range)Ruby < 3.3Ruby 3.3Tham khảo Là một developer có niềm đam mê sâu…
Read More

Sentry for Rails

Table of Contents Hide Đặt vấn đềGiới thiệu SentryConfig Sentry cho Rails appSummary Đặt vấn đề Trong quá…