Ruby – Scrap the content from HTML Source
November 3, 2009
CODE.rb
code=’<html>
<head>
<title>title</title>
<meta content=”title” name=”keywords”/>
<meta content=”text/html; charset=iso-8859-1″ http-equiv=”Content-
Type”/>’
puts content= code.scan(%r{content=”(.*?)”}im).flatten[0].to_s
puts name=code.scan(%r{name=”(.*?)”}im).flatten.to_s
Output:
title
keywords
Ruby – Get ASCII value
October 30, 2009
Example 1:
puts ?r #=> 114
puts ?a #=> 97
Example 2:
output=”raveendran”
puts output[0] #=>114 ‘value of r’
puts output[1] #=> 97 ‘value of a’
Example 3:
output=”raveendran”
output.each_byte do |a|
puts a
end
>ruby ascii_from_ruby.rb
114
97
118
101
101
110
100
114
97
110
>Exit code: 0
Watir – handling hidden process in windows
October 30, 2009
Code:
def running(a)
running=`tasklist`
if running.include?(a)
puts “#{a} is already running in windows machine”
else
puts “#{a} is not running”
end
end
runnning(‘java.exe’)
#=> java.exe is already running in windows machine
runnning(‘notepad.exe’)
#=> notepad.exe is not running
Where this code will useful:
We can able to chaeck the process is runnign or not — When Script need to close some opened Firefox windows or chrome windows
Ruby – Identifying opened applications in windows machine
October 28, 2009
Code
require ‘rubygems’
require ‘win32ole’
def window_name_opened(name)
w=WIN32OLE.new(‘WScript.Shell’)
output=w. AppActivate(name)
puts output
end
window_name_opened(‘notepad’)
#=> false
Step 2 –> Now open one note pad and run the ruby program again it should returns TRUE
Joker – Ruby gem
September 10, 2009
Joker – Ruby gem
Good Alternate for Regular expressions
require ‘rubygems’
require ‘joker’
wild = Wildcard['Fairy?ake*']
puts wild =~ ‘Fairycake’ #=> true
puts wild =~ ‘Fairyfakes’ #=> true
puts wild =~ ‘Fairylake is a cool place’ #=> true
puts wild =~ ‘Dairycake’ #=> false
puts wild =~ ‘Fairysteakes’ #=> false
puts wild =~ ‘fairycake’ #=> false
puts wildi = Wildcard['Fairy?ake*\?', true]
puts wildi =~ ‘FairyCake?’ #=> true
puts wildi =~ ‘fairyfakes?’ #=> true
puts wildi =~ ‘FairyLake IS A COOL Place?’ #=> true
Wildcard.quote(‘*?\\’) #=> ‘\\*\\?\\\\’
For more details about this gem –> http://bit.ly/L9kcg
Faker – Ruby gem
September 1, 2009
Installation
cmd>gem install faker
Basic use:
require ‘faker’
puts c=Faker::Name.name
output:
lemon arora
Fill your MYSQL DB with faker gem.
Code:
require ‘mysql’
require ‘faker’
db = Mysql::new(“localhost”, “root”, “”, “jazzezravi_development”)
i=1
while i < 1000 do
c=Faker::Name.name
c.gsub(“‘”,”")
sql = “INSERT INTO datas VALUES(#{i},’#{c}’,'0′,’0′);”
db.query(sql)
i+=1
end
For more details about faker gem –> http://faker.rubyforge.org
Ruby – Read/Write ProxyServer details in Windows registry
August 18, 2009
Ruby – Read/Write ProxyServer details in Windows registry
Code:
require ‘win32/registry’
def get_proxy
path = ‘Software\Microsoft\Windows\CurrentVersion\Internet Settings’
type, value = Win32::Registry::HKEY_CURRENT_USER.open(path).read (‘ProxyServer’)
puts value
end
def set_proxy
path = ‘Software\Microsoft\Windows\CurrentVersion\Internet Settings’
name=’ProxyServer’
data=’socks=34.12.78.92:54323′
type=1
Win32::Registry::HKEY_CURRENT_USER.open (path,Win32::Registry::KEY_WRITE).write(name,type,data)
end
get_proxy
set_proxy
get_proxy
Output:
socks=68.42.78.102:56773
socks=34.12.78.92:54323
Ruby +Mysql
August 12, 2009
Connecting, retriving data from MYSQL database:
Code
require ‘mysql’
db = Mysql::new(“localhost”, “root”, “”, “new_development”)
sql = “SELECT url FROM sites;”
x= db.query(sql)
x.each do |record|
puts record
end
Note:
root – SQL db user name
“” – Password for root access
new_development – DB Name
url - Field Name
sites – Table Name
Output:
From sites table –> url field –>
http://yahoomail.com
http://70.85.16.159/
google.com
yahoo.com
google.com
Ruby – Rename files in a single directory
August 1, 2009
Code:
def rando
letter=(“a”..”z”).to_a
return @name=
letter[rand(letter.length)]+”#{rand(9)}”+letter[rand(letter.length)]+
letter[rand(letter.length)]+letter[rand(letter.length)]+”#{rand(9)}”+letter[rand(letter.length)]+
letter[rand(letter.length)]+”#{rand(9)}”+letter[rand(letter.length)]+letter[rand(letter.length)]+
letter[rand(letter.length)]+letter[rand(letter.length)]+”#{rand(9)}”
end
dir = “../setup/videos”
$files = Dir.entries(dir)
puts “Before rename”
puts $files
$files.each do |f|
next if f == “.” or f == “..”
oldFile = dir + “\\” + f
newFile = dir + “\\#{rando;@name}” + “.”+f.split(“.”)[1]
File.rename(oldFile, newFile)
@file_path=File.expand_path(newFile)
@file_path=@file_path.gsub(“/”,”\\”)
end
puts “After Renamed”
$files = Dir.entries(dir)
puts $files
OUTPUT:
Before rename
.
..
four.flv
one.flv
three.flv
two.flv
After Renamed
.
..
f5vdc7zw3vplz8.flv
h5yyy5xj1jmbg8.flv
m6roa8sp5lqbq1.flv
q4rtq5ui0zdvx0.flv