Skip to content

Instantly share code, notes, and snippets.

View sesharim's full-sized avatar
🏠
Working from home

Lukin Max sesharim

🏠
Working from home
View GitHub Profile
@sesharim
sesharim / gist:0ccf1d59dcfd61c6e64b
Created August 25, 2014 14:27
block on initialize
class Gladiator
attr_accessor :name, :rank, :height, :weight
def initialize
yield(self)
end
end
spartak = Gladiator.new do |his|
his.name = "spartak"
recursive_array = lambda do |array, result|
array.each do |item|
result << item[:name] if item[:name]
recursive_array.call(item[:children], result) if item[:children]
end
result
end
a = [
{name: "Folder 1",
module MyModule
def self.included(base)
puts base
base.instance_variable_set :@my_instance_variable, {}
base.extend ClassMethods
end
module ClassMethods
def my_instance_variable
# self is ClassA here, so we need to call superclass
@sesharim
sesharim / Gemfile
Last active August 29, 2015 14:10 — forked from jodosha/Gemfile
source 'https://rubygems.org'
gem 'rake'
gem 'lotus-router'
gem 'lotus-controller'
gem 'lotus-view'
group :test do
gem 'rspec'
gem 'capybara'
my_hash.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
# I was considering how I might want to further implement my gem and incorporate configuration settings for it. I wanted only one instance of my configuration and it needed to be part of my module. So I’ve come up with a solution I like that allows me to do this and even have a true “sense” of untouchable constants.
# I’ve found OpenStruct to be for my liking in this situation as it allows for any configuration (variable) to be set on it. I’ve also found I can set “unalterable” variables… at least through standard assignment options.
# The reason I created the class MyProject::MyConfig just to inherit OpenStruct is that I want my configuration to be identified by a class name that indicates it is a config object that belongs to my project. In module MyProject I use attr_reader for config because we are getting the OpenStruct instance handed back to us and we can still invoke method/value assignment on it. I use the single class variable @config for the internal singleton value because it’s not going a
@sesharim
sesharim / agent.rb
Last active August 29, 2015 14:17 — forked from regedarek/agent.rb
require 'faraday'
require 'forwardable'
module Soup
class Agent
extend Forwardable
def initialize(domain = 'https://www.soup.io/')
@agent ||= faraday(domain)
end
@sesharim
sesharim / fib
Created March 30, 2015 14:40
fib
def fib(n)
n < 2 ? n : fib(n-1) + fib(n-2)
end
@sesharim
sesharim / dsl
Created April 26, 2015 22:08
dsl
class User
attr_accessor :name, :pet_name
end
class Factory < BasicObject
def initialize
@attributes = {}
end
attr_reader :attributes
  • Dynamic Dispatch
  • Dynamic Method
  • Ghost Methods
  • Dynamic Proxies
  • Blank Slate
  • Kernel Method
  • Flattening the Scope (aka Nested Lexical Scopes)
  • Context Probe
  • Class Eval (not really a 'spell' more just a demonstration of its usage)
  • Class Macros