Skip to content

Instantly share code, notes, and snippets.

Byebug syntax highlighting. Works for v10.0.0 and higher.

Object that raises error on any method call

Useful for mocking in tests.

  • Transparent GitHub Avatar.
@marian13
marian13 / suppress_ruby_output.rb
Created December 8, 2021 00:00 — forked from moertel/suppress_ruby_output.rb
Temporarily suppress STDOUT and STDERR (ruby)
# Temporarily redirects STDOUT and STDERR to /dev/null
# but does print exceptions should there occur any.
# Call as:
# suppress_output { puts 'never printed' }
#
def suppress_output
original_stderr = $stderr.clone
original_stdout = $stdout.clone
$stderr.reopen(File.new('/dev/null', 'w'))
$stdout.reopen(File.new('/dev/null', 'w'))

Delete and recreate primary key

Delete a row with a primary key, recreate that row with the same primary key.

Preparation

mysql -u root
# Welcome to the MySQL monitor.  Commands end with ; or \g.
# Your MySQL connection id is 26

Deep Merge Concat

Can be useful for "inheritance" in YAML.

A proof that unless condition_1 && condition_2 is the same as if !condition_1 || !condition_2.

  • Create a file.

    touch test.rb
  • Paste the following content inside of it.

Do not accidentally create global methods in Ruby

Here is an example:

do_not_accidentally_create_global_methods_in_ruby_2_7

It is common to make such mistake in spec_helper, Rakefile, etc.

Module#include has incompatibilities between Ruby 2 and 3

Try to run the following example:

puts RUBY_VERSION

module IncludeModule
  def foo
 "foo from `IncludeModule`"

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