1. 변수
루비에서 변수는 데이터를 저장하는 데 사용됩니다, 변수는 = 기호를 사용해 값을 할당할 수 있습니다.
# 변수 선언 및 값 할당
name = "HE"
age = 30
2. 데이터 타입
Ruby는 여러가지 데이터 타입을 지원하며, 주요 데이터 타입은 아래와 같습니다.
- 문자열(String): 문자열은 텍스트를 선언하고 출력하는데 사용됩니다.
# 문자열 나타내기
plus = "Hello, World"
print "#{plus}" # Hello, World
- 정수(Integer): 정수값을 나타냅니다
# 정수값
count = 10
print "this count: #{count}" # 10
- 부동소수점(Float): 소수점을 표현한 숫자입니다.
# 부동 소수점
price = 16.99
print "#{price}" # 16.99
- 불리언(Boolean): 참(True) 또는 거짓(False)값을 가집니다
# 불리언(Boolean) 타입
is_active = true;
is_false = false;
print "루비(Ruby)는 동적 객체 지향 스크립트 프로그래밍 언어이다:#{is_active}"
- 배열(array): 배열(array)는 여러 값을 순서대로 저장합니다.
# 배열(Array)
faint = ["Red", "Blue", "Green", "Yellow"]
print "#{faint}" # ["Red", "Blue", "Green", "Yellow"]
- 해시(Hash): 키-값 쌍으로 데이터를 저장합니다.
# 해시(Hash)
user = { name: "thinky", job: "Fullstack Blogger"}
print "#{user}" # {:name=>"thinky", :job=>"Fullstack Blogger"}
3. 연산자
루비에서 기본 연산자는 다음과 같습니다.
- 산술 연산자
# 산술 연산자
sum = 5 + 3 # 덧셈
differance = 10 - 5 # 뺄셈
product = 5 * 8 # 곱셈
quotient = 8 / 4 # 나눗셈
print "#{sum}, #{differance}, #{product}, #{quotient}" # 8, 5, 40, 2
- 비교 연산자
# 비교 연산자
is_equal = (10 == 10) # true
is_not_equal = (5 != 6) # false
is_greater = (5 > 3) # true
is_less = (5 < 3) # small true
- 논리 연산자
# 논리 연산자
and_result = true && false ## And 연산자
or_result = true || false ## or 연산자
not_result = !true ## ! NOT 연산자
GitHub - set-up-init/ruby-tutorial-tistory: https://thinky.tistory.com/category/Back-End/Ruby
https://thinky.tistory.com/category/Back-End/Ruby. Contribute to set-up-init/ruby-tutorial-tistory development by creating an account on GitHub.
github.com
LIST
'Back-End > Ruby' 카테고리의 다른 글
[Ruby] 6장 모듈과 믹스인 (0) | 2025.03.07 |
---|---|
[Ruby] 5장 클래스와 객체 지향 프로그래밍 (0) | 2025.03.01 |
[Ruby] 4장 함수 및 메서드 (0) | 2025.02.26 |
[Ruby] 3장 Ruby의 제어흐름 (0) | 2025.02.24 |
[Ruby] 1강 Ruby란 무엇인가? Ruby의 탄생 (0) | 2025.02.15 |