Skip to content

Instantly share code, notes, and snippets.

@marian13
Last active May 16, 2024 12:19
Show Gist options
  • Save marian13/c81d8a32d4264e419ba09f08bedfc650 to your computer and use it in GitHub Desktop.
Save marian13/c81d8a32d4264e419ba09f08bedfc650 to your computer and use it in GitHub Desktop.

Not predictable equal? behaviour

  • Try to guess what is the return value of question.equal?("hurting yourself") in test_1.rb.

  • true? Are you sure?

  • Run test_1.rb

ruby test_1.rb
  • Remove # frozen_string_literal: true from test_2.rb

  • Rerun test_1.rb

ruby test_1.rb
  • Are you surprised?

  • Now imagine this code somewhere in production?

  • Still not hurting?

  • What if test_1.rb and test_2.rb has 1000+ of lines with huge amount of useless indirections?

  • OK, have a nice day debugging... 👍

# frozen_string_literal: true
require_relative "test_2"
require "byebug"
question = QUESTION
p question.equal?("hurting yourself")
p question.object_id
p "hurting yourself".object_id
byebug
byebug
# frozen_string_literal: true
QUESTION = "hurting yourself"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment