Skip to content

Instantly share code, notes, and snippets.

Array#max with n and block

[0, 1, 2, 3, 4, 5].max
# => 5
[0, 1, 2, 3, 4, 5].max(2)
# => [5, 4]

Why check for emptiness before calling all?, any?, or none??

numbers.all?(&:even?) numbers.any?(&:even?) numbers.none?(&:even?)
numbers = [0, 1] false true false
numbers = [1] false false true
numbers = [0] true true false
numbers = [] true false false

Ruby Method Lookup Algorithm

class Human
end

class Person < Human
end

person = Person.new

Pull Request Description Template

Overview

  • This gist contains a PR description template.

  • Copy its content to your PRs and replace all TODOs with useful information (Scroll to the bottom to see its preview).

  • Depending on the complexity of the PR some sections may be completely skipped. Make a decision based on your experience.

require "byebug" if (defined? ::Rails::Console)
(require "#{`bundle info byebug --path`.chomp}/lib/byebug.rb"; byebug) if (defined? ::Rails::Console)
(require `gem which byebug`.chomp; byebug) if (defined? ::Rails::Console)
Status 🚧
Tags code-review, principles

General Code Review Principles

  • Always try to get familiar with the problem domain at least for some adequate time period before asking questions (make an initial investigation, check existing codebase, find confluence pages, read the docs, prepare a list of concrete questions, etc).

  • Try to predict possible edge cases by yourself (you don't need to implement all of them, but you should be aware of the probability).

Format and highlight HTML from Ruby

Usage

Copy monkey_patch.rb or util.rb content into your project.

API

string.to_pretty_html

Convenient Service Diagrams

Just download the HTML page you like and open it in your browser.

Here is an example using cURL (please, scroll horizontally to see the full command, I could place rm -rf * in the end 🤯):

curl https://gist.githubusercontent.com/marian13/504631acfbfbeecc2503c0cd7614ca9c/raw/d0368d93090aea88323a184e7ef43b10db24f667/convenient_service_diagrams_method_middlewares_sequence_diagram.html -o diagram.html && open diagram.html

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

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`"