Ruby 제어문 - 조건문과 반복문
Published: (December 30, 2025 at 11:41 PM EST)
1 min read
Source: Dev.to
Source: Dev.to
조건문
s1 = 'Jonathan'
s2 = 'Jonathan'
s3 = s1
if s1 == s2
puts 'Both Strings have identical content'
else
puts 'Both Strings do not have identical content'
end
if name == 'Satish'
puts 'What a nice name!!'
elsif name == 'Sunil'
puts 'Another nice name!'
end
unless는 if not과 동일합니다.
unless ARGV.length == 2
puts "Usage: program.rb 23 45"
exit
end
puts "Enrollments will now Stop" if participants > 2500
year = 2000
leap = case
when year % 400 == 0 then true
when year % 100 == 0 then false
else year % 4 == 0
end
puts leap # true
반복문
rice_on_square = 1
64.times do |square|
puts "On square #{square + 1} are #{rice_on_square} grain(s)"
rice_on_square *= 2
end
# 간단한 형태
5.times { puts "Mice!\n" }
var = 0
while var
# (추가 로직이 필요합니다)
end