摘要:require'socket'includeSocket::Constants defscan_port if$*[0]==nilor$*[1]==nilor$*[2]==nilabort"用法示例:ruby#$0ip地址开始端口 结束端口EX:如ruby#$0localhost11024"endtime=Time.now scan$*[0],$*[1],$*[2] puts"\n共耗时:#{Time.now-time}秒"endprivate defscan(address,start_port,end_port) threads=[]
阅读全文
随笔分类 - Ruby
摘要:irb(main):005:0> [1,1,3,5] & [1,2,3]=> [1, 3]irb(main):006:0> [1,2,3] * 3=> [1, 2, 3, 1, 2, 3, 1, 2, 3]irb(main):007:0> [1,2,3 ] * "--"=> "1--2--3"irb(main):008:0> [1,1,2,2,3,3,4,5]-[1,2,4]=> [3, 3, 5]irb(main):009:0> [1,2] <<'c'<&l
阅读全文
摘要:可以用{}书写一个block,如 {puts "hello"}也可以不用{},直接写为puts "hello"或用begin puts "hello"end在每一行可以加;puts "hello";puts "hey";或不加;puts "hello"puts "hey"或在do..end中定义do club.enroll(person) person.socializeend另外if radiation>3000 puts "Danger
阅读全文
摘要:ruby中有类似vb的模块module如:module Summable def sum inject {|v,n| v+n} endendclass Array include Summableendclass Range include Summableendclass VowelFinder include Summableend[1,2,3,4,5].sum 得到 15('a'..'m') 得到 "abcdefghijklm"vf=VowelFinder.new("the quick brown fox jumped&quo
阅读全文
摘要:class File def File.Open(*args) result=f=File.new(*args) if block_given? begin result=yield f ensure f.close end return result endend注: ensure用于必定要执行的语句
阅读全文
摘要:ruby名字用来引用常量,变量,方法,类和模块局部变量 由小写字母后跟命名用字表示,如 fred _x three_two实例变量名 以"at"符(@)开始,后跟一个名字,如 @name @_ @size类变量名 以两个"at"符(@)开始,后跟一个名字,如@@name,@@_ @@Size常量名 以一个大写字母开始,后跟多个命名用字符。习惯上,常量引用为全部大写字母和下划线组成,如 NAME MAT_INT全局变量 由"$"后跟命名用字符组成。,如$$ $! $_ $-a $-K
阅读全文
摘要:%q中定义的字符串,不加分号 如'hello'与%q(hello) 表示同一字符串%w表示数组,不对变量进行替换;%W表示数组,对变量进行替换散列表用=>序列进行分隔irb(main):001:0> %w(fred wilma barney betty great\ gazoo)=> ["fred", "wilma", "barney", "betty", "great gazoo"]irb(main):002:0> arr=%w(fred wilma b
阅读全文
摘要:类似于C,一般用'\'作为转义字符如'escape using "\\"' 将显示 escape using "\" 'That\'s right' 显示 That's right#{expr}序列把任何Ruby代码的值放入字符串中"Seconds/day: #{24*60*60}" 得到 Seconds/day: 86400"#{'Ho! '*3'}Merry Christmas!" 得到 "Ho! Ho! Ho!
阅读全文
摘要:irb(main):020:0> f=File.open("/home/dongjichao/c/3_1.c")=> #<File:/home/dongjichao/c/3_1.c>irb(main):021:0> f.each do |line|irb(main):022:1* puts lineirb(main):023:1> end#include <stdlib.h>int main(void){ printf("ddddd\n"); return EXIT_SUCCESS;}=> #<
阅读全文
摘要:irb(main):012:0> def n_times(thing)irb(main):013:1> return lambda {|n| thing * n}irb(main):014:1> end=> nilirb(main):015:0> p1=n_times(23)=> #<Proc:0x
阅读全文
摘要:ruby中整数支持times,upto,downto,step等几种迭代irb(main):010:0> 3.times {print "X " }X X X => 3irb(main):011:0> 1.upto(5) {|i| print i," "}1 2 3 4 5 => 1irb(main):012:0> 99.downto(95) {|i| print i," "}99 98 97 96 95 => 99irb(main):013:0> 50.step(80,5) {|i| pri
阅读全文
摘要:在ruby中方法与block的合用:~/ruby$ irbirb(main):001:0> def take_block(p1)irb(main):002:1> if block_given?irb(main):003:2> yield(p1)irb(main):004:2> elseirb(main):005:2* p1irb(main):006:2> endirb(main):007:1> end=> nilirb(main):008:0> take_block("no block")=> "no bloc
阅读全文
摘要:正则表达式的分组在模式内部,\1序列指的是第一个组的匹配,\2序列指的是第二个组的匹配,如irb(main):008:0> "12:50am"=~/(\d\d):(\d\d)(..)/=> 0irb(main):009:0> "Hour is #$1,minute is #$2"=> "Hour is 12,minute is 50"irb(main):010:0> "12:50am"=~/((\d\d):(\d\d))(..)/=> 0irb(main):011:0> &
阅读全文
摘要:ruby的正则表达式中的字符类缩写字符 是 含义\d [0-9] 数字字符\D [^0-9] 除数字之外的任何字符\s [ \t\r\n\f] 空格字符\S [^ \t\r\n\f] 除空格之外的任何字符\w [A-Za-z0-9] 组词字符\W [^A-Za-z0-9] 除组词字符之外的任何字符测试如下irb(main):001:0> a="the quick brown fox"=> "the quick brown fox"irb(main):002:0> a.sub(/[aeiou]/,'*')=> &qu
阅读全文
摘要:ruby语言中用~/字符/来匹配表达式,$`得到匹配之前的那部分字符串,$'得到匹配之后的字符串,$&得到匹配到的字符串,如下所示def show_regexp(a,re) if a=~re puts "#{$`}<<#{$&}>>#{$'}" else puts "no match" endendshow_regexp('very interesting',/t/)show_regexp('Fats Waller',/a/)show_regexp('Fats
阅读全文
摘要:cat FormatSql.rb
while line=gets
line1=line.gsub(/(select|from|where|sum|having|group|end|case|by)/) {|match| match.upcase}
puts line1
end
阅读全文
摘要:1.首先要获得汉字的支持,必须在文件开头加入以下两句
require "jcode"
$KCODE="utf8"
阅读全文
摘要:ruby apache cgi 配置 1,按说明安装ruby的apache mod2,配置文件如下:Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> 1 ######################################################### 2 # ruby cgi 配置 3 ######################################################### 4 LoadModule
阅读全文
摘要:# Sample code from Programing Ruby, page 239require 'soap/wsdlDriver'以下通过对Google的服务请求,使用Google的一个服务,并使用了WSDL(Web Services Description Language)来完成.(前提是你拥有一个Google Web API的使用账号,并保存在主目录的.goolge_key文件中)require 'cgi'WSDL_URL = "http://api.google.com/GoogleSearch.wsdl"soap = SOA
阅读全文
摘要:我们可以通过SOAP服务器来访问预先定义好的对象,通过soap/rpc/driver就可做到,这也可以看作是同其他语言交互的一种很好的方式,服务器端存为server.rbrequire 'soap/rpc/standaloneServer'class InterestCalculator attr_reader :call_count def initialize @call_count=0 end def compound(printcipal,rate,freq,years) @call_count+=1 printcipal*(1.0+rate/freq)**(freq*
阅读全文