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
def list_squared(m, n) | |
divs_hash = {} | |
(m..n).reduce([]) do |result, i| | |
sum = 1 | |
divs = [1] | |
for k in (2..i/2) | |
if (i % k).zero? | |
current = (i/k) | |
if divs_hash.has_key?(current.to_s) | |
divs_hash[current.to_s].each do |c| |
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
"some string".gsub(pattern, replacement) # every occurrence of pattern is replaced by replacement | |
"other string".gsub(pattern) { |match| replacement } # every match is replaced by return value (replacement) of block | |
"hello".gsub(/./) { |c| "#{c.ord} " } # 104 101 108 108 111 |
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
(?=foo) # followed by foo | |
(?<=foo) # preceded by foo | |
(?!foo) # is not followed by foo | |
(?<!foo) # is not preceded by foo |
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
[1,2,3].zip([5,6,7,9], [7]) #[[1, 5, 7], [2, 6, nil], [3, 7, nil]] |
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
class Ln | |
include Comparable | |
attr_reader :value | |
def initialize(n) | |
@current = n | |
@value = Math.log2(n) | |
end | |
# Objects of the range have to comparable |
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
class Person | |
include Comparable | |
# Comparable adds methods, which allow to compare objects | |
attr_reader :age | |
def initialize(age) | |
@age = age | |
end | |
def <=>(another) # method is obligatory, as Comparable uses it to construct methods |
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
module SomeModule | |
# instance methods: | |
def some_instance_method | |
# do something here | |
end | |
# module, which contains class methods | |
module ClassMethods | |
# class methods: | |
def some_class_method |
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
select * from crosstab($$ | |
select row_name, some_category, some_value --only 3 columns! | |
from some_table | |
$$, $$values(category1), (category2), ..., (categoryn)$$ -- same can be achieved by selecting distinct values | |
) as g(RowName type, Category1 type, Category2 type, ..., CategoryN type); | |
-- type of CategoryN should be the same as type of value |
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
select count(*) over(order by id rows between unbounded preceding and unbounded following); -- all rows in partition | |
select count(*) over(order by id rows between 2 preceding and 1 following); -- 2 rows before, current and next row | |
select count(*) over(order by id rows unbounded preceding); -- all rows before current |
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
select * from t1, lateral (select * from t2 where t2.id = t1.id); |