最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Ruby語(yǔ)法筆記

 更新時(shí)間:2016年02月15日 11:21:11   投稿:hebedich  
本文給大家記錄的是本人學(xué)習(xí)ruby之后所記錄下來(lái)的部分語(yǔ)法知識(shí),分享給有需要的小伙伴,希望對(duì)大家能夠有所幫助。

接受用戶輸入

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

相關(guān)文章

最新評(píng)論

灯塔市| 临清市| 肃南| 临江市| 桐柏县| 华蓥市| 金沙县| 池州市| 佛冈县| 衡东县| 个旧市| 巴里| 新乐市| 泰来县| 咸宁市| 柘城县| 景德镇市| 易门县| 修武县| 湖州市| 阿鲁科尔沁旗| 松溪县| 黄平县| 金乡县| 万年县| 泗阳县| 双江| 长寿区| 凤阳县| 永登县| 获嘉县| 亳州市| 宣恩县| 涞水县| 临武县| 鄄城县| 乌拉特后旗| 华阴市| 金门县| 思茅市| 内丘县|