佳木斯湛栽影视文化发展公司

主頁 > 知識(shí)庫(kù) > Ruby語法筆記

Ruby語法筆記

熱門標(biāo)簽:企業(yè)做大做強(qiáng) 電話運(yùn)營(yíng)中心 Win7旗艦版 呼叫中心市場(chǎng)需求 百度AI接口 硅谷的囚徒呼叫中心 語音系統(tǒng) 客戶服務(wù)

接受用戶輸入

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
  end
end

class1 = Classname.new
class1.functionname('1')
unless
unless false
  print 'ok'
else
  print 'no'
end    

是否包含字符

print 'puts'

user_input = gets.chomp

user_input.downcase!

if user_input.include?"s"
  print 'has s'
end

替換字符

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

在字符串中輸出變量值

puts 'okok #{user_input}'

while

counter = 1

while counter  11
  puts counter
  counter = counter + 1
end

Until

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

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

for num in 1...10
  puts num
end  

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

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

Next

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

數(shù)組

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

The .each Iterator迭代器

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

# one way to loop
numbers.each { |item| puts item }

# another way to loop
numbers.each do |item|
 puts item
end

The .times Iterator 次數(shù)迭代器

10.times { print 'ok'})

Looping with 'While'

num = 1

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

Looping with 'Until'

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

Loop the Loop with Loop

num = 0

loop do
  num += 1
  print "Ruby!"
  break if num == 30
end  

The .split Method,

text.split(",")

puts "Text to search through: "
text = gets.chomp
puts "Word to redact"
redact = gets.chomp

words = text.split(" ")

words.each do |word|
  print word
end

您可能感興趣的文章:
  • Ruby的基礎(chǔ)語法入門學(xué)習(xí)教程
  • Ruby編程中的語法使用風(fēng)格推薦
  • Ruby基礎(chǔ)語法初探
  • Ruby中一些基本語法知識(shí)點(diǎn)的羅列匯總
  • ruby聲明式語法的實(shí)現(xiàn)例子
  • Ruby的基本語法學(xué)習(xí)總結(jié)
  • ruby 學(xué)習(xí)筆記(1) 初識(shí)語法
  • Ruby的語法和語言特性總結(jié)

標(biāo)簽:山西 安康 喀什 濟(jì)南 海南 長(zhǎng)沙 崇左 山西

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Ruby語法筆記》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    杭锦旗| 义马市| 牙克石市| 娱乐| 耿马| 民勤县| 故城县| 河源市| 新乡县| 鹤岗市| 湘潭县| 融水| 泰兴市| 湟源县| 高碑店市| 海安县| 通山县| 马公市| 怀仁县| 梨树县| 化州市| 新蔡县| 义乌市| 内黄县| 汤原县| 龙海市| 金山区| 柳河县| 黔东| 德庆县| 浑源县| 南江县| 扬中市| 防城港市| 五大连池市| 济源市| 香格里拉县| 江西省| 子洲县| 沙河市| 莎车县|