??
?????? ~_~今天刚刚接触Ruby这门语言。Ruby一种为简单快捷的面向对象编程(面向对象程序设计)而创的脚本语言,在20世纪90年代由日本人松本行弘 开发,遵守GPL协议和Ruby License。它的灵感与特性来自于 Perl、Smalltalk、Eiffel、Ada 以及 Lisp 语言。由 Ruby 语言本身还发展出了JRuby(Java 平台)、IronRuby(.NET 平台)等其他平台的 Ruby 语言替代品。Ruby的作者于1993年2月24日开始编写Ruby,直至1995年12月才正式公开发布于fj(新闻组)。因为Perl发音与6月诞生石pearl(珍珠)相同,因此Ruby以7月诞生石ruby(红宝石)命名。
?
????? (1)安装Ruby
打开 http://www.ruby-lang.org/zh_cn/downloads/
?
从上图所示的地方下载Ruby,然后安装
?
????? (2)测试是否安装成功
cmd-->ruby -v
显示如下
证明安装成功
?
????? (3)运行第一个Ruby程序
编写Ruby可以使用记事本、EditPlus、Eclipse等工具,我选择了Editplus,因为比较轻便,进行一些配置可以高亮ruby关键字。
配置Editplus http://butian.org/knowledge/develop/web/1409.html
上述文章作了详细的说明
建立一个 helloruby.rb 文件
print("Hello,Ruby.\n")
?运行 ruby helloruby.rb 输出 Hello,Ruby.
?
???? (4)Ruby的一些基础的学习
print("------------------print()方法------------\n") print("Hello,Ruby.\n") print("Hello,\"Ruby\".\n") print('Hello,\"Ruby\".\n') print("\n") print('Hello,\'Ruby\'.\\') print("\n") print("Hello,","Ruby","\n") print("-----------------puts()方法-------------\n") puts('Hello,Ruby.') puts("Hello,","Ruby") print("-----------------p()方法-------------\n") puts("Hello,\n\tRuby") p("Hello,\n\tRuby") print("-----------------显示中文-------------\n") puts("一二三四五六七","七六五四三二一") p("一二三四五六七","七六五四三二一") print("-----------------四则运算-------------\n") print("2+1=",2+1,"\n") print("2-3=",2-3,"\n") print("3*4=",3*4,"\n") print("100/5=",100/5,"\n") print("-----------------include Math-------------\n") print("sin(100)=",Math.sin(100),"\n") print("sqrt(100)=",Math.sqrt(100),"\n") include Math print("sin(100)=",sin(100),"\n") print("sqrt(100)=",sqrt(100),"\n") print("-----------------变量-------------\n") =begin 这是注释 =end x = 10 #长度 y = 20 #宽度 z = 30 #高度 area = (x * y + x * z + y * z) * 2 volume = x * y * z print("表面积为:",area,"\n") print("体积为:",volume,"\n") print("-----------------条件判断-------------\n") p(1 == 1) if("Ruby" == "Ruby") then print("Yes\n") else print("No\n") end print("-----------------循环-------------\n") i = 1 while i < 10 print(i,"\n") i = i + 1 end 10.times{ print(":)\n") } print("-----------------定义方法-------------\n") def hello print("Hello,Ruby.\n") end hello()
?
print("------------------数组---------------------------\n") name = ["小林","林","高野","森岗"] print("第一个元素为",name[0],"\n") print("依次输出name中元素\n") name.each{|n| print(n,"\n") p(n) } p name num = [1,2,3,4,5] num.each{|n| print(n,"\n") p(n) } p num mix = [1,"jim",2,"ton"] mix.each{|n| print(n,"\n") p(n) } p mix p mix.size print("----------------hash---------------------------------\n") front_table = {"nomal" => "+0","small" => "-1","big" => "+1"} print("<html><title>front size list</title>\n") print("<body>\n<p>\n") front_table.each{|key,value| print('<front size=',value,'>',key,'</front><br>',"\n") } print("</p>\n</body>\n</html>\n") v = {"key00" => "快乐学习ruby","key01" => "Ruby宝石", "key03" => "Ruby红宝石" } p v require "pp" pp v print("----------------正则表达式-------------------------\n") p /Ruby/ =~ "ruby" p /Ruby/i =~ "ruby" p /Ruby/ =~ "hhuhdadaRuby" print("----------------综合-------------------------------\n") name.each{|n| if( /林/ =~ n) puts(n) p n end }
?
puts("----------------从命令行读取数据-----------") print("第一个参数",ARGV[0],"\n") print("第二个参数",ARGV[1],"\n") print("第三个参数",ARGV[2],"\n") name = ARGV[0] puts name num0 = ARGV[0].to_i num1 = ARGV[1].to_i print(num0,"+",num1,"=",num0 + num1,"\n") puts("----------------从文件读取数据-------------") filename = ARGV[3] file = open(filename) puts("read开始") text = file.read print text puts("read结束") file.close file1 = open(filename) puts("gets开始") while text = file1.gets do print text end puts("gets结束") file1.close pattern = Regexp.new(ARGV[4]) filename = ARGV[3] file = open(filename) while text = file.gets do if pattern =~ text puts text end end
?
?ok,上面这些为学习Ruby第一天的全部内容。:)
?