Skip to content

Instantly share code, notes, and snippets.

@NILID
NILID / get_empty_ranges.rb
Created March 4, 2021 12:26
Displaying empty range indices based on filled range indices
arr_size = 100
arr = [[0,5], [20, 30], [50, 70]]
def get_ranges(arr_size, arr)
# calculating adjacent indices
# flatten multiple arrays in one
arr.map!{|k, v| [get_min(k), get_max(v, arr_size)]}.flatten!
# checking the occurrence of range boundaries
sum = arr + [0, arr_size]
@NILID
NILID / user.rb
Last active September 27, 2019 18:48
User model with association by :parent_username
class User < ApplicationRecord
belongs_to :parent, primary_key: :username,
foreign_key: :parent_username,
class_name: 'User',
optional: true
has_many :children, primary_key: :username,
foreign_key: :parent_username,
class_name: 'User'
end
@NILID
NILID / random_color.rb
Created September 27, 2019 11:43
Generate a random hex color, or with fixing anyone color
class RandomColor
def self.get_random
rand(255)
end
def self.color_hex(options = {})
default = { red: get_random, green: get_random, blue: get_random }
options = default.merge(options)
'#%X%X%X' % options.values
end
// Simple style for the content of parent & child
p{
margin: 0;
background-color: #fff;
color: #00B9AE;
padding: 20px;
border-radius: 10px;
box-shadow: 0 3px 6px rgba(#CC8367, 0.22);
}
@NILID
NILID / devise_invitable.ru.yml
Created November 2, 2012 13:32
Russian locale file
ru:
devise:
invitations:
send_instructions: 'Приглашение было отправлено %{email}.'
invitation_token_invalid: 'Некорретный ключ приглашения!'
updated: 'Ваш пароль изменён. Теперь вы вошли в систему.'
no_invitations_remaining: "Больше нет приглашений"
new:
header: "Отправка приглашения"
submit_button: "Отправить приглашение"
@NILID
NILID / fibonacci.rb
Created September 5, 2012 14:34
fibonacci
def fib(n)a,b,s=0,1,[];n.times{s<<a;a,b=b,a+b};s end
p fib(33)
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, ...]