a great resource to learn ruby as a link below:
codeacademy:https://www.codecademy.com/courses/ruby-beginner-en-d1Ylq/0/5?curriculum_id=5059f8619189a5000201fbcb&pro-cta-modal=1
1. puts 与 print区别:puts换行
2. 6个数学运算符:+, -, *, /, **(4**3=64), %
3.methods of String
1).length
2).reverse
3).upcase and .downcase
4.single comment and multi-line comments: # and
=begin
"this are multi-lines comments"
=end
5.name convention: lowercase string + underscore like "my_name"
6.chain methods together:
7.控制台输入 :gets.chomp, 只大写第一个字母capitalize!, 应用变量#{first_name}
8.notice that elsif , if we want to check whether it's false, then we use unless.
9. .include? method is Great!
10.replace substring to anther: input.gsub!(/substring/, anothersubstring)
11. loop: while, until,
for num in 1...11
puts num #1, 2, 3, 4...10, 1...11, think about 2 dots
end
loop:
i = 20
loop do
i -= 1
print "#{i}"
break if i <= 0
end
12.next is to skip some values you don't need
for i in 1..20
next if i % 2 == 1
puts i
end
13. 致命的循环,很容易走上死循环
i = 20
loop do
i -= 1
next if i % 2 == 1
print i
break if i <= 0
end
14.上面的for,loop都不算什么,.each才说王道。
1)两种用法
object.each do |item|
object.each { |item|
2) 3.times do
print "hello"
end
15.用while和until,for分别实现打印从1到50
1)j = 1
until j == 51 do
print j
j += 1
end
2)i = 1
while i <= 50 do
print i
i += 1
end
3)for i in 1..50
print i
end
4)print "Ruby!" for 30 times
i = 0
loop do
print "Ruby!"
i += 1
break if i == 30
end
another simeple way: 30.times do print "Ruby!" end
16.same as javascript and java, Ruby also has .split method