Ruby Basic - 2


1.条件文

if expression
	...
elsif expression
	...
else
	...
end
elsifがない限り.
unless expression
	...
else
	...
end
1行でもよい
"Hello" if i_am_nice
"Go away" else

2.例文

case
when expression
	...
when expression
	...
else
	...
end
case test_value
when value
	...
when value
	...
else
	...
end

3. short hand operator

> boolean ? result1 : result2
# y 값이 있으면 y, 없다면 z를 x에 할당.
> x = y || z
# x 값이 없으면 x에 y값을 할당
> x ||= y

4. loop


コントロールキー
  • 中断:繰り返し停止
  • next:次のループ
  • にジャンプ
  • redo:サイクル繰返し
  • 再試行:フルループ再試行
  • i = 5
    loop do
    	break if i <= 0
        puts "Countdown: #{i}"
        i -= 1
    end
    puts "Blast off!"
    while boolean
    	...
    end
    
    until boolean
    	...
    end
    イテレーションの使用
    5.times do
        puts "Hello !"
    end
    
    1.upto(5) { puts "Hello" }
    5.downto(1) { puts "Hello" }
    (1..5).each { puts "Hello" }
    "stirng\nstring\n".each_line {|line| puts line }
    "stirng".each_char {|char| puts char }
    "stirng".each_byte {|byte| puts byte }
    array.each
    array.each_index
    array.each_with_index
    hash.each
    hash.each_key
    hash.each_value
    hash.each_pair
    
    for fruit in ["banana", "apple", "pear"]
    	puts fruit.capitalize
    end