ruby初学者文档.docx

上传人:b****7 文档编号:9221954 上传时间:2023-02-03 格式:DOCX 页数:15 大小:19.08KB
下载 相关 举报
ruby初学者文档.docx_第1页
第1页 / 共15页
ruby初学者文档.docx_第2页
第2页 / 共15页
ruby初学者文档.docx_第3页
第3页 / 共15页
ruby初学者文档.docx_第4页
第4页 / 共15页
ruby初学者文档.docx_第5页
第5页 / 共15页
点击查看更多>>
下载资源
资源描述

ruby初学者文档.docx

《ruby初学者文档.docx》由会员分享,可在线阅读,更多相关《ruby初学者文档.docx(15页珍藏版)》请在冰豆网上搜索。

ruby初学者文档.docx

ruby初学者文档

目录

Ruby基础语法2

打印语句2

显示中文2

注释3

控制语句3

if3

unless5

while5

case6

文件包含7

文件读取8

变量8

函数10

类12

类的三种定义方法12

类方法引用13

常数14

类变量14

字符串类15

‘’与””的区别15

rails15

Ruby基础语法

打印语句

print("hello,ruby")

print('hello,\nruby\n')

print('hello,\'ruby\'')

print('hello\\ruby')

puts("hello","ruby")

puts("hello\nruby")

puts("100")

puts(100)

p100

p"100"

p("hello\nruby")

字符串操作

字符串长度:

p“hello”.length

P‘hello’.size

字符串是否为空:

p“”.empty?

P‘oo’.empty?

字符串删除:

s.delete(str)

s.delete!

(str)

显示中文

ruby-Kshello.txt

print(1+2)

print(1/2)

print(1*2)

print(1-2)

 

includeMath

print(sin

(1))

注释

print("no")

#print("ok")

=begin

print("begin")

print("end")

=end

控制语句

判断

if

a=20

ifa>20then

print"bigger"

elsifa==20then

print"equal"

else

print"smaller"

end

 

a=20

ifa>20

p"bigger"

elseifa==20

p"equal"

else

p"smaller"

end

end

a=10

ifa>10

print"bigger"

elsifa==10

print"equal"

else

print"smaller"

end

 

unless

a=10

b=20

unlessa

print"a>=b"

else

print"a

end

case

tag=["a","b","c"]

tag.each{|item|

caseitem

when"a"

print"a"

when"b"

print"b"

else

print"c"

end

}

 

array=["aa",1,nil]

item=array[1]

caseitem

whenString

puts"string"

whenNumeric

puts"numeric"

else

puts"else"

end

循环

i=1

whilei<=10

printi,"\n"

i+=1;

end

10.times{

print"hello\n"

}

defhello

print"helloruby"

end

hello()

文件包含

require"hello"

hello()

传参数

print"第一个实参",ARGV[0]

num0=ARGV[0].to_i

num1=ARGV[1].to_i

print"num0-num1=",num0-num1

文件读取

filename="C:

\\Users\\Administrator\\Desktop\\ruby\\one.txt"

file=open(filename)

text=file.read

printtext

file.close

filename=ARGV[0]

file=open(filename)

whiletext=file.gets

printtext

end

file.close

 

变量

局部变量:

以小写字母或“_”开头的变量

全局变量:

以$开始的变量

实例变量:

以@开始的变量

类变量:

以@@开始的变量

虚拟变量:

”true”“false”“self”等特定名称的变量

$x=0

x=0

require"one"

p$x

px

str1="foo"

str2="f"+"o"+"o"

p"str1.equal?

(str2)",str1.equal?

(str2)

pstr1.eql?

(str2)效果类似

p"str1==str2",str1==str2但是数值类比较重定义了该方法

 

p1.0==1

p1.0.eql?

1

 

str1="foo"

str2="f"+"o"+"o"

pstr1.object_id

pstr2.object_id

p"".empty?

p"a".empty?

()

p/ruby/=~"hello,ruby"

p/ruby/=~"hello"

函数

defhello(name="jia")

print"welcome:

"+name+"\n"

end

hello()

hello("one")

