Skip to content

Instantly share code, notes, and snippets.

View rickhull's full-sized avatar

Rick Hull rickhull

View GitHub Profile
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}",
# 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],
# 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.
<?xml version="1.0"?>
<test>"'&amp;&lt;&gt;</test>
Now monkey patching
<?xml version="1.0"?>
<test>"'&amp;&lt;&gt;</test>
<!-- Notice there is no XML entity substitution for the single- and double-quotes -->
require 'rubygems'
require 'nokogiri'
include Nokogiri::XML
b = Builder.new {
fields {
field '1'
field '2'
}
require 'socket'
require 'rubygems'
require 'nokogiri'
PORT = rand(65535 - 1024) + 1024
sthread = Thread.new {
s = TCPServer.new(PORT).accept
s.write('<test>')
class ResponseSAX < Nokogiri::XML::SAX::Document
attr_reader :fields
def start_document
puts "Starting the parse"
@fields = {}
@char_buf = ''
@start_fields = false
end
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
require 'socket'
require 'rubygems'
require 'nokogiri'
PORT = rand(65535 - 1024) + 1024
sthread = Thread.new {
s = TCPServer.new(PORT).accept
puts "[SERVER] Client connected."
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