Posted by root
Wed, 30 Jul 2008 13:59:00 GMT
require 'benchmark'
n = 1_000_000
s = " : : : "
Benchmark.bm do |x|
x.report("tr:") { n.times{ s.tr(":", ";") } }
x.report("tr_s:") { n.times{ s.tr_s(":", ";") } }
x.report("gsub:") { n.times{ s.gsub(":", ";") } }
end
Right,
tr beats the other methods:
user system total real
tr: 2.760000 0.120000 2.880000 ( 2.897022)
tr_s: 5.310000 0.240000 5.550000 ( 5.822026)
gsub: 8.850000 0.200000 9.050000 ( 9.311452)
Posted in Ruby | Tags performance | no comments
Posted by root
Sun, 27 Jul 2008 11:47:00 GMT
In essence (from bulgarian to japanese):
agent = WWW::Mechanize.new
agent.post("http://translate.google.com/translate_t",
"sl" => "bg",
"tl" => "ja",
"text" => "Здравей, свят!").search("#result_box").inner_html
results in こんにちは、世界!
Posted in Ruby | Tags api | no comments
Posted by root
Sun, 27 Jul 2008 07:55:00 GMT
String methods:
"a".upto("z"){ |i| puts i }
a = String.new "a"
0.upto(0){ p a }
1.upto(25){ p a.next! }
Range method:
("a".."z").each{ |e| puts e }
Posted in Ruby | Tags alphabet | no comments
Posted by root
Sat, 26 Jul 2008 21:46:00 GMT
Posted in scripts | Tags vampire | no comments
Posted by root
Sat, 26 Jul 2008 04:50:00 GMT
def my_method; end
undef my_method
The core manual
Posted in Ruby | Tags OOP | no comments
Posted by root
Sat, 26 Jul 2008 04:11:00 GMT
arr = (1..10).to_a
str = arr * ', '
str = arr.join(', ')
Am I going to use the perlish coding style? Not really.
Posted in Ruby | Tags observation | no comments
Posted by root
Mon, 21 Jul 2008 22:15:00 GMT
Ruby file manipulation is as easy as:
MyFile.create "test.txt"
MyFile.add "test.txt", "some content"
MyFile.remove "test.txt"
For example create a file caled fileeasy.rb and add the following module to it:
module FileEasy
def self.included(subject)
subject.extend(ClassMethods)
end
module ClassMethods
alias_method :delete, :remove
alias_method :unlink, :remove
def create(filename)
File.open(filename, "w")
end
def add(filename, text)
File.open(filename, "a"){ |f| f << text }
end
def remove(filename)
File.delete(filename)
end
end
end
Then test it with irb:
require 'fileeasy'
class MyClass
include FileEasy
end
MyFile.create "text.txt"
MyFile.add "text.txt", "test content"
MyFile.remove "text.txt"
Of course this post is just a simple tutorial and the standard library is the best choice – FileUtils.
Posted in Ruby | Tags file | no comments
Posted by root
Sun, 20 Jul 2008 04:13:00 GMT
class Class
def attr(arg)
arg = arg.to_s
if self.class_variables.include?(arg)
class_eval %(def self.#{arg.sub(/^\@\@/, '')}; #{arg}; end)
else
raise ArgumentError, "No such class attribute", caller
end
end
end
class Test
@@class_var = "class variable value"
attr :@@class_var
end
puts Test.class_var => "class variable value"
Posted in Ruby | Tags OOP | no comments
Posted by root
Sun, 20 Jul 2008 03:46:00 GMT
AoH = [{"z"=>26}, {"g"=>7}, {"r"=>18}, {"a"=>1}, {"v"=>22}]
AoH.map{|e| e.to_a.flatten}.sort{|x,y| x[0] <=> y[0]}
AoH.map{|e| e.to_a.flatten}.sort{|x,y| x[1] <=> y[1]}
Posted in Ruby | Tags sort | no comments
Posted by root
Sat, 19 Jul 2008 21:59:00 GMT
require 'tempfile'
require 'net/http'
arg = ARGV[0].chomp
url = 'sa.dir.bg'
h = Net::HTTP.new(url, 80)
resp,data = h.get("/cgi/sabig.cgi?word=#{arg}", nil )
outtmp = Tempfile.new("translator")
outtmp << data.to_s
outtmp.rewind
puts %x[html2text "#{outtmp.path}"]
Posted in scripts | Tags tools | no comments