a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 編程 > Ruby > 正文

Ruby語法筆記

2020-10-29 19:36:37
字體:
來源:轉載
供稿:網友

接受用戶輸入

first_name = gets.chomp

首字母大寫

first_name.capitalize!

字母變大寫

first_name.upcase!

字母變小寫

first_name.downcase!

多行輸出

print <<EOF  # 多行輸出EOF

注釋

# 我是注釋

變量獲取

#{first_name}

變量

  1. 全局變量 $
  2. 類變量 @@
  3. 方法變量 @
  4. 局部變量 小寫字母或_

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  

The .split Method,

text.split(",")puts "Text to search through: "text = gets.chompputs "Word to redact"redact = gets.chompwords = text.split(" ")words.each do |word|  print wordend

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 免费看a| 你懂的在线视频播放 | 午夜高清视频 | 久久精品国产99国产精品 | 六月丁香av | 99re热精品视频 | 国产99久久精品 | 艹逼网| 欧美色视频在线观看 | 成人午夜视频在线观看 | 日韩精品人成在线播放 | 国产成人片| 在线看免费黄色片 | 久草视频污 | 久久成人精品一区二区三区 | 国产一区二区三区久久99 | 精品久久久久久一区二区 | 久久九九久久精品 | 色丁香婷婷 | 超碰在线看 | 卡通动漫第一页 | 不卡日韩在线 | 日韩av一区二区三区在线 | 91高清视频| 久草在线 | 神马香蕉久久 | 国产福利在线观看 | 日韩三及片 | 欧美性猛交一区二区三区精品 | 日本欧美一区 | 欧美日韩三级 | 欧美成人一区二区三区片免费 | 日韩精品在线一区二区 | 成人午夜天 | 超碰免费在线观看 | 一级毛片电影院 | 国产精品久久久久久久久久三级 | 日本免费在线 | 欧美日韩在线免费 | 欧美黄视频 | 成人性大片免费观看网站 |