Delbert's Octopress Blog

A blogging framework for hackers.

Ruby 补充笔记 1

完成了 Learn Ruby The Hard Way ,但是在 Codecademy 上面的课程还是没有完成。今天抽空从头复习了一下,发现了不少在 lrthw 中没有提到的地方。

字符串方法

1
2
3
4
5
6
7
.length
.reverse
.upcase
.downcase
.capitalize

.gsub!(/s/,"th") # replace s with th, while gsub is short for **g**lobal **sub**stitution. 

多行注释

1
2
3
4
5
6
=begin
注释
Multi-Line Comments
=end

# Single-Line Comments

控制流

if/else

1
2
3
4
5
6
7
if true
  # do something
elsif
  # do something else
else
  # others
end

unless/else

1
2
3
4
5
6
7
unless false
  # do something
elsif
  # do something else
else
  # others
end

循环

while

1
2
3
while true
  # do something
end

until

1
2
3
until false
  # do something
end

loop

1
2
3
4
5
loop do
  next if true # equals continus
  # do something
  break if true
end

.each

Multi-Line

1
2
3
4
array = [1, 2, 3]
array.each do |x|
  # do something
end

Single-Line

1
object.each { |item| # Do something }

.times

1
3.times{print "Delbert"} # print "Delbert" out for 3 times{print

Comments