Ruby Basic - 2
7415 ワード
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
コントロールキー
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
Reference
この問題について(Ruby Basic - 2), 我々は、より多くの情報をここで見つけました https://velog.io/@wpark/Ruby-Basic-2テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol