First I should say rubymonk is a great web site for ruby learners.
I just have some time this afternoon so I dived my self into this language and found it quite amazing!
Look at this code snippet below. Besides a variable parameter list, it's also possible to add naming parameters to your method arguments.
def add(*numbers)
numbers.inject(0) { |sum, number| sum + number }
end
def subtract(*numbers)
sum = numbers.shift
numbers.inject(sum) { |sum, number| sum - number }
end
def calculate(*arguments)
# if the last argument is a Hash, extract it
# otherwise create an empty Hash
options = arguments[-1].is_a?(Hash) ? arguments.pop : {}
options[:add] = true if options.empty?
return add(*arguments) if options[:add]
return subtract(*arguments) if options[:subtract]
end
invoking add(4, 5) returns 9 ✔
invoking add(-10, 2, 3) returns -5 ✔
invoking add(0, 0, 0, 0) returns 0 ✔
invoking subtract(4, 5) returns -1 ✔
invoking subtract(-10, 2, 3) returns -15 ✔
invoking subtract(0, 0, 0, 0, -10) returns 10 ✔
defaults to addtion when no option is specified ✔
invoking calculate(4, 5, add: true) returns 9 ✔
invoking calculate(-10, 2, 3, add: true) returns -5 ✔
invoking calculate(0, 0, 0, 0, add: true) returns 0 ✔
invoking calculate(4, 5, subtract: true) returns -1 ✔
invoking calculate(-10, 2, 3, subtract: true) returns -15 ✔
invoking calculate(0, 0, 0, 0, -10, subtract: true) returns 10 ✔
分享到:
相关推荐
国外非常好的一本Ruby on Rails入门教程。文字清晰,代码彩色高亮,有目录,方便阅读。
学习Ruby on Rails 4.0的逐步指南。 它包括针对Ruby 2.0.0的基本教程,是为至少了解另一种编程语言并熟悉HTML的程序员编写的。
老外写的书,随便看看吧
作为Codecademy的Learn Ruby课程的一部分,已完成所有练习的集合。 达菲鸭嘴机 使用一些新的Ruby字符串方法练习控制流,以Daffy Duckify用户的字符串,用“ th”替换每个“ s”。 已编辑 练习数组和迭代器-使用迭代...
Learn the principles behind object-oriented programming and within a few chapters create a fully functional Ruby application. You’ll also gain a basic understanding of many ancillary technologies ...
Ruby_learning_教程-中文版 Ruby 是一种跨平台,面向对象的解释型语言。他是本着简单实用的原则设计出来的
Learn the principles behind object-oriented programming and within a few chapters create a fully functional Ruby application. You'll also gain a basic understanding of many ancillary technologies such...
Shaw is the author of the popular online books Learn Python the Hard Way, Learn Ruby the Hard Way, and Learn C the Hard Way. He is also the creator of several open source software projects like ...
Beginning Ruby is a thoroughly contemporary guide for every type of reader who wants to learn Ruby, from novice programmers to web developers to Ruby newcomers. It starts by explaining the principles ...
Learn the principles behind object-oriented programming and within a few chapters create a fully functional Ruby application. You'll also gain a basic understanding of many ancillary technologies such...
If you’re a web developer or designer ready to learn Ruby on Rails, this hands-on guide is the ideal way to get started. Rather than toss you into the middle of the framework’s Model-View-Controller...
ruby book ,learn ruby book
学习 Rails 通过 RailsApps 项目的教程学习 Rails。
学习 Rails 通过 RailsApps 项目的教程学习 Rails。
学习 Rails == 这是我使用 RailsApps 项目中的教程构建的应用程序 Learning Rails。 通过
If you’re a web developer or designer ready to learn Ruby on Rails, this hands-on guide is the ideal way to get started. Rather than toss you into the middle of the framework’s Model-View-Controller...
If you’re a web developer or designer ready to learn Ruby on Rails, this hands-on guide is the ideal way to get started. Rather than toss you into the middle of the framework’s Model-View-Controller...
In Part I, you'll learn Ruby and Rails fundamentals and then dive straight into models, controllers, views, and deployment. In Part II, you'll take your skills to the next level as you build a social...
Ruby.on.Rails.Tutorial,Learn.Web.Development.with.Rails,Third.Edition-中文版 文字版.pdf 个人收集电子书,仅用学习使用,不可用于商业用途,如有版权问题,请联系删除!