国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Ruby語法筆記

2019-10-26 19:28:46
字體:
來源:轉載
供稿:網友

接受用戶輸入

first_name = gets.chomp

首字母大寫

first_name.capitalize!

字母變大寫

first_name.upcase!

字母變小寫

first_name.downcase!

多行輸出

print <<EOF  # 多行輸出EOF

注釋

# 我是注釋

變量獲取

#{first_name}

變量

    全局變量 $ 類變量 @@ 方法變量 @ 局部變量 小寫字母或_

if/else

if a < b  puts '1'elsif b < a  puts '2'end    


class Classname  def functionname(params)    puts params  endendclass1 = Classname.newclass1.functionname('1')unlessunless false  print 'ok'else  print 'no'end    

是否包含字符

print 'puts'user_input = gets.chompuser_input.downcase!if user_input.include?"s"  print 'has s'end

替換字符

# s -> thuser_input.gsub!(/s/,"th")

在字符串中輸出變量值

puts 'okok #{user_input}'

while

counter = 1while counter < 11  puts counter  counter = counter + 1end

Until

counter = 1until counter > 10  print counter  counter = counter + 1end  

+= 、 -= 、 *=、 /=
Some languages have the increment operators ++ and -- (which also add or subtract 1 from a value), but Ruby does not
for循環
# 如果 1...10 包含1-9,如果 1..10 包含1-10

for num in 1...10  puts numend  

Loop Method
An iterator is just a Ruby method that repeatedly invokes a block of code.

i = 20loop do  i -= 1  print "#{ i }"  break if i <= 0end  

Next

i = 20loop do i -= 1 next if i%2 != 0 print "#{i}" break if i <= 0end

數組

my_array = [1,2,3,4,5]

The .each Iterator迭代器

numbers = [1, 2, 3, 4, 5]# one way to loopnumbers.each { |item| puts item }# another way to loopnumbers.each do |item| puts itemend

The .times Iterator 次數迭代器

10.times { print 'ok'})

Looping with 'While'

num = 1while num <= 50 do  print num  num += 1end  

Looping with 'Until'

num = 1until num > 50 do  print num  num += 1end  

Loop the Loop with Loop

num = 0loop do  num += 1  print "Ruby!"  break if num == 30end              
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 松阳县| 永修县| 鹤庆县| 宿迁市| 桃源县| 双城市| 绍兴县| 湟中县| 佛山市| 黔西| 鸡东县| 江北区| 陇南市| 双江| 基隆市| 津南区| 兰坪| 浦城县| 游戏| 吴川市| 紫云| 佛教| 平度市| 图木舒克市| 温宿县| 嘉祥县| 兴山县| 湖南省| 洱源县| 施秉县| 通道| 苏尼特右旗| 依兰县| 洛宁县| 东莞市| 河北省| 南皮县| 绥棱县| 锡林浩特市| 射阳县| 那曲县|