Skip to content

Instantly share code, notes, and snippets.

@fobiasmog
Last active September 13, 2023 08:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fobiasmog/bc1418d815bec58f00f8fb0dca6fb920 to your computer and use it in GitHub Desktop.
Save fobiasmog/bc1418d815bec58f00f8fb0dca6fb920 to your computer and use it in GitHub Desktop.
Ruby & Rails hacks

Проверка, что все элементы в массиве идут по порядку от 1 до Х

arr.select.with_index { |e, i| e == i+1 }.size == arr.size

extend self

Полезен, когда нужно сделать метод модуля

module X
  extend self
  def x
    pp 'hello'
  end
end

[2] pry(main)> X.x
"hello"

module Y
  def y
    pp 'hi'
  end
end

[4] pry(main)> Y.y
NoMethodError: private method `y' called for Y:Module
from (pry):13:in `__pry__'

Рекурсивно превратить хэш в объект

require 'ostruct'
x = {email: {value: 'me@ya.ru'}}
JSON.parse x.to_json, object_class: OpenStruct

Не ставить локально при bundle install модули из продовской группы

bundle config set without 'production'

Найти расположение метода объекта

User.first.method(:myCustomMethod).source_location
=> ["/Users/mikeappell/Development/demo/app/models/concerns/demoable.rb", 53]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment