Learn to Program(2)
6. Flow Control
6.1 Comparison Method
puts 'cat' < 'dog'
result is true, because cat comes before dog in the directory.
6.2 Branching
That is branching, if what comes after the if is true, we run the code between the if and the end. If what comes after the if is false,
we don't.
if name == 'carl'
puts 'that is an nice name'
6.3 Looping
command = ''
while command != 'bye'
puts command
command = gets.chomp
end
puts 'The program comes an end'
7 Arrays and Iterators
[], [5], ['Hello', 'Goodbye'], [89.9, 'test', [true, false]]
names = ['Ada', 'Belle', 'Chris']
puts '1' + names.to_s
puts '2' + names[0]
puts '3' + names[1]
puts '4' + names[2]
1["Ada", "Belle", "Chris"]
2Ada
3Belle
4Chris
7.1 The Method each
each allows us to do something (whatever we want) to each object the array points to.
languages = ['English', 'German', 'Ruby']
languages.each do |lang|
puts 'I love ' + lang + '!'
end
I love English!
I love German!
I love Ruby!
3.times do
puts 'go'
end
go go go
7.2 More Array Methods
foods = ['artichoke', 'brioche', 'caramel']
puts foods.to_s
puts foods.join(', ')
["artichoke", "brioche", "caramel"]
artichoke, brioche, caramel
200.times do
puts []
end
puts an empty array 200 times means nothing.
favorites = []
favorites.push 'carl'
favorites.push 'kiko'
puts '0 =' + favorites[0]
puts 'last =' + favorites.last
puts 'length =' + favorites.length.to_s
puts 'pop =' + favorites.pop
puts 'length =' + favorites.length.to_s
0 =carl
last =kiko
length =2
pop =kiko
length =1
8. Writing Your Own Methods
def sayHello
puts 'say hello to everyone!'
end
sayHello
sayHello
say hello to everyone!
say hello to everyone!
8.1 Method Parameters
def saysomething words
puts 'my words: ' + words
end
saysomething 'carl is a good boy'
my words: carl is a good boy
8.2 Local Variables
We can access the variable, but we can not screw them up.
def saysomething words
words = 'nothing'
puts 'my words: ' + words
end
words = 'carl is here'
saysomething words
puts words
my words: nothing
carl is here
8.3 Return Values
puts always returns nil. Every method has to return something, even if it's just nil.
def saysomething words
puts 'my words: ' + words
'result value'
end
words = 'carl is here'
result = saysomething words
puts result
my words: carl is here
result value
references:
http://pine.fm/LearnToProgram/?Chapter=06
分享到:
相关推荐
ISBN-10: 0-9766940-4-2 ISBN-13: 978-1-934356-36-4 Printed on acid-free paper. P1.1 printing, March 2009 Version: 2009-7-22
Learn to Program with Python 英文无水印pdf pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者...
Learn to Program with Python:Take your first steps in programming and learn the powerful Python programming language,高清文字版
《Learn to Program》是一本专为初学者设计的编程教程,旨在帮助读者掌握编程基础知识,以英文版的形式呈现,让全球的学习者都能接触到这一宝贵的资源。在这个数字化的时代,编程技能已经成为一项重要的技能,不论是...
### 学习C++编程:McGraw-Hill出版社的《Learn To Program With C++》概览 #### 书籍背景与概述 《Learn To Program With C++》是McGraw-Hill出版社出版的一本关于C++编程的学习指南。这本书旨在为初学者提供一个...
https://www.amazon.cn/dp/148421868X/ref=sr_1_1?ie=UTF8&qid=1524202763&sr=8-1&keywords=learn+to+program+with+python 亚马逊上468原价的书,高清英文原版。
《Learn to Program with Small Basic》是一本专门针对儿童和少年编程教学的书籍,由Majed Marji和Ed Price编写,通过微软出品的Small Basic编程语言教授编程知识。该书旨在为初学者提供一种易于学习的编程方式,使...
Introduction to Programming_ Learn to program with Data StructuresIntroduction to Programming_ Learn to program with Data StructuresIntroduction to Programming_ Learn to program with Data ...
In Learn to Program with Scratch, author Majed Marji uses Scratch to explain the concepts essential to solvin g real-world programming problems. The labeled, color-coded blocks plainly show
2. **即时反馈**:读者可以在Minecraft中看到自己的代码产生的效果,这种即时反馈极大地增强了学习体验。 3. **分步教程**:书中包含一系列由简入难的编程练习,每个练习都附有详细的指导说明。 4. **Python编程基础...
Learn to Program with C teaches computer programming to the complete beginner using the native C language. As such, it assumes you have no knowledge whatsoever about programming. The main goal of this...