Skip to content

Instantly share code, notes, and snippets.

View benjamintanweihao's full-sized avatar

Benjamin Tan Wei Hao benjamintanweihao

View GitHub Profile
# encoding: utf-8
require 'cgi'
require 'mechanize'
require 'open-uri'
require 'pathname'
require 'uri'
class Scraper
def initialize
# encoding: utf-8
require 'parallel'
require 'cgi'
require 'mechanize'
require 'open-uri'
require 'pathname'
require 'uri'
def product_name(asin)
url = "http://www.amazon.com/dp/#{asin}"
defmodule SecureRandom do
@moduledoc """
Ruby-like SecureRandom module.
## Examples
iex> SecureRandom.base64
"xhTcitKZI8YiLGzUNLD+HQ=="
iex> SecureRandom.urlsafe_base64(4)
@benjamintanweihao
benjamintanweihao / cheng_yu_scraper.rb
Created February 17, 2014 04:25
Chengyu Scraper
# encoding: UTF-8
require 'nokogiri'
require 'open-uri'
require 'json'
Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8
class ChengyuScraper
def initialize
@benjamintanweihao
benjamintanweihao / distributing_work.ex
Last active August 29, 2015 13:56
Distributing Work example in Elixir.
defmodule DistributingWork do
# NOTE: When no more work, and no more active workers, return state (our answer)
def process_work([], [], _, state), do: IO.puts inspect(state, char_lists: :as_lists)
# NOTE: When no more work, and all workers are active, wait for workers to return
def process_work(work, active, passive, state) when work == [] or passive == [] do
receive do
{ worker, result } ->
process_work(work, List.delete(active, worker), [worker | passive], [result | state])
end
defmodule Acky do
defmodule Client do
def start do
spawn(__MODULE__, :loop, [])
end
def loop do
receive do
{from, {:ok, result}} ->
class Blah
def add(x, y)
x + y
end
end
# => nil
class Symbol
def call(*args, &block)
->(o) { o.send(self, *args, &block) }
end
@benjamintanweihao
benjamintanweihao / copy_as_rtf.rb
Created March 19, 2014 13:45
Textmate Copy as RTF
#!/usr/bin/env ruby -Ku
require "#{ENV['TM_BUNDLE_SUPPORT']}/lib/copy_as_rtf.rb"
require "#{ENV['TM_SUPPORT_PATH']}/lib/progress.rb"
doc = RtfExporter.new.generate_rtf( STDIN.read )
`echo "hi" | pbcopy`
Kernel.open('|pbcopy','w') do |f|
f.write(doc)
end
print doc

Erlang Tracing: more than you wanted to know

Rough Outline

  • What can be traced?
  • How can trace events be specified?
  • "match specifications": twisty passages, all alike
  • WTF, can I just use DTrace and drink my coffee/beer/whisky in peace?
  • Trace delivery mechanisms: pick one of two

I want to write plugins for Atom's editor in Ruby. Opal makes this possible. Atom is one of several projects in recent times to combine Chromium with Node.js for a desktop app. While it utilizes chromium for it's gui, and boasts "[e]very Atom window is essentially a locally-rendered web page", writing Atom plugins is more like writing a server-side node.js app than a typical single-page client-side app (albeit with really awesome integration with Chrome Devtools). Opal development, on the other hand, has to-date been focused primarily on the browser use-case.

Because of this, I had to make a choice between using the opal-node package from npm, using Opal via Ruby w/ a compile step, or packaging up opal-parser.js, including it with the app, and writing in compilation on the fly. Each choice came with compromises. Using opal-node would have been easiest, just create a top level index.coffee that required opal-node, and then require in your ruby