Skip to content

Instantly share code, notes, and snippets.

@ievgrafov
ievgrafov / main.rb
Created April 1, 2016 12:34
example_for_calling_exact_method_for_class_with_several_mixins
module A
def a
puts 'A'
end
end
module B
def a
puts 'B'
end
@ievgrafov
ievgrafov / gist:42a0ec18565c38458603
Created March 14, 2016 09:53
formula building example
class Formula
attr_accessor :value
def initialize(v)
@value = v
end
def to_s
value
end
@ievgrafov
ievgrafov / gist:30fef46eae8bcec9fd27
Last active June 28, 2017 11:36
rails 4.2 comma as decimal sparator
# A monkey-patch that allows you to use comma as decimal separator in ActiveRecord
# Notice for rails 5.0: in Rails 5.0 module Type with all nested classes is moved into ActiveModel,
# so don't forget to substitute 'module ActiveRecord' with 'module ActiveModel' after update
module ActiveRecord
module Type
class Decimal
private
alias_method :cast_value_without_comma_separator, :cast_value
@ievgrafov
ievgrafov / gist:e4750f67d90d465848b1
Last active February 16, 2016 10:47
Difference between IN and ANY + array in Postgres (this difference shows itself only with several IN, one IN works just as ANY + array)
Запрос через IN:
SELECT "documents".* FROM "documents"
WHERE "documents"."archived_at" IS NULL
AND (
contractor_id IN (SELECT id FROM companies WHERE lower(COALESCE(companies.title, '')) ~* 'основ')
OR stock_to_id IN (SELECT id FROM stocks WHERE lower(COALESCE(stocks.title, '')) ~* 'основ')
OR stock_from_id IN (SELECT id FROM stocks WHERE lower(COALESCE(stocks.title, '')) ~* 'основ')
);
EXPLAIN ANALYZE:
>> Append (cost=135.96..2696.11 rows=3680 width=291) (actual time=4.539..52.993 rows=1940 loops=1)
def generic_restriction(method_name, attributes)
[:define_method, :define_singleton_method].each do |definition_method|
send definition_method, method_name do
attributes
end
end
end
@ievgrafov
ievgrafov / new_stateful
Last active August 29, 2015 14:21
stateful example
x0 = (0..500).map{ |xx| xx/1000.0 }
y0 = x0.map{ |xx| Math.exp(-xx) }
x1 = (500..1000).map{ |xx| xx/1000.0 }
y1 = x1.map{ |xx| Math.exp(-xx) }
# create new datablock
datablock = Datablock.new([x0, y0])
# now data will be sent to gnuplot and new datablock in terminal created
# everything is good, we still may have stateless objects