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

unlessif 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
Back to Blog

Related posts

Read more »

Ruby 기초 - 문법과 기본 개념

Ruby 프로그래밍 언어의 기본 문법과 개념을 알아봅니다. 파일 및 실행 ruby 파일 형식: 파일명.rb 실행 방법: ruby 파일명.rb 확장자가 rb가 아니어도 실행 가능 주석 ruby 한 줄 주석 =begin 여러 줄 주석 =end 줄바꿈 및 명령 구분 ruby 줄바꿈 puts...

Why Bash Syntax Feels So Arcane

Bash’s Historical Roots Bash runs deep beneath the surface of Linux. With only a line or two, it can take full control of your machine. It is often viewed by b...

My Simple Tic-Tac-Toe Game

As part of Codecademy's portfolio project series, I built an interactive Tic‑Tac‑Toe game that runs entirely in the terminal. This was my first complete Python...

Ruby 블록과 Lambda

Ruby 블록Block Ruby의 블록Block은 메서드 호출과 함께 코드 블록을 전달하는 방법입니다. ruby def call_block puts 'Start of method' yield yield puts 'End of method' end call_block { puts 'In...