Skip to content

Instantly share code, notes, and snippets.

@tario
tario / packetexample.rb
Created June 6, 2011 01:17
packetexample.rb
require "evalhook"
class MyHandler < EvalHook::HookHandler
...
end
handler = MyHandler.new
packet = handler.pack(code)
@tario
tario / gist:1011399
Created June 7, 2011 00:02
minispike: embed live ruby code including classes inside methods
class Package
def run
class << Object
class ::X
def foo
print "hello world\n"
end
end
end
end
@tario
tario / chat.rb
Created December 14, 2011 13:18 — forked from rkh/chat.rb
Simple Chat Application using the Sinatra Streaming API
# coding: utf-8
require 'sinatra'
set server: 'thin', connections: []
get '/' do
halt erb(:login) unless params[:user]
erb :chat, locals: { user: params[:user].gsub(/\W/, '') }
end
get '/stream', provides: 'text/event-stream' do
@tario
tario / picomock.rb
Created December 28, 2011 16:55
picomock
class TestClass
def foo(x)
@instance_var + $global.to_i
print self.method_name, "\n"
x**2
end
end
suite( 1 => 1, 2 => 4, 3 => 9, 4 => 16 ).test(
@tario
tario / gist:1541370
Created December 30, 2011 20:33 — forked from mislav/gist:17371
haml2erb
class ErbEngine < Haml::Engine
def push_script(text, preserve_script, in_tag = false, preserve_tag = false,
escape_html = false, nuke_inner_whitespace = false)
push_text "<%= #{text.strip} %>"
end
def push_silent(text, can_suppress = false)
push_text "<% #{text.strip} %>"
end
end
@tario
tario / test.rb
Created January 4, 2012 16:56
method_missing!
class X
attr_accessor :a, :b
def initialize
@a = 0
@b = b
end
def foo!(a)
@a = a
@tario
tario / chains.rb
Created January 7, 2012 22:19
chains
require "set"
require "sexp"
class Chain
def initialize
@groups = Hash.new
@enabled_groups = Set.new
enable :default
end
@tario
tario / method_container.rb
Created March 8, 2012 02:32
method_container
require "method_source"
def method_container_for(target, &blk)
# create adhoc class under target
method_container = Class.new
def method_container.method_added(mname)
# do whatever you want with methods (e.g. "use the source" )
p instance_method(mname).source
end
#Newbie programmer
def factorial(x):
if x == 0:
return 1
else:
return x * factorial(x - 1)
print factorial(6)
#First year programmer, studied Pascal
@tario
tario / javascriptmode.rb
Created March 19, 2012 18:10
Programming ruby like javascript
obj = lambda{ |binding_|
Object.new.tap { |retobj|
retobj.instance_variable_set(:@binding_,binding_)
def retobj.method_missing(m,arg=nil)
if m =~ /(.*)=$/
$tmp = arg
@binding_.eval("#{$&} = $tmp")
else
@binding_.eval(m.to_s)