Ruby sample scripts - advanced (miscellaneous)

escape-elements.rb

require 'cgi'

str = "<html><h1>This is 'a' <i>test</i> string</h1></html>"
puts str
puts CGI.escapeElement(str, "h1", "html")
puts CGI.escapeHTML(str)

input-timeout.rb
require 'thread'

puts "Enter your name: "

catch (:timeout) do
   timeout_thread = Thread.new do
     5.times do
        sleep 1
        print "."
        STDOUT.flush
     end
     puts "-- TIMEOUT --"
     exit -1
     throw :timeout
   end
   name = gets
   puts "Hello " + name
end
 

marshal-retrieve.rb
require 'Person'

File.open("marshalled.bin") do |data|
    p = Marshal.load(data)
    puts p.name
end 


marshal-test.rb
require 'Person'

p = Person.new
p.name = "Smith"
p.age = 15
p.city = "Bengaluru"

p2 = Person.new
p2.name = "Sam"
p2.age = 10
p2.city = "Chennai"


file = File.new("marshalled.bin", "w")

data = Marshal.dump(p)
file.puts(data)

data = Marshal.dump(p2)
file.puts(data)

file.close


pstore-retrieve.rb

require 'Person'
require 'pstore'

store = PStore.new("object.bin")


store.transaction do
   p = store['1']
   p2 = store["aaa"]

   puts "Person 1: name -> #{p.name}, age -> #{p.age}"
   puts "Person 2: name -> #{p2.name}, age -> #{p2.age}"
end



pstore-test.rb
require 'Person'
require 'pstore'

p = Person.new
p.name = "Smith"
p.age = 15
p.city = "Bengaluru"

p2 = Person.new
p2.name = "Sam"
p2.age = 10
p2.city = "Chennai"


store = PStore.new("object.bin")

store.transaction do
   store['1'] = p
   store['aaa'] = p2
end



test-mymodule.rb
require 'MyModules/Test'

MyModules::Test.foo

url-escape.rb

require 'cgi'

url = "http://www.chandrashekar.info/q='   sfdgdsfg  &%# @' "

puts url
new_url = CGI.escape(url)
puts new_url



user-agent-test.rb
require 'net/http'

request = Net::HTTP.new("www.chandrashekar.info", 80)
response, data = request.get("/contact", nil)

#response.each {|line| puts line }
puts data



escape-elements.rb
require 'cgi'

str = "<html><h1>This is 'a' <i>test</i> string</h1></html>"
puts str
puts CGI.escapeElement(str, "h1", "html")
puts CGI.escapeHTML(str)

input-timeout.rb
require 'thread'

puts "Enter your name: "

catch (:timeout) do
   timeout_thread = Thread.new do
     5.times do
        sleep 1
        print "."
        STDOUT.flush
     end
     puts "-- TIMEOUT --"
     exit -1
     throw :timeout
   end
   name = gets
   puts "Hello " + name
end
 

marshal-retrieve.rb
require 'Person'

File.open("marshalled.bin") do |data|
    p = Marshal.load(data)
    puts p.name
end 


marshal-test.rb
require 'Person'

p = Person.new
p.name = "Smith"
p.age = 15
p.city = "Bengaluru"

p2 = Person.new
p2.name = "Sam"
p2.age = 10
p2.city = "Chennai"


file = File.new("marshalled.bin", "w")

data = Marshal.dump(p)
file.puts(data)

data = Marshal.dump(p2)
file.puts(data)

file.close


pstore-retrieve.rb

require 'Person'
require 'pstore'

store = PStore.new("object.bin")


store.transaction do
   p = store['1']
   p2 = store["aaa"]

   puts "Person 1: name -> #{p.name}, age -> #{p.age}"
   puts "Person 2: name -> #{p2.name}, age -> #{p2.age}"
end



pstore-test.rb
require 'Person'
require 'pstore'

p = Person.new
p.name = "Smith"
p.age = 15
p.city = "Bengaluru"

p2 = Person.new
p2.name = "Sam"
p2.age = 10
p2.city = "Chennai"


store = PStore.new("object.bin")

store.transaction do
   store['1'] = p
   store['aaa'] = p2
end



test-mymodule.rb
require 'MyModules/Test'

MyModules::Test.foo

url-escape.rb

require 'cgi'

url = "http://www.chandrashekar.info/q='   sfdgdsfg  &%# @' "

puts url
new_url = CGI.escape(url)
puts new_url



user-agent-test.rb
require 'net/http'

request = Net::HTTP.new("www.chandrashekar.info", 80)
response, data = request.get("/contact", nil)

#response.each {|line| puts line }
puts data