This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
# @example provides Redis distributed lock api | |
# | |
# $ TemporaryLock.create!(:example, 'c22a0420-51c8-4e47-843c-92a37e8d2eba') | |
# => #<TemporaryLock:0x000055b43e328410 @key="temporary_lock:example:c22a0420-51c8-4e47-843c-92a37e8d2eba"> | |
# | |
# @example raises `TemporaryLock::StaleObjectError` when `id` already registered | |
# | |
# $ TemporaryLock.create!(:example, 'c22a0420-51c8-4e47-843c-92a37e8d2eba') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sequence1 = %w[d c b] | |
sequence2 = %w[e a f] | |
base = %w[z a c d f y b] | |
def sort_by_sequence(sequence:) | |
array = yield | |
array.select { |el| sequence.include?(el) }.sort do |left, right| | |
sequence.index(left) <=> sequence.index(right) | |
end | sort_by_alfabet { array } | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
WITH messages_ranks AS ( | |
SELECT *, | |
rank() OVER ( PARTITION BY channel_id ORDER BY created_at DESC) AS rank | |
FROM messages | |
) | |
SELECT row_to_json(t) | |
FROM ( | |
SELECT channels.id, | |
channels.name, | |
channels.owner_id, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
User.joins('LEFT JOIN "invoices" ON "invoices"."merchant_id" = "users"."id"').group("users.id").having("count(*) <= ?", 5) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pub fn nth(n: u32) -> Option<u32> { | |
pub fn is_prime(n: u32) -> bool { | |
if n == 1 {return false}; | |
if n == 2 {return true}; | |
let sqrt_n = ((n as f32).sqrt() as u32) + 1; | |
for i in 2..sqrt_n { | |
if n % i == 0 {return false}; | |
} | |
true |