Ruby little details to notice

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:

name = "GREAT xufei"
name.downcase
         .reverse
         .upcase

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值