Skip to content

Instantly share code, notes, and snippets.

articles_table = Article.arel_table
# subquery
articles = articles_table
.project(Arel.star) # project = select
.where(articles_table[:type].eq('daily'))
.or(articles_table[:type].eq('weekly')) # or
.and(articles_table[:type].not_eq('monthly')) #!=
.and(articles_table[:active].eq(1)) # =
.and(articles_table[:created_at].gt(Date.today)) # >
def index(idx, arr)
# 終了条件
return p arr[0] if idx == 0
# 頭1つ落とす
index idx - 1, arr[1..-1]
end
index 3, [1, 2, 3, 4, 5]
module A
hoge = "hoge"
module B
fuga = "fuga"
def self.b
p hoge
end
def self.c
p fuga
@littlekbt
littlekbt / scope.rb
Last active December 18, 2015 05:27
def dubble(a)
if a.is_a?(Fixnum)
result = a * 2
end
p result
end
ary = [10, 5, 3, 4, 20, 50, 15]
#配列を受けて二つに割る。
def split_ary(ary, pivot_idx = 0)
return ary if ary.empty?
left = []
right = []
ary.each_with_index do |val, index|
next if index == pivot_idx
if val < ary[pivot_idx]
ary = [10, 5, 3, 4, 20, 50, 15]
#配列を受けて二つに割る。
def split_ary(ary, pivot_idx = 0)
left = []
right = []
pivot = 0
ary.each_with_index do |val, index|
next if index == pivot_idx
if val < ary[pivot_idx]
class Array
def insert_and_delete(idx, y)
move_elem = self[idx]
self.delete_at(idx)
self.insert(y, move_elem)
end
end
def insert_sort(arr)
num_of = arr.size - 1