- 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
This file contains 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 Gladiator | |
attr_accessor :name, :rank, :height, :weight | |
def initialize | |
yield(self) | |
end | |
end | |
spartak = Gladiator.new do |his| | |
his.name = "spartak" |
This file contains 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
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", |
This file contains 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 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 |
This file contains 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
source 'https://rubygems.org' | |
gem 'rake' | |
gem 'lotus-router' | |
gem 'lotus-controller' | |
gem 'lotus-view' | |
group :test do | |
gem 'rspec' | |
gem 'capybara' |
This file contains 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
my_hash.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo} |
This file contains 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
# 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 |
This file contains 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
require 'faraday' | |
require 'forwardable' | |
module Soup | |
class Agent | |
extend Forwardable | |
def initialize(domain = 'https://www.soup.io/') | |
@agent ||= faraday(domain) | |
end |
This file contains 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 fib(n) | |
n < 2 ? n : fib(n-1) + fib(n-2) | |
end |
This file contains 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 User | |
attr_accessor :name, :pet_name | |
end | |
class Factory < BasicObject | |
def initialize | |
@attributes = {} | |
end | |
attr_reader :attributes |
OlderNewer