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  
相关文档:
开发环境
Ruby:Ruby1.9.1
Rails:Rails2.3.5
Mysql:Mysql5.0.9
Driver:mysql-2.8.1-x86-mingw32.gem
IDE:Rubymine2.0.1
一、创建View/login
在View/login下创建login.html.erb、index.html.erb、loginFail.html.erb
login.html.erb代码如下:
<h1>Welcome to login!</h1>
<% form_tag do %>
& ......
http://www.robertsosinski.com/2008/12/21/understanding-ruby-blocks-procs-and-lambdas/
Understanding Ruby Blocks, Procs and Lambdas
Blocks, Procs and lambdas (referred to as closures
in Computer Science) are one of the most powerful aspects of Ruby, and
also one of the most misunderstood. This ......
下文转自:
http://www.cnblogs.com/watir/archive/2009/04/25/1443440.html
ruby文件从命令行中接收参数
在命令行方法执行ruby文件时,需要从命令行中传入参数,可以使用全局变量:ARGV
如有ruby 文件test.rb,内容如下:
1 def hello(a)
2 puts a
3 end
4
5 ......
1、安装ubuntu
一路next,记住安装英文版。待安装完毕后,首先选择“软件源”,系统--系统管理-软件源,国内一般选择的是:http://ubuntu.cn99.com/ubuntu;接着安装中文包,选择软件源就是为了下载软件的速度更快,安装完成后重启ubuntu,重启后会提示是否改变某些文件夹名称,选择“否”,防止系统对 ......
Ruport中pdf_writer对中文的支持并不好,输出的中文显示的是乱码。上网查了很多资料,也没有找到好的解决方案,无奈只好查看源代码,到底为什么Ruport自带的PDF工具不支持中文输出。
Ruport::Formatter::PDF::Writer中找到以下代码
metrics = load_font_metrics(font)
metrics = PDF::Writer: ......