码迷,mamicode.com
首页 > 其他好文 > 详细

[ruby on rails] 深入(2) ruby基本语法

时间:2014-12-05 14:01:27      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   sp   on   文件   div   问题   log   

1. 调试&注释&打印输出

调试

   ruby属于解释型语言,即脚本,在linux上,脚本的执行无法三种:   

1. 用解释器运行脚本

解释器  脚本文件

即:ruby  脚本文件

2. 直接运行脚本

在脚本文件里面用 

#! 脚本解释器

定义好脚本解释器路径,然后再授予脚本执行权限,接着直接运行 

./脚本文件

即可。

3. 在解释器里面运行脚本

root@tommy:/home/ywt/ror_tests/ruby_tests# irb
2.1.5 :001 > str = "sdfsdf"
 => "sdfsdf"
2.1.5 :002 > puts str
sdfsdf
 => nil
2.1.5 :003 > print str
sdfsdf => nil
2.1.5 :004 >

 

ps:建议直接用第一种,第二种比较麻烦,第三种比较难看(当然try语法可以用这个)

注释

coment.rb

#single line comment
str = ‘hello world‘
=begin
this is a test of
mutiple line comments
=end
puts str

测试输出如下:

root@tommy:/home/ywt/ror_tests/ruby_tests# ruby comment.rb
hello world

即:

#单行注释
=begin
多行注释
=end

 

打印输出

print_test.rb

str =‘hello world‘
puts str
print str
puts ‘ =========‘

测试输出

root@tommy:/home/ywt/ror_tests/ruby_tests# ruby print_test.rb
hello world
hello world =========

即:

puts  str  =  print str  + print new_line  

(new_line在windows下面是 ‘\r\n‘ ,linux上面是 ‘\n‘)

 

一般定义

class Aclass
end

注意:类名的第一个字母必须大写!(python则无此要求)

 

继承的一般定义

class Child<Father
end

即继承符为‘<‘;

问题:是否支持多继承?

成员

成员变量

成员函数

 

[ruby on rails] 深入(2) ruby基本语法

标签:style   blog   color   sp   on   文件   div   问题   log   

原文地址:http://www.cnblogs.com/Tommy-Yu/p/4146375.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!