This file contains hidden or 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 MySMTP | |
HOST = 'mail.x.com' | |
PORT = 25 | |
HELO = 'x.com' | |
def self.compose_mail(recipients, from, subject, body) | |
recipients = [recipients] if !recipients.kind_of?(Array) | |
["From: <#{from}>", | |
"To: " + recipients.map { |r| "<#{r}>" }.join(', '), | |
"Subject: #{subject}", |
This file contains hidden or 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
# per http://stackoverflow.com/questions/3684484/peak-detection-in-a-2d-array | |
require 'pp' | |
NUM_PEAKS = 5 | |
NEIGHBOR_DISTANCE = 1 | |
data = [[1,2,3,4,5], | |
[2,6,4,4,6], | |
[3,6,7,4,3], |
This file contains hidden or 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
# from http://jsteinhardt.wordpress.com/2010/09/13/nobody-understands-probability/ | |
<quote> | |
Let’s consider an example. Suppose that a man comes up to you and says | |
“I have two children. At least one of them is a boy.” | |
What is the probability that they are both boys? | |
</quote> | |
Note: we are assuming the man is telling the truth. | |
Justification: Any probabilistic analysis presumes that given facts are true. |
This file contains hidden or 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
<?xml version="1.0"?> | |
<test>"'&<></test> | |
Now monkey patching | |
<?xml version="1.0"?> | |
<test>"'&<></test> | |
<!-- Notice there is no XML entity substitution for the single- and double-quotes --> |
This file contains hidden or 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 'rubygems' | |
require 'nokogiri' | |
include Nokogiri::XML | |
b = Builder.new { | |
fields { | |
field '1' | |
field '2' | |
} |
This file contains hidden or 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 'socket' | |
require 'rubygems' | |
require 'nokogiri' | |
PORT = rand(65535 - 1024) + 1024 | |
sthread = Thread.new { | |
s = TCPServer.new(PORT).accept | |
s.write('<test>') |
This file contains hidden or 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 ResponseSAX < Nokogiri::XML::SAX::Document | |
attr_reader :fields | |
def start_document | |
puts "Starting the parse" | |
@fields = {} | |
@char_buf = '' | |
@start_fields = false | |
end |
This file contains hidden or 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 'rubygems' | |
require 'nokogiri' | |
class PushDocument < Nokogiri::XML::SAX::Document | |
attr_reader :document, :current, :parents, :errors, :warnings | |
def cdata_block(string) | |
current << Nokogiri::XML::CDATA.new(document, string) | |
end |
This file contains hidden or 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 'socket' | |
require 'rubygems' | |
require 'nokogiri' | |
PORT = rand(65535 - 1024) + 1024 | |
sthread = Thread.new { | |
s = TCPServer.new(PORT).accept | |
puts "[SERVER] Client connected." |
This file contains hidden or 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 NewsItem < ActiveRecord::Base | |
validates_presence_of(:date, :headline, :body) | |
def date | |
@date.strftime("%Y-%m-%d") if @date | |
end | |
def date=(input) | |
case input | |
when String |