Language: Ruby
Untitled Ruby (9-Dec @ 09:50)
Syntax Highlighted Code
- # traditional ways
- "a : bcd(1234: xyz)".gsub(/(\(\w*:)( )(\w*\))/, '\1ijk\3')
- [1 more lines...]
Plain Code
# traditional ways
"a : bcd(1234: xyz)".gsub(/(\(\w*:)( )(\w*\))/, '\1ijk\3')
# look-behind assertions
"a : bcd(1234: xyz)".gsub(/(?<=\w:) /, 'ijk')
Untitled Ruby (5-Oct @ 14:57)
Syntax Highlighted Code
- require 'net/http'
- require 'hpricot'
- require 'rss/maker'
- require 'uri'
- [61 more lines...]
Plain Code
require 'net/http'
require 'hpricot'
require 'rss/maker'
require 'uri'
class EMB
attr_writer :realm, :user, :password
def initialize ( realm = 'chs_programming', user='zichun', password='paksux' )
@realm, @user, @password = realm, user, password
@cookies = []
@cookie_string = ''
end
def get_cookie
http = Net::HTTP.new('smb.chs.edu.sg', 80)
resp,data = http.post('/cgi-bin/emb/login.pl',
"userid=#{@user}&password=#{@password}&login=+++Login+++",
{'referer'=>'http://smb.chs.edu.sg/emb/'+@realm+'/',
'user_agent'=>'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9) Gecko/2008051206 Firefox/3.0'
})
resp.get_fields('set-cookie').each { |x|
@cookies << x
@cookie_string += x.split("\; ")[0] + "\;"
}
@cookie_string
end
def get_main
http = Net::HTTP.new('smb.chs.edu.sg', 80)
http.get('/cgi-bin/emb/view.pl?date', {'Cookie'=>@cookie_string})
end
def get_msg (link)
url = URI.parse(link)
http = Net::HTTP.new('smb.chs.edu.sg',80)
http.get( url.path+'?'+url.query, {'Cookie'=>@cookie_string} )
end
end
emb_client = EMB.new
emb_client.get_cookie
res = emb_client.get_main
version = "2.0"
content = RSS::Maker.make(version) { |m|
m.channel.title = "EMB CHS_PROGRAMMING"
m.channel.link = "http://smb.chs.edu.sg/emb/chs_programming"
m.channel.description = "Extracted messages from chs_programming EMB"
m.items.do_sort = true
doc = Hpricot(res.body)
for i in 2...(doc/"form").length
item = m.items.new_item
x = (doc/"form")[i]
item.title = (x/"a").text
item.link = "http://smb.chs.edu.sg/emb/chs_programming/"
item.date = Time.parse(((x/"font")[0]).innerHTML.strip)
emb_body = Hpricot(emb_client.get_msg( x.at("a")['href'] ).body)
item.description = emb_body.at("pre")
item.author = (x/"td")[2].innerHTML.strip
end
}
$stdout.write(content)
Untitled Ruby (30-Sep @ 09:13)
Syntax Highlighted Code
- require 'rubygems'
- require 'sinatra'
- [34 more lines...]
Plain Code
require 'rubygems'
require 'sinatra'
get '/' do
haml <<END
!!!
%html
%head
%title Greetings
%body
%p Prepare to be greeted
%form{:action => 'hello', :method => 'post'}
%input{:type => 'text', :name => 'name'}
%input{:type => 'submit', :value => 'Greet'}
END
end
get '/hello/:name' do
greet(params[:name])
end
post '/hello' do
greet(params[:name])
end
private
def greet(name)
haml <<END
!!!
%html
%head
%title Greetz
%body
%h1 Heya!
%p= "Hello <em> #{name} </em>"
END
end
Untitled Ruby (21-Sep @ 22:20)
Syntax Highlighted Code
- puts "hello World"
Plain Code
puts "hello World"
Untitled Ruby (13-Sep @ 23:31)
Syntax Highlighted Code
- puts "hola que tal?"
Plain Code
puts "hola que tal?"
Untitled Ruby (27-Aug @ 10:05)
Syntax Highlighted Code
- p = ['a', 'b', 'c']
- p.each do |item|
- puts item
- end
Plain Code
p = ['a', 'b', 'c']
p.each do |item|
puts item
end
Untitled Ruby (28-Jul @ 10:22)
Syntax Highlighted Code
- before :deploy, "solr:stop"
- after :deploy, "solr:start"
Plain Code
before :deploy, "solr:stop"
after :deploy, "solr:start"
Untitled Ruby (4-Jul @ 09:50)
Syntax Highlighted Code
- a = String.new
- puts a.methods
Plain Code
a = String.new
puts a.methods
Untitled Ruby (18-Jun @ 21:58)
Syntax Highlighted Code
- class Carro
- def initialize
- [2 more lines...]
Plain Code
class Carro
def initialize
end
end
Untitled Ruby (28-May @ 10:32)
Syntax Highlighted Code
- class Example
- def foo
- put 'bar'
- end
- end
Plain Code
class Example
def foo
put 'bar'
end
end
Untitled Ruby (27-May @ 22:53)
Syntax Highlighted Code
- a = 123
- b = 123
- puts a + b
Plain Code
a = 123
b = 123
puts a + b
Untitled Ruby (12-May @ 08:05)
Syntax Highlighted Code
- include "hpricot"
Plain Code
include "hpricot"
Untitled Ruby (10-May @ 10:48)
Syntax Highlighted Code
- def test
- :foo => "bar"
- end
Plain Code
def test
:foo => "bar"
end