defvolume(x,y,z)

returnx*y*z

end

pvolume(1,2,3)

省略return返回最后一行

defarea(x,y,z)

xy=x*y

xz=x*z

yz=y*z

(xy+xz+yz)*2

end

parea(1,2,3)

 

defmax(a,b)

ifa>b

returna

else

returnb

end

end

pmax(3,1)

ary=Array.new

str="helloworld"

pary.class

pstr.class

ary=[]

str="helloworld"

pary.instance_of?

(Array)

pstr.instance_of?

(String)

str="helloworld"

pstr.is_a?

(String)

pstr.is_a?

(Object)

类的三种定义方法

(1)

classHelloWorld

defHelloWorld.hello(name)

printname,"saidhello"

end

end

HelloWorld.hello("ruby")

(2)

classHelloWorld

end

class<

defhello(name)

printname+":

saidhello"

end

end

HelloWorld.hello("ruby")

(3)

classHelloWorld

defself.hello(name)

printname+":

saidhello"

end

end

HelloWorld.hello("ruby")

类方法引用

类.方法

类:

方法

常数

classHello

Version="1.0"

end

pHello:

:

Version

类变量

classHello

@@count=0

defHello.count

@@count

end

definitialize(name="ruby")

@name=name

end

defhello

@@count+=1;

print@name+":

hello\n"

end

end

tom=Hello.new("tom")

jim=Hello.new("jim")

ruby=Hello.new

pHello.count

tom.hello

jim.hello

ruby.hello

pHello.count

字符串类

‘’与””的区别

moji="字符串"

str="这也是#{moji}"

pstr

str1='这也是#{moji}'

pstr1

rails

设置字符编码

before_filter:

set_charset

defset_charset

@headers["Content-Type"]="text/html;charset=utf-8"

end

表单

或本地同目录下文件

脚手架

RailsprojectName–dmysql

邮件发送程序

require'rubygems'

require'action_mailer'

ActionMailer:

:

Base.delivery_method=:

smtp

ActionMailer:

:

Base.smtp_settings={

:

address=>'',

:

port=>25,

:

domain=>'',

:

user_name=>'wo_dwl@',

:

password=>'459614088',

:

authentication=>:

login}

ActionMailer:

:

Base.default_charset='GBK'

classSimpleMailer

:

Base

defsimple_message()

from'wo_dwl@'

recipients'wo_dwl@'

subject'ok'

body'simple'

end

end

SimpleMailer.deliver_simple_message()

p'ok'

邮件激活注册案例

注册邮箱:

zhixintaike@

密码:

zhixintaike123

(1)创建项目

Railsmail–dmysql

(2)必要的时候使用sql语句创建数据库

(3)修改database.yml文件的用户名和密码

(4)生成注册页面

Rubyscript/generatescaffoldusername:

stringpass:

stringemail:

stringactive_code:

stringis_activated:

boolean

(5)删除目录db\migrate下相应迁移文件中没用的属性(如:

t.timestamps)

(6)迁移文件

Rakedb:

migrate

(7)创建注册控制器

Rubyscript/generatecontrollerregister

myshop_2集成邮箱激活系统

邮箱配置:

config\environment.rbmodel\active_mail.rb

(1)增加字段

t.string:

active_code

t.boolean:

is_activated

sql语句:

Altertableusersaddcolumnactive_codevarchar(30);

Altertableusersaddcolumnis_activatedBoolean;

(2)配置邮件环境变量\config\environment.rb下添加如下代码:

require'rubygems'

require'action_mailer'

 

ActionMailer:

:

Base.delivery_method=:

smtp

ActionMailer:

:

Base.smtp_settings={

:

address=>'',

:

port=>25,

:

domain=>'',

:

user_name=>'zhixintaike@',

:

password=>'zhixintaike123',

:

authentication=>:

login}

ActionMailer:

:

Base.default_charset='utf-8'

注:

配置完后需重启服务

(3)生成mail模型

Rubyscript/generatemailActivateMailsent

(4)生成控制器register

Rubyscript/generatecontrollerregister

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 小学教育 > 数学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1