python和ruby之争,不好说什么,个人还是比较喜欢python,不过ruby也相当不错。
Python与Ruby
目测python会比较适合编程入门学习,而不是C。遗憾的是,这两个都不是我的入门语言,而是古老的海龟。
只是因为python用得比较多,不过python和ruby算是有点对立的,从语法上就可以看出来,从宗旨上也是如此。。
Python's Philosophy: “There should be one and preferably only one way to do it.”
Ruby's Philosophy:"There are many ways to do it"
Python可以做得到简化一个程序,不过有时候会变成是一个过程。强大的库及模块算是python的最大优势,换句说,我们要做的就是集中于我们的想法。(转载保留 Phodal's Blog Phodal's zenthink)
Arduino与AVR
Python与Ruby
import json
import urllib
url="http://localhost/athome/1"
date=urllib.urlopen(url)
result=json.load(date)
print result[0]['led1']
def fun
@a
end
两者是等效的
def fun
return @a
不过Ruby的函数有一个不错的东东,算是可以让人喜欢的,就是返回多个值
def fun
@a,@b
end
不过,也正是这种方法太多,会让人有点不适应。
def self.define_component(name)
define_method(name){
...
day.send"#{name}"
}
end
self.define_component:weekday
self.define_component:weekend
这个的效果会等效于
def weekend
day.weekend
end
def weekday
day.weekday
end
不过这个还是相当有效的删除重复代码的方式,至于使用missing_method()我更多的时候会觉得可读性有点低 ,宁愿把上面的代码变为
def component(name)
day.send"#{name}"
end
def weekend
component:weekend
end
def weekday
component:weekday
end
可读性相当不错,不是么。