Skip to content

Instantly share code, notes, and snippets.

@simplay
simplay / own_dsl.rb
Created May 21, 2013 18:30
event monitoring system - example for scope, blocks, procs, lambdas in ruby source: metaprogramming-ruby
lambda {
setups = []
events = {}
Kernel.send :define_method, :event do |name, &block|
events[name] = block
end
Kernel.send :define_method, :setup do |&block|
setups << block
end
@simplay
simplay / refactoring_using_metaprogrammin.rb
Created May 20, 2013 14:57
Refactoring in Ruby using dynamic methods and dynamic dispatch (1) using dynamic proxy that is also a blank slate (2) source: book - metaprogramming-ruby
# given class Computer: refactor me
class Computer
def initialize(computer_id, data_source)
@id = computer_id
@data_source = data_source
end
def mouse
info = @data_source.get_mouse_info(@id)
price = @data_source.get_mouse_price(@id)
@simplay
simplay / Ruby_Lambda_Fun_Numerical _Integration_Trapezoid.rb
Last active April 22, 2022 15:23
# Numerical Integraion over real domain of real 1d valued functions using Newton-Cotes approximation. # NB1: Error of approximation in O(f'' * (b-a)^3 ) # NB2: In practice quite usable for many common functions, like affine, trigonomatric function
# D subset |R Domain over which we integrate - sample D = [1,10]
D = (1..10).to_a
# f:|R->|R function - sample x |-> f(x) := x^2
f = lambda {|x| x*x}
# summation of function values over our domain
summator = lambda {|function, domain| domain.inject(0){|x,y| x += function.call(y)} }
# Integrations Driver (for real valued 1d functions only) - assumption: integration steps equidistant