Ruby
总结
attr_writer :motherland 相当于 def motherland=(value) return @motherland =value end 之后用的时候: class Person def name=(name) @name=name end def gender=(gender) @gender=gender end def age=(age) @age=age end end people = Person.new('Tom', 'male', 15) people.name = "Henry" people.gender = "male" people.age = 25 attr_ reader :motherland 相当于 def motherland return @motherland end 用的时候:class Person def name @name end def gender @gender end def age @age end end people = Person.new('Tom', 'male', 15) puts people.name puts people.gender puts people.age 输出结果为:
Tom male 15 这就是我们熟悉的 getter
和 setter
方法的简写形式。你不熟悉也不重要。
用 Array new 方法创建数组 b = Array new b[0] = "aa" b[1] ="bb" Windows 下获取 ip 地址的 require 'socket' i = IPSocket.getaddress(Socket.gethostname) s = TCPSocket.gethostbyname(Socket.gethostname) pi ps Linux 下面运行的 ruby 匹配 ip 地址脚本 辛春 1 #!/usr/bin/ruby 2 3 #h = {"fir" => "aa", "sec" => "mm", "third" => "ldlld"} 4 #h.length 5 #h["fir"] or h["sec"] 6 #h[100] = "henry" 7 8 9 #ip = `ifconfig`; 10 file = File.open("ip.txt", "r"); 11 ip = file.read; 12 puts ip; 13 ipre = /:(2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)\s/; 14 while ip =~ ipre; 15 puts $&;; 16 ip = $' 17 end 2[0-4]\d 匹配200-249 25[0-5]匹配250-255
[01]?\d\d?匹配0-199的 -----------------------------------------------------------------------------------------------------------文森 require 'win32ole' require 'open-uri' echocmd = "IP: " ip = ( open("myip.dk") {|f| /([0-9]{1,3}\.){3}[0-9] {1,3}/.match(f.read)[0].to_a[0]}) puts echocmd + ip
----------------------------------------------------------------------------------------------------------------李杰 print "input:" STDOUT.flush str = gets if not str[/[a-z,A-Z]/] and str[/^[1-9,(]|^\([1-9]/] and str[/[\+\-\*\/]/]then puts "result:"+"#{eval str}" else puts "input error" end 计算器的小代码,正侧表达式的匹配还是有点问题,大家也可以试试其他方法 这里有个
问题:双引号,跟单引号,单引号是原样输出,双引号里加#{}可替换, 如,“#{20*30}”它输出的结果就会是 60,不是 20*30 ----------------------------------------------------------------------------------计算器 何国兴 p "Please enter the computational expressions" getep=gets if getep=~/\D/ then begin p "hegx:#{eval getep}" rescue =>ex p ex.to_s ensure p "Calculation end" end End -------------------------------------------------------------------------------------我--封装了加减乘除四个方法,正则表达式匹配符号进行调用 class Jisuan
def add print "first" geta = gets getaa = geta.to_f print "second" getb = gets getbb = getb.to_f sum = getaa + getbb print"#{sum}" end def subtraction print "first" geta = gets getaa = geta.to_f print "second" getb = gets getbb = getb.to_f sum = getaa -getbb print"#{sum}" end de
f multiply print "first" geta = gets getaa = geta.to_f print "second" getb = gets getbb = getb.to_f sum = getaa * getbb print"#{sum}" end def divide print "first" geta = gets getaa = geta.to_f print "second" getb = gets getbb = getb.to_f sum = getaa / getbb print"#{sum}" end end print "please input symbol:" str = gets #提示输入符号 #得到这个字符串
if str !~/[a-zA-Z]/ and str !~/\d/ #进行过滤,不匹配字母跟数字,否则错误 if str =~/\+/ then #匹配+号调用+的方法 p1 = Jisuan.new p1.add elsif st