Language: Ruby

Untitled Ruby (9-Dec @ 09:50)

Syntax Highlighted Code

  1. # traditional ways
  2. "a : bcd(1234: xyz)".gsub(/(\(\w*:)( )(\w*\))/, '\1ijk\3')
  3.  
  4.  
  5. [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

  1. require 'net/http'
  2. require 'hpricot'
  3. require 'rss/maker'
  4. require 'uri'
  5. [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)

rrees.wordpress.com

Syntax Highlighted Code

  1.  
  2. require 'rubygems'
  3. require 'sinatra'
  4.  
  5. [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

  1. puts "hello World"

Plain Code

puts "hello World"

Untitled Ruby (13-Sep @ 23:31)

Syntax Highlighted Code

  1. puts "hola que tal?"

Plain Code

puts "hola que tal?"

Untitled Ruby (5-Sep @ 06:42)

Syntax Highlighted Code

  1. date();

Plain Code

date();

Untitled Ruby (27-Aug @ 10:05)

Syntax Highlighted Code

  1. p = ['a', 'b', 'c']
  2. p.each do |item|
  3.   puts item
  4. end

Plain Code

p = ['a', 'b', 'c']
p.each do |item|
  puts item
end

Untitled Ruby (28-Jul @ 10:22)

Syntax Highlighted Code

  1. before :deploy, "solr:stop"
  2. after  :deploy, "solr:start"

Plain Code

before :deploy, "solr:stop"
after  :deploy, "solr:start"

Untitled Ruby (21-Jul @ 13:05)

Syntax Highlighted Code

  1. foo = String.new
  2.  
  3.  
  4.  

Plain Code

foo = String.new


Untitled Ruby (14-Jul @ 13:55)

Syntax Highlighted Code

  1. def foo
  2. end

Plain Code

def foo
end

Untitled Ruby (14-Jul @ 06:19)

Syntax Highlighted Code

  1. dsaf

Plain Code

dsaf

Untitled Ruby (4-Jul @ 09:50)

Syntax Highlighted Code

  1. a = String.new
  2. puts a.methods

Plain Code

a = String.new
puts a.methods

Untitled Ruby (18-Jun @ 21:58)

Syntax Highlighted Code

  1. class Carro
  2.  
  3.     def initialize
  4.        
  5. [2 more lines...]

Plain Code

class Carro

    def initialize
        
    end

end

hghgfhgfh (11-Jun @ 23:53)

Syntax Highlighted Code

  1. ghhffghgfhgfhgfh

Plain Code

ghhffghgfhgfhgfh

Untitled Ruby (28-May @ 10:32)

Syntax Highlighted Code

  1. class Example
  2.   def foo
  3.     put 'bar'
  4.   end
  5. end

Plain Code

class Example
  def foo
    put 'bar'
  end
end

Untitled Ruby (27-May @ 22:53)

Syntax Highlighted Code

  1. a = 123
  2. b = 123
  3. puts a + b

Plain Code

a = 123
b = 123
puts a + b

Untitled Ruby (12-May @ 08:05)

Syntax Highlighted Code

  1. include "hpricot"

Plain Code

include "hpricot"

Untitled Ruby (10-May @ 10:48)

Syntax Highlighted Code

  1. def test
  2.   :foo => "bar"
  3. end

Plain Code

def test
  :foo => "bar"
end