Ruby语言学习系列 基本的ruby语法
Ruby语言学习系列--基本的ruby语法
1. 基本的ruby语法
1.1 变量、常量和类型
1) 定义变量
变量类型
描述
示例
局部变量(或伪变量)
以小写字母或下划线卡头
var _var
全局变量
以$开头
$var
类变量
类中定义,以@@开头
@@var
实例变量
对象中定义,以@开头
@var
常量
以大写字母开头
Var
2) 变量内插
在双引号内使用“#{变量名}”内插变量
a = 2
b = 3
puts "#{a} + #{b} = #{a+b}" #输入结果为:2 + 3 = 5
1.2 注释
1)单行注释:以#开头,如: #注释内容
2)多行注释:在=begin 和 =end 之间定义,如:
=begin
注释内容
=end
1.3 循环和分支
1.3.1 条件语句
If 形式
unless 形式
a =1 if y==3
a=1 unless y!=3
x= if a > 0 then b else c end
x= unless a<=0 then a else b end
if x<5 then
a =1
else
a =2
end
unless x<5 then
a =2
else
a =1
end
1.3.2 循环结构
#while循环
i= 0
while i< list.size do
print “#list[i] ”
I += 1
end
#until循环
i= 0
until i == list.size do
print “#list[i]”
i += 1
end
#for循环
for x in lisy do
print “#{x}”
end
#each循环
list.each do |x|
print “#{x}”
end
#loop循环
i = 0
n = list.size-1
loop do
print “#{list[i]}”
i += 1
break id i > n
end
#times循环
n = list.size
n.times do |i|
print “#{list[i]}”
end
#upto循环
n =list.size–1
0.upto(n) do |i|
print “#{list[i]}”
end
#each_index循环
list.each_index do |x|
print “#{list[x]}”
end
1.3.3 异常
begin
x = Math.sqrt(y/z)
rescue ArgumentError  
相关文档:
Ruport官方网站:http://www.rubyreports.org/
本例中将介绍Ruby报表的简单开发
开发环境
OS:WindowsXP
Ruby:Ruby1.8.7
Rails:Rails2.3.5
Mysql:Mysql5.0.9
IDE:Rubymine2.0.1
一、安装gem
安装命令
gem install ruport
gem install ruport-util
gem install acts_as_reportable
二、创建数据库
datab ......
1、安装ubuntu
一路next,记住安装英文版。待安装完毕后,首先选择“软件源”,系统--系统管理-软件源,国内一般选择的是:http://ubuntu.cn99.com/ubuntu;接着安装中文包,选择软件源就是为了下载软件的速度更快,安装完成后重启ubuntu,重启后会提示是否改变某些文件夹名称,选择“否”,防止系统对 ......
ruby中单引号和双引号的意义有所不同,双引号包围的字符作变量替换,单引号包围的变量不做替换
也可以使用 %q 和 %Q 来生成字符串对象。%q 相当于单引号,%Q相当于双引号。
举例如下:
irb(main):010:0> "show trsult: #{1*3}"
=> "show trsult: 3"
irb(main):011:0> 'show trsult: #{1*3}'
=> "show trs ......
Installing Ruby from source is my preferred method, although in Ubuntu Feisty you can supposedly install it with apt-get install ruby
now. Here’s the essential packages needed to get a source build working right though and the process I just went through:
sudo apt-get install build-essentia ......
Update: This post is outdated. All in one installer for 1.9
is ready now, you should use it if you need 1.9 on windows. Get it here
http://rubyforge.org/frs/?group_id=167
Ruby has “all-in-one” installer for Windows, but it is outdated. As
of May 2009, Ruby 1.9.1 is released, ......