Together: The Merb Story
4 years ago
A repository for thoughts about hacking, the software development process, and various development frameworks. Focused around Ruby, with forays into Rails, Shoes, Camping, Sinatra, and other development frameworks. May include occasional dalliances with other languages and subjects.
module Enumerable def any_true? inject(false) do |truth, item| truth || (yield item) end end endSo now I can write
arr.any_true? {|item| some_method(item)}Then I realized that a simpler way of expressing this is just
arr.map {|item| some_method(item)}.any?
def random_truth(likelihood) sleep 0.1 rand < likelihood endI can now run some tests that demonstrate the extreme difference in runtime:
>> Benchmark.realtime {10.times { (1..100).to_a.map {|i| random_truth(0.5)}.any? }} => 100.000391960144 >> Benchmark.realtime {10.times { (1..100).to_a.any_true? {random_truth(0.5)} }} => 2.00004100799561
function A(){};
A.prototype.method1 = function() {foo};
A.prototype.method2 = function() {bar};
A.prototype.method3 = function() {blah};
function B(){};
B.method1 = function() {foo};
B.method2 = function() {bar};
B.method3 = function() {blah};
A.prototype = B;
function A(){};
Shoes.app do
para "Hello World"
end
app = Shoes.app do
para "Hello World"
end
Shoes.debug "Parent is #{app.slot.parent.inspect}"
Shoes.debug "Children are #{app.slot.children.inspect}"
Shoes.app do
p = para "Hello World"
Shoes.debug "Para Parent is #{p.parent.inspect}"
Shoes.debug "Para Children are #{p.children.inspect}"
end
b = button "Click Me"
b.click { alert "You clicked the button" }
def add_handler(type, &block)
@_handlers ||= {}
@_handlers[type] ||= []
@_handlers[type].push block
# set up callbacks from old handlers invocation into
# new handler chain
self.send "#{type}_without_chains".to_sym do |*args|
self.call_handlers(type, *args)
end
end
def clear_handlers(type)
if @_handlers
if @_handlers.delete(type)
_handler_deleted(type)
end
end
end
def call_handlers(type, *args)
return unless @_handlers[type
@_handlers[type].each do |block|
block.call(*args)
end
end
HANDLERS.each do |handler|
eval %(
def #{handler}_with_chains(&block)
# need to clear handlers to maintain old semantics
clear_handlers(:#{handler})
add_handler(:#{handler}, &block)
end
)
end
def self.included(klass)
klass.instance_eval do
ShoeShine::HANDLERS.each do |handler|
if klass.instance_methods.include? handler.to_s
# Replace old handlers with new handlers
alias_method "#{handler}_without_chains".to_sym, handler
alias_method handler, "#{handler}_with_chains".to_sym
end
end
end
end
Shoes.constants.each do |c|
k = Shoes.const_get(c)
if k.is_a? Class
k.send :include, ShoeShine
end
end
def method_missing(method, *args, &block)
if args.empty? && !block_given?
# if you have no arguments and no block, think of it as a query
# and return the answer for the first element.
if first.respond_to? method
return first.send(method)
else
return elem.style(method)
end
elsif block_given?
each {|elem| elem.send(method, *args, &block)}
else
# Otherwise, think of it as a setter
setter = "#{method}=".to_sym
if first.respond_to? setter
each {|elem| elem.send(setter, *args, &block)}
else
each {|elem| elem.style(method => args[0], &block)}
end
end
self
end
# find all paragraphs, change their text to 'all ps', and color
# them red
shoe_query('para').text("all ps").stroke(red)
# replace the click handlers on all buttons
shoe_query('button').click do
para "All your button are belong to us now
end
load 'shoe_query.rb'
Shoes.app do
app = Shoes.app do
para "foo"
f = flow do
stack do
para "bar"
para 'blah'
end
end
b = button "click me to change all paragraphs"
b.click do
shoe_query('para').each do |p|
p.text = "all ps"
end
end
b2 = button "click me to just change the paragraphs inside the flow"
b2.click do
shoe_query('flow').find('para').each do |p|
p.text = "flow ps"
end
end
b3 = button "click me to change the button inside the stack"
b3.click do
shoe_query('stack para').each do |p|
p.text = "stack p"
end
end
end
def class_tree
tree = {}
Shoes.constants.each do |c|
k = Shoes.const_get(c)
next unless k.respond_to? :superclass
c = "Shoes::#{c}"
if k.superclass == Object
tree[c] ||= []
else
k.ancestors[1..-1].each do |sk|
break if [Object, Kernel].include? sk
(tree[sk.name] ||= []) << c
c = sk.name
end
end
end
tree
end
Shoes.debug Shoes::EditBox.ancestors.inspect
[Shoes::EditBox, Shoes::Basic, Shoes::Native, Object, FileUtils, FileUtils::StreamUtils_, Kernel]
cEditBox = rb_define_class_under(cShoes, "EditBox", cNative)
k.ancestors[1..-1].each do |sk|
break if [Object, Kernel].include? sk
+ next unless sk.is_a? Class #don't show mixins
(tree[sk.name] ||= []) << c
c = sk.name
end
class Window; end
class Mouse; end
class Canvas; end
class Shoes < Canvas
# Canvas-like things
class App < ::Shoes; end
class Flow < ::Shoes; end
class Stack < ::Shoes; end
class Mask < ::Shoes; end
class Widget < ::Shoes; end
# Standalones with no children
class Shape; end
class Effect; end
class Video; end
# Patterns
class Pattern; end
class Background < Pattern; end
class Border < Pattern; end
# TextBlocks
class TextBlock; end
class Para < TextBlock; end
class Banner < TextBlock; end
class Title < TextBlock; end
class Subtitle < TextBlock; end
class Tagline < TextBlock; end
class Caption < TextBlock; end
class Inscription< TextBlock; end
# Text
class Text; end
class Code < Text; end
class Del < Text; end
class Em < Text; end
class Ins < Text; end
class Span < Text; end
class Strong < Text; end
class Sup < Text; end
class Sub < Text; end
class Link < Text; end
class LinkHover < Text; end
# Natives
class Native; end
class Button < Native; end
class EditLine < Native; end
class EditBox < Native; end
class ListBox < Native; end
class Progress < Native; end
class Check < Native; end
class Radio < Native; end
# Timing related stuff
class TimerBase; end
class Animation < TimerBase; end
class Every < TimerBase; end
class Timer < TimerBase; end
class Color; end
class Download
class Response; end
end
# Errors
class InvalidModeError < StandardError; end
class NotImplementedError < StandardError; end
class VideoError < StandardError; end
end
button "My Button"
b = button "My Button"
b.click do
alert "You clicked my button!"
end
module Hello::Controllers
class Index < R '/', '/snips'
def get
@snippets = Snippet.find :all
render :index
end
end
end
module Hello::Controllers
class Index < R '/', '/snips'
def get
@snippets = Snippet.find :all
render :index
end
end
class New < R '/snips/new'
def get
@snippet = Snippet.new
render :new
end
def post
snip = Snippet.create(:title => input.title,
:body => input.body)
redirect Show, snip.id
end
end
class Show < R '/snips/(\d+)'
def get(id)
@snippet = Snippet.find(id)
render :show
end
end
class Edit < R '/snips/edit/(\d+)'
def get(id)
@snippet = Snippet.find(id)
render :edit
end
def post(id)
Snippet.find(id).update_attributes(:title => input.title,
:body => input.body)
redirect Show, id
end
end
end