sub学习rubyonrails

本blog主要包括ruby,RubyonRails(RoR),ajax,css的学习经验和sub的web2.0观察,主要关注社会书签(social bookmarking)和链接挖掘和管理(links digging and management)

Saturday, September 23, 2006

My first ruby experiencs

ruby主页上对ruby的定义是
A dynamic, open source programming language with a focus on simplicity and productivity. It has an elegant syntax that is natural to read and easy to write.
什么是动态语言呢,wikipedia上的定义是
In computer science, a dynamic programming language is a kind of programming language in which programs can change their structure as they run: functions may be introduced or removed, new classes of objects may be created, new modules may appear. As a side effect of this dynamism, most dynamic programming languages are dynamically typed, which static typing advocates consider a drawback.According to advocates of dynamic programming languages, however, the flexibility of dynamic languages offsets these drawbacks, and even provides advantages so considerable as to make this an essential feature, for example for interactive programming.
简单的理解,可能可以是动态类型的语言是动态语言
什么是动态的类型呢,大概可以理解为不需要静态的指定变量类型

看了ruby主页上的Ruby in Twenty Minutes ,一点感觉:
1.常见的elseif被缩写成elsif,非常不习惯
2.一个类被定义后,还可以为该类定义新的方法,非常动态吧
3.一个非常impressive的tricky:if __FILE__ == $0,其中__FILE__是代码所在文件名的变量,$0是启动程序的文件名,如果当前文件==启动程序的文件,说明在执行这个文件,那么就继续执行下面的代码
这种情况是比如说比如这个文件被编成库,运行的程序名肯定不等于这个文件名,那后面的代码就不用执行
这个后面可以放一些单元测试的代码
4.借鉴的lisp中的closure:
@names.each do
|name|
puts "Hello #{name}!"
这句的意思是说,对@name中的每个元素,执行puts "Hello #{name}!",(#{}是把括号里的量转化为字符串),简化for(i=1;i<=@names.length;i++) dosomething这种写法
当然还有更加强大的用法,见MartinFlower写的closure ,MartinFlower将其认为是很多rubyist喜欢ruby的一个原因

0 Comments:

Post a Comment

<< Home