Skip to content

Instantly share code, notes, and snippets.

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

SengMing Tan tansengming

🏠
Working from home
View GitHub Profile
rd, wr = IO.pipe
if fork
wr.close
puts "Parent got: <#{rd.read}>"
rd.close
Process.wait
else
rd.close
puts "Sending message to parent"
# How to build dynamic instance methods for classes.
# First define a method creator (i.e. attribute_accessor below) that
# loops through your params to create new methods.
# Via: http://codeshooter.wordpress.com/2009/06/04/understanding-class_eval-module_eval-and-instance_eval/
Object.class_eval do
class < < self
def attribute_accessor( *attribute_names )
attribute_names.each do |attribute_name|
class_eval %Q?
ffmpeg -i in.dts -ac 6 -ab 640000 -ar 48000 out.ac3
@tansengming
tansengming / forking_do_it.rb
Created October 20, 2009 02:33
How not to use fork
# Doing this will likely kill your machine
very_large_array.each do |array_item|
fork do
cpu_intensive_task(array_item)
end
end
# This however will lower the changes of that happening.
CPU_CORE_COUNT = 4
very_large_array.each_slice(CPU_CORE_COUNT).each do |smaller_array|
@tansengming
tansengming / meta.rb
Created October 28, 2009 06:17
Where I show an iterative method of building an attr_reader type metaprogramming model
# Initial Rev
# machine.rb
class Machine
def initialize(name)
@name = name
end
def device
Lot.find_by_tester(@name).device
end
@tansengming
tansengming / crawl.rake
Created October 29, 2009 07:57
Rake task to crawl you rails app for broken links
# Use this to look for broken links in your app.
# crawls the development server http://localhost:3000
# Suggestion: Also check to make sure that intentional 404s
# are handled gracefully by app.
task :crawl => :environment do
require 'anemone'
root = 'http://localhost:3000'
options = {:discard_page_bodies => true, :verbose => true}
@tansengming
tansengming / define_method.rb
Created November 3, 2009 02:55
some notes to help me with define_method
# here lies some notes to help me with define_method
class MetaTest
define_method(:meta_1) do
puts 'm.meta_1'
end
def initialize
@a_var = 'iable'
end
@tansengming
tansengming / owl_by_example.xml
Created February 19, 2010 13:20
Owl By Example
<!-- Class / Concept -->
<owl:Class rdf:ID="a_subclass">
<rdfs:label rdf:datatype="http://www.w3.org/2001/XMLSchema#string">Subsclass of a class</rdfs:label>
<rdfs:subClassOf>
<owl:Class rdf:ID="a_class"/>
</rdfs:subClassOf>
</owl:Class>
<!-- Simple Instance -->
<Route rdf:ID="Route_1">
@tansengming
tansengming / ar.rb
Last active July 24, 2018 17:24
Programming Notes
# joins with time range
time_range = (Time.now.midnight - 1.day)..Time.now.midnight
Client.joins(:orders).where(orders: { created_at: time_range })
@tansengming
tansengming / .ackrc
Created December 15, 2011 02:45
getting ack to work with coffee haml and sass
--type-add
html=.haml
--type-add
css=.sass,.scss,.less
--type-add
js=.coffee