Ruby Examples
This commit is contained in:
58
.gitignore
vendored
Normal file
58
.gitignore
vendored
Normal file
@@ -0,0 +1,58 @@
|
||||
### Ruby template
|
||||
*.gem
|
||||
*.rbc
|
||||
/.config
|
||||
/coverage/
|
||||
/InstalledFiles
|
||||
/pkg/
|
||||
/spec/reports/
|
||||
/spec/examples.txt
|
||||
/test/tmp/
|
||||
/test/version_tmp/
|
||||
/tmp/
|
||||
|
||||
# Used by dotenv library to load environment variables.
|
||||
# .env
|
||||
|
||||
# Ignore Byebug command history file.
|
||||
.byebug_history
|
||||
|
||||
## Specific to RubyMotion:
|
||||
.dat*
|
||||
.repl_history
|
||||
build/
|
||||
*.bridgesupport
|
||||
build-iPhoneOS/
|
||||
build-iPhoneSimulator/
|
||||
|
||||
## Specific to RubyMotion (use of CocoaPods):
|
||||
#
|
||||
# We recommend against adding the Pods directory to your .gitignore. However
|
||||
# you should judge for yourself, the pros and cons are mentioned at:
|
||||
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
|
||||
#
|
||||
# vendor/Pods/
|
||||
|
||||
## Documentation cache and generated files:
|
||||
/.yardoc/
|
||||
/_yardoc/
|
||||
/doc/
|
||||
/rdoc/
|
||||
|
||||
## Environment normalization:
|
||||
/.bundle/
|
||||
/vendor/bundle
|
||||
/lib/bundler/man/
|
||||
|
||||
# for a library or gem, you might want to ignore these files since the code is
|
||||
# intended to run in multiple environments; otherwise, check them in:
|
||||
# Gemfile.lock
|
||||
# .ruby-version
|
||||
# .ruby-gemset
|
||||
|
||||
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
||||
.rvmrc
|
||||
|
||||
# Used by RuboCop. Remote config files pulled in from inherit_from directive.
|
||||
# .rubocop-https?--*
|
||||
|
||||
10
.idea/.gitignore
generated
vendored
Normal file
10
.idea/.gitignore
generated
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
# 디폴트 무시된 파일
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# 쿼리 파일을 포함한 무시된 디폴트 폴더
|
||||
/queries/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
||||
# 에디터 기반 HTTP 클라이언트 요청
|
||||
/httpRequests/
|
||||
8
.idea/modules.xml
generated
Normal file
8
.idea/modules.xml
generated
Normal file
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/ruby-examples.iml" filepath="$PROJECT_DIR$/.idea/ruby-examples.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
15
.idea/ruby-examples.iml
generated
Normal file
15
.idea/ruby-examples.iml
generated
Normal file
@@ -0,0 +1,15 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="RUBY_MODULE" version="4">
|
||||
<component name="ModuleRunConfigurationManager">
|
||||
<shared />
|
||||
</component>
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/features" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/spec" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
|
||||
</content>
|
||||
<orderEntry type="jdk" jdkName="ruby-4.0.0-p0" jdkType="RUBY_SDK" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
||||
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
||||
119
README.md
Normal file
119
README.md
Normal file
@@ -0,0 +1,119 @@
|
||||
# 루비 연습 문제
|
||||
|
||||
## 1단계: 루비 기초 감각 익히기 (초급 입문)
|
||||
|
||||
**목표**: 문법, 출력, 변수, 제어문에 익숙해지기
|
||||
|
||||
1. `"Hello, Ruby!"`를 출력하는 프로그램 작성
|
||||
2. 이름을 변수에 저장하고 `"안녕하세요, 이름님"` 출력
|
||||
3. 두 정수를 변수로 받아 합, 차, 곱, 몫 출력
|
||||
4. 숫자를 입력받아 짝수/홀수 판별
|
||||
5. 점수(0~100)를 받아 A/B/C/D/F 등급 출력
|
||||
6. 1부터 100까지 출력 (for, while 각각 사용)
|
||||
7. 1~100 중 3의 배수만 출력
|
||||
8. 사용자 입력이 `"exit"`이면 종료되는 루프 작성
|
||||
9. 구구단 2단 출력
|
||||
10. 구구단 전체 출력
|
||||
|
||||
---
|
||||
|
||||
## 2단계: 메서드와 조건 분기 (초급)
|
||||
|
||||
**목표**: 메서드 정의, 리턴값, 재사용
|
||||
|
||||
11. 두 수를 받아 더한 값을 반환하는 메서드 작성
|
||||
12. 세 수 중 최댓값을 반환하는 메서드
|
||||
13. 숫자를 받아 절댓값을 반환하는 메서드
|
||||
14. 문자열을 받아 길이를 반환하는 메서드
|
||||
15. 원의 반지름을 받아 넓이를 계산하는 메서드
|
||||
16. 나이를 받아 성인/미성년자 판별 메서드
|
||||
17. 문자열과 반복 횟수를 받아 반복 출력하는 메서드
|
||||
18. BMI 계산 메서드 작성
|
||||
19. 비밀번호 길이가 8자 이상인지 검사하는 메서드
|
||||
20. 윤년 여부를 판단하는 메서드
|
||||
|
||||
---
|
||||
|
||||
## 3단계: 배열(Array)과 반복 (초급 → 중급)
|
||||
|
||||
**목표**: 컬렉션과 반복 처리에 익숙해지기
|
||||
|
||||
21. 숫자 배열의 합을 구하기
|
||||
22. 배열에서 최댓값, 최솟값 찾기
|
||||
23. 배열 요소를 하나씩 출력 (each 사용)
|
||||
24. 배열의 짝수만 출력
|
||||
25. 문자열 배열에서 글자 수가 5 이상인 단어만 출력
|
||||
26. 배열 요소를 모두 대문자로 변환
|
||||
27. 배열에서 특정 값이 있는지 검사
|
||||
28. 배열의 평균값 구하기
|
||||
29. 중복 제거된 배열 만들기
|
||||
30. 배열을 오름차순/내림차순 정렬
|
||||
|
||||
---
|
||||
|
||||
## 4단계: 해시(Hash) 다루기 (중급 초입)
|
||||
|
||||
**목표**: 키-값 구조 이해
|
||||
|
||||
31. 사람의 이름과 나이를 해시로 표현
|
||||
32. 해시에서 특정 키의 값 출력
|
||||
33. 모든 키와 값을 `"이름: 나이"` 형식으로 출력
|
||||
34. 가장 나이가 많은 사람 찾기
|
||||
35. 점수 해시에서 평균 점수 계산
|
||||
36. 해시에 새로운 요소 추가
|
||||
37. 해시에서 특정 조건(나이 ≥ 30)인 항목만 출력
|
||||
38. 해시 키 목록만 배열로 추출
|
||||
39. 해시 값 목록만 배열로 추출
|
||||
40. 단어 빈도수 계산 프로그램
|
||||
|
||||
---
|
||||
|
||||
## 5단계: 블록과 Enumerable (중급)
|
||||
|
||||
**목표**: 루비스러운 코드 작성
|
||||
|
||||
41. `map`을 사용해 배열의 제곱값 배열 생성
|
||||
42. `select`로 조건에 맞는 값 필터링
|
||||
43. `reject`로 특정 조건 제거
|
||||
44. `reduce`로 배열 합계 계산
|
||||
45. 문자열 배열을 하나의 문자열로 합치기
|
||||
46. 배열에서 가장 긴 문자열 찾기
|
||||
47. 해시를 배열로 변환
|
||||
48. 배열을 해시로 변환 (index → 값)
|
||||
49. `each_with_index` 사용
|
||||
50. 사용자 정의 메서드에 블록 전달하기
|
||||
|
||||
---
|
||||
|
||||
## 6단계: 문자열 처리 (중급)
|
||||
|
||||
**목표**: 텍스트 처리 감각 익히기
|
||||
|
||||
51. 문자열에서 모음 개수 세기
|
||||
52. 문자열 뒤집기
|
||||
53. 특정 문자 제거
|
||||
54. 공백 기준으로 단어 분리
|
||||
55. 단어 수 세기
|
||||
56. 문자열이 회문인지 검사
|
||||
57. 이메일 형식 간단 검증
|
||||
58. 파일명에서 확장자 추출
|
||||
59. snake_case → camelCase 변환
|
||||
60. 문자열 압축 (aaabb → a3b2)
|
||||
|
||||
---
|
||||
|
||||
## 7단계: 객체지향 기초 (중급)
|
||||
|
||||
**목표**: 클래스와 객체 개념 이해
|
||||
|
||||
61. Person 클래스 정의 (이름, 나이)
|
||||
62. 인스턴스 메서드 작성
|
||||
63. 생성자(`initialize`) 사용
|
||||
64. 접근자(`attr_accessor`) 사용
|
||||
65. 나이 증가 메서드
|
||||
66. 여러 Person 객체를 배열로 관리
|
||||
67. 클래스 메서드 작성
|
||||
68. 간단한 BankAccount 클래스
|
||||
69. 입금/출금 기능 구현
|
||||
70. 잔액 부족 예외 처리
|
||||
|
||||
10
level1/01.rb
Executable file
10
level1/01.rb
Executable file
@@ -0,0 +1,10 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
`"Hello, Ruby!"`를 출력하는 프로그램 작성
|
||||
=end
|
||||
|
||||
puts "Hello, Ruby!"
|
||||
|
||||
# puts는 개행 포함 출력
|
||||
11
level1/02.rb
Executable file
11
level1/02.rb
Executable file
@@ -0,0 +1,11 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
이름을 변수에 저장하고 `"안녕하세요, 이름님"` 출력
|
||||
=end
|
||||
|
||||
name = "홍길동"
|
||||
puts "안녕하세요, #{name}님."
|
||||
|
||||
# 루비는 #{} 문자열 보간 사용
|
||||
17
level1/03.rb
Executable file
17
level1/03.rb
Executable file
@@ -0,0 +1,17 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
두 정수를 변수로 받아 합, 차, 곱, 몫 출력
|
||||
=end
|
||||
|
||||
print "숫자 1: "
|
||||
number1 = gets.chomp.to_i
|
||||
print "숫자 2: "
|
||||
number2 = gets.chomp.to_i
|
||||
|
||||
puts "#{number1} + #{number2} = #{number1 + number2}"
|
||||
puts "#{number1} - #{number2} = #{number1 - number2}"
|
||||
puts "#{number1} * #{number2} = #{number1 * number2}"
|
||||
puts "#{number1} / #{number2} = #{number1 / number2}"
|
||||
puts "#{number1} % #{number2} = #{number1 % number2}"
|
||||
11
level1/04.rb
Executable file
11
level1/04.rb
Executable file
@@ -0,0 +1,11 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
숫자를 입력받아 짝수/홀수 판별
|
||||
=end
|
||||
|
||||
print "숫자 : "
|
||||
number = gets.chomp.to_i
|
||||
|
||||
puts number.even? ? "짝수" : "홀수"
|
||||
23
level1/05.rb
Executable file
23
level1/05.rb
Executable file
@@ -0,0 +1,23 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
점수(0~100)를 받아 A/B/C/D/F 등급 출력
|
||||
=end
|
||||
|
||||
print "점수(0~100) : "
|
||||
number = gets.chomp.to_i
|
||||
|
||||
grade = case number
|
||||
when 90..100
|
||||
"A"
|
||||
when 80..89
|
||||
"B"
|
||||
when 70..79
|
||||
"C"
|
||||
when 60..69
|
||||
"D"
|
||||
else
|
||||
"F"
|
||||
end
|
||||
puts "#{number}점은 #{grade} 등급입니다."
|
||||
19
level1/06.rb
Executable file
19
level1/06.rb
Executable file
@@ -0,0 +1,19 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
1부터 100까지 출력 (for, while 각각 사용)
|
||||
=end
|
||||
|
||||
for i in 1..100
|
||||
puts i
|
||||
end
|
||||
|
||||
i = 1
|
||||
while i <= 100 do
|
||||
puts i
|
||||
i += 1
|
||||
end
|
||||
|
||||
(1..100).each { |item| puts item }
|
||||
|
||||
11
level1/07.rb
Executable file
11
level1/07.rb
Executable file
@@ -0,0 +1,11 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
1~100 중 3의 배수만 출력
|
||||
=end
|
||||
|
||||
(1..100).each do |item|
|
||||
puts item if (item % 3 == 0)
|
||||
end
|
||||
|
||||
12
level1/08.rb
Executable file
12
level1/08.rb
Executable file
@@ -0,0 +1,12 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
사용자 입력이 `"exit"`이면 종료되는 루프 작성
|
||||
=end
|
||||
|
||||
loop do
|
||||
print "입력 : "
|
||||
break if gets.chomp == "exit"
|
||||
end
|
||||
|
||||
11
level1/09.rb
Executable file
11
level1/09.rb
Executable file
@@ -0,0 +1,11 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
구구단 2단 출력
|
||||
=end
|
||||
|
||||
(1..9).each do |item|
|
||||
puts "2 * #{item} = #{2 * item}"
|
||||
end
|
||||
|
||||
14
level1/10.rb
Executable file
14
level1/10.rb
Executable file
@@ -0,0 +1,14 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
구구단 전체 출력
|
||||
=end
|
||||
|
||||
(2..9).each do |i|
|
||||
(1..9).each do |j|
|
||||
puts "#{i} * #{j} = #{i * j}"
|
||||
end
|
||||
puts
|
||||
end
|
||||
|
||||
17
level2/11.rb
Executable file
17
level2/11.rb
Executable file
@@ -0,0 +1,17 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
두 수를 받아 더한 값을 반환하는 메서드 작성
|
||||
=end
|
||||
|
||||
def add(a, b)
|
||||
a + b
|
||||
end
|
||||
|
||||
print "숫자 1: "
|
||||
number1 = gets.chomp.to_i
|
||||
print "숫자 2: "
|
||||
number2 = gets.chomp.to_i
|
||||
|
||||
puts "#{number1} + #{number2} = #{add(number1, number2)}"
|
||||
12
level2/12.rb
Executable file
12
level2/12.rb
Executable file
@@ -0,0 +1,12 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
세 수 중 최댓값을 반환하는 메서드
|
||||
=end
|
||||
|
||||
def max(a, b, c)
|
||||
[a, b, c].max
|
||||
end
|
||||
|
||||
puts "Max = #{max(5, 2, 9)}"
|
||||
12
level2/13.rb
Executable file
12
level2/13.rb
Executable file
@@ -0,0 +1,12 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
숫자를 받아 절댓값을 반환하는 메서드
|
||||
=end
|
||||
|
||||
def abs(a)
|
||||
a.abs
|
||||
end
|
||||
|
||||
puts "Abs = #{abs(-7)}"
|
||||
14
level2/14.rb
Executable file
14
level2/14.rb
Executable file
@@ -0,0 +1,14 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
문자열을 받아 길이를 반환하는 메서드
|
||||
=end
|
||||
|
||||
def len(s)
|
||||
s.length
|
||||
end
|
||||
|
||||
print "?"
|
||||
s = gets.chomp
|
||||
puts "#{s}의 길이는 #{len(s)}"
|
||||
12
level2/15.rb
Executable file
12
level2/15.rb
Executable file
@@ -0,0 +1,12 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
원의 반지름을 받아 넓이를 계산하는 메서드
|
||||
=end
|
||||
|
||||
def area(r)
|
||||
Math::PI * r ** 2
|
||||
end
|
||||
|
||||
puts "Area = #{area(2)}"
|
||||
12
level2/16.rb
Executable file
12
level2/16.rb
Executable file
@@ -0,0 +1,12 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
나이를 받아 성인/미성년자 판별 메서드
|
||||
=end
|
||||
|
||||
def adult?(age)
|
||||
age >= 20
|
||||
end
|
||||
|
||||
puts adult?(13) ? "성인입니다." : "미성년자입니다."
|
||||
12
level2/17.rb
Executable file
12
level2/17.rb
Executable file
@@ -0,0 +1,12 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
문자열과 반복 횟수를 받아 반복 출력하는 메서드
|
||||
=end
|
||||
|
||||
def repeat(s, i)
|
||||
s * i
|
||||
end
|
||||
|
||||
puts repeat("도깨비", 6)
|
||||
16
level2/18.rb
Executable file
16
level2/18.rb
Executable file
@@ -0,0 +1,16 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
BMI 계산 메서드 작성
|
||||
=end
|
||||
|
||||
def bmi(kg, cm)
|
||||
weight = kg.to_f
|
||||
height = cm.to_f / 100
|
||||
|
||||
weight / (height ** 2)
|
||||
end
|
||||
|
||||
puts "BMI = #{bmi(70, 170)}" # 24.22
|
||||
puts "BMI = #{bmi(75, 175)}" # 24.5
|
||||
12
level2/19.rb
Executable file
12
level2/19.rb
Executable file
@@ -0,0 +1,12 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
비밀번호 길이가 8자 이상인지 검사하는 메서드
|
||||
=end
|
||||
|
||||
def password_valid?(pw)
|
||||
pw.length >= 8
|
||||
end
|
||||
|
||||
puts password_valid?("dfjirgaofmrioa") ? "OK" : "Nope"
|
||||
19
level2/20.rb
Executable file
19
level2/20.rb
Executable file
@@ -0,0 +1,19 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
윤년 여부를 판단하는 메서드
|
||||
|
||||
## 윤년의 조건
|
||||
1. 서력 기원 연수가 004로 나누어 떨어지는 해는 윤년으로 한다.
|
||||
2. 서력 기원 연수가 100으로 나누어 떨어지는 해는 평년으로 한다.
|
||||
3. 서력 기원 연수가 400으로 나누어 떨어지는 해는 윤년으로 둔다.
|
||||
=end
|
||||
|
||||
def leap_year?(year)
|
||||
year %4 == 0 && (year %100 != 0 || year %400 == 0)
|
||||
end
|
||||
|
||||
puts leap_year?(2000) ? "윤년" : "평년"
|
||||
puts leap_year?(2020) ? "윤년" : "평년"
|
||||
puts leap_year?(2025) ? "윤년" : "평년"
|
||||
10
level3/21.rb
Executable file
10
level3/21.rb
Executable file
@@ -0,0 +1,10 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
숫자 배열의 합을 구하기
|
||||
=end
|
||||
|
||||
arr = [1,3,5,7,9]
|
||||
|
||||
puts arr.sum
|
||||
11
level3/22.rb
Executable file
11
level3/22.rb
Executable file
@@ -0,0 +1,11 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
배열에서 최댓값, 최솟값 찾기
|
||||
=end
|
||||
|
||||
arr = [5, 7, 4, 3, 2, 9, 1, 6]
|
||||
|
||||
puts "Min = #{arr.min}"
|
||||
puts "Max = #{arr.max}"
|
||||
10
level3/23.rb
Executable file
10
level3/23.rb
Executable file
@@ -0,0 +1,10 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
배열 요소를 하나씩 출력 (each 사용)
|
||||
=end
|
||||
|
||||
arr = [5, 7, 4, 3, 2, 9, 1, 6]
|
||||
|
||||
arr.each { |i| puts i }
|
||||
16
level3/24.rb
Executable file
16
level3/24.rb
Executable file
@@ -0,0 +1,16 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
배열의 짝수만 출력
|
||||
=end
|
||||
|
||||
arr = [5, 7, 4, 3, 2, 9, 1, 6]
|
||||
|
||||
arr.each do |num|
|
||||
puts num if num.even?
|
||||
end
|
||||
|
||||
# select 필터 사용
|
||||
arr.select { |v| v.even? }
|
||||
.each { |v| puts v }
|
||||
12
level3/25.rb
Executable file
12
level3/25.rb
Executable file
@@ -0,0 +1,12 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
문자열 배열에서 글자 수가 5 이상인 단어만 출력
|
||||
=end
|
||||
|
||||
arr = %w[this is a test array]
|
||||
|
||||
# select 필터 사용
|
||||
arr.select { |v| v.length >= 5 }
|
||||
.each { |v| puts v }
|
||||
12
level3/26.rb
Executable file
12
level3/26.rb
Executable file
@@ -0,0 +1,12 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
배열 요소를 모두 대문자로 변환
|
||||
=end
|
||||
|
||||
arr = %w[this is a test array]
|
||||
|
||||
# select 필터 사용
|
||||
arr.map { |v| v.upcase }
|
||||
.each { |v| puts v }
|
||||
10
level3/27.rb
Executable file
10
level3/27.rb
Executable file
@@ -0,0 +1,10 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
배열에서 특정 값이 있는지 검사
|
||||
=end
|
||||
|
||||
arr = %w[this is a test array]
|
||||
|
||||
puts arr.include?("hash") ? "Yes" : "No"
|
||||
10
level3/28.rb
Executable file
10
level3/28.rb
Executable file
@@ -0,0 +1,10 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
배열의 평균값 구하기
|
||||
=end
|
||||
|
||||
arr = [5, 7, 4, 3, 2, 9, 1, 6]
|
||||
|
||||
puts arr.sum / arr.size.to_f
|
||||
10
level3/29.rb
Executable file
10
level3/29.rb
Executable file
@@ -0,0 +1,10 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
중복 제거된 배열 만들기
|
||||
=end
|
||||
|
||||
arr = [5, 7, 4, 3, 2, 9, 1, 6, 2, 9, 1, 6, 7, 4, 3, 2, 9]
|
||||
|
||||
puts arr.uniq
|
||||
12
level3/30.rb
Executable file
12
level3/30.rb
Executable file
@@ -0,0 +1,12 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
배열을 오름차순/내림차순 정렬
|
||||
=end
|
||||
|
||||
arr = [5, 7, 4, 3, 2, 9, 1, 6]
|
||||
|
||||
puts arr.sort
|
||||
|
||||
puts arr.sort.reverse
|
||||
10
level4/31.rb
Executable file
10
level4/31.rb
Executable file
@@ -0,0 +1,10 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
사람의 이름과 나이를 해시로 표현
|
||||
=end
|
||||
|
||||
person = { name: "Charlie", age: 13 }
|
||||
|
||||
puts person
|
||||
10
level4/32.rb
Executable file
10
level4/32.rb
Executable file
@@ -0,0 +1,10 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
해시에서 특정 키의 값 출력
|
||||
=end
|
||||
|
||||
person = { name: "Charlie", age: 13 }
|
||||
|
||||
puts person[:name]
|
||||
18
level4/33.rb
Executable file
18
level4/33.rb
Executable file
@@ -0,0 +1,18 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
모든 키와 값을 "이름: 나이" 형식으로 출력
|
||||
=end
|
||||
|
||||
person = { name: "Charlie", age: 13 }
|
||||
|
||||
person.each do |key, value|
|
||||
puts "#{key}: #{value}"
|
||||
end
|
||||
|
||||
|
||||
people = [{ name: "Charlie", age: 13 },
|
||||
{ name: "Steve", age: 34 },
|
||||
{ name: "Anne", age: 23 }]
|
||||
people.each { |item| puts "#{item[:name]}: #{item[:age]}"}
|
||||
14
level4/34.rb
Executable file
14
level4/34.rb
Executable file
@@ -0,0 +1,14 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
가장 나이가 많은 사람 찾기
|
||||
=end
|
||||
|
||||
people = [{ name: "Charlie", age: 13 },
|
||||
{ name: "Steve", age: 34 },
|
||||
{ name: "Anne", age: 23 }]
|
||||
|
||||
person = people.max_by { |_| _[:age] }
|
||||
|
||||
puts person[:name]
|
||||
14
level4/35.rb
Executable file
14
level4/35.rb
Executable file
@@ -0,0 +1,14 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
점수 해시에서 평균 점수 계산
|
||||
=end
|
||||
|
||||
people = [{ name: "Charlie", score: 13 },
|
||||
{ name: "Steve", score: 34 },
|
||||
{ name: "Anne", score: 23 }]
|
||||
|
||||
avg = people.sum { |_| _[:score] } / people.length.to_f
|
||||
|
||||
puts avg
|
||||
12
level4/36.rb
Executable file
12
level4/36.rb
Executable file
@@ -0,0 +1,12 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
해시에 새로운 요소 추가
|
||||
=end
|
||||
|
||||
person = { name: "Charlie", score: 13 }
|
||||
|
||||
person[:address] = "Seoul"
|
||||
|
||||
puts person
|
||||
13
level4/37.rb
Executable file
13
level4/37.rb
Executable file
@@ -0,0 +1,13 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
해시에서 특정 조건(나이 ≥ 30)인 항목만 출력
|
||||
=end
|
||||
|
||||
people = [{ name: "Charlie", score: 13 },
|
||||
{ name: "Steve", score: 34 },
|
||||
{ name: "Anne", score: 23 }]
|
||||
|
||||
people.select { |item| item[:score] >= 30 }
|
||||
.each { |item| puts "#{item[:name]}: #{item[:score]}" }
|
||||
12
level4/38.rb
Executable file
12
level4/38.rb
Executable file
@@ -0,0 +1,12 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
해시 키 목록만 배열로 추출
|
||||
=end
|
||||
|
||||
person = { name: "Charlie", score: 13 }
|
||||
|
||||
keys = person.keys
|
||||
|
||||
puts keys
|
||||
12
level4/39.rb
Executable file
12
level4/39.rb
Executable file
@@ -0,0 +1,12 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
해시 값 목록만 배열로 추출
|
||||
=end
|
||||
|
||||
person = { name: "Charlie", score: 13 }
|
||||
|
||||
keys = person.values
|
||||
|
||||
puts keys
|
||||
13
level4/40.rb
Executable file
13
level4/40.rb
Executable file
@@ -0,0 +1,13 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
단어 빈도수 계산 프로그램
|
||||
=end
|
||||
|
||||
words = "This is a test This is only a test".downcase.split(" ")
|
||||
|
||||
result = Hash.new
|
||||
words.each { |w| result[w] = result[w]?result[w]+1:1}
|
||||
|
||||
puts result
|
||||
10
level5/41.rb
Executable file
10
level5/41.rb
Executable file
@@ -0,0 +1,10 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
map을 사용해 배열의 제곱값 배열 생성
|
||||
=end
|
||||
|
||||
array = [2, 3, 5, 7, 9]
|
||||
|
||||
puts array.map { |item| item ** 2 }
|
||||
10
level5/42.rb
Executable file
10
level5/42.rb
Executable file
@@ -0,0 +1,10 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
select로 조건에 맞는 값 필터링
|
||||
=end
|
||||
|
||||
array = [2, 3, 5, 7, 9]
|
||||
|
||||
puts array.select { |item| item.odd? }
|
||||
10
level5/43.rb
Executable file
10
level5/43.rb
Executable file
@@ -0,0 +1,10 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
reject로 특정 조건 제거
|
||||
=end
|
||||
|
||||
array = [2, 3, 5, 7, 9]
|
||||
|
||||
puts array.reject { |item| item.even? }
|
||||
10
level5/44.rb
Executable file
10
level5/44.rb
Executable file
@@ -0,0 +1,10 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
reduce로 배열 합계 계산
|
||||
=end
|
||||
|
||||
array = [2, 3, 5, 7, 9]
|
||||
|
||||
puts array.reduce { |sum,item| sum += item }
|
||||
10
level5/45.rb
Executable file
10
level5/45.rb
Executable file
@@ -0,0 +1,10 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
문자열 배열을 하나의 문자열로 합치기
|
||||
=end
|
||||
|
||||
array = ["Hello", "World"]
|
||||
|
||||
puts array.join(" ")
|
||||
12
level5/46.rb
Executable file
12
level5/46.rb
Executable file
@@ -0,0 +1,12 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
배열에서 가장 긴 문자열 찾기
|
||||
=end
|
||||
|
||||
array = %w[This is a Hello Ruby examples]
|
||||
|
||||
word = array.max_by{ |str| str.length }
|
||||
|
||||
puts word
|
||||
10
level5/47.rb
Executable file
10
level5/47.rb
Executable file
@@ -0,0 +1,10 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
해시를 배열로 변환
|
||||
=end
|
||||
|
||||
person = { name: "Charlie", age: 13 }
|
||||
|
||||
puts person.to_a
|
||||
11
level5/48.rb
Executable file
11
level5/48.rb
Executable file
@@ -0,0 +1,11 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
배열을 해시로 변환 (index → 값)
|
||||
=end
|
||||
|
||||
people = ["Charlie", "Steve", "Anne"]
|
||||
|
||||
i = 0
|
||||
puts people.map { |e| { name: e, index: i = i + 1 } }
|
||||
12
level5/49.rb
Executable file
12
level5/49.rb
Executable file
@@ -0,0 +1,12 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
each_with_index 사용
|
||||
=end
|
||||
|
||||
people = ["Charlie", "Steve", "Anne"]
|
||||
|
||||
h = people.each_with_index.to_h
|
||||
|
||||
puts h
|
||||
12
level5/50.rb
Executable file
12
level5/50.rb
Executable file
@@ -0,0 +1,12 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
사용자 정의 메서드에 블록 전달하기
|
||||
=end
|
||||
|
||||
def my_method
|
||||
puts yield if block_given?
|
||||
end
|
||||
|
||||
my_method {"Hello, Ruby!"}
|
||||
10
level6/51.rb
Executable file
10
level6/51.rb
Executable file
@@ -0,0 +1,10 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
문자열에서 모음 개수 세기
|
||||
=end
|
||||
|
||||
str = "Hello World"
|
||||
|
||||
puts str.count("aeiou")
|
||||
10
level6/52.rb
Executable file
10
level6/52.rb
Executable file
@@ -0,0 +1,10 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
문자열 뒤집기
|
||||
=end
|
||||
|
||||
str = "Hello World"
|
||||
|
||||
puts str.reverse
|
||||
10
level6/53.rb
Executable file
10
level6/53.rb
Executable file
@@ -0,0 +1,10 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
특정 문자 제거
|
||||
=end
|
||||
|
||||
str = "Hello World"
|
||||
|
||||
puts str.delete("aeiou")
|
||||
10
level6/54.rb
Executable file
10
level6/54.rb
Executable file
@@ -0,0 +1,10 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
공백 기준으로 단어 분리
|
||||
=end
|
||||
|
||||
str = "Hello World"
|
||||
|
||||
puts str.split
|
||||
10
level6/55.rb
Executable file
10
level6/55.rb
Executable file
@@ -0,0 +1,10 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
단어 수 세기
|
||||
=end
|
||||
|
||||
str = "Hello World"
|
||||
|
||||
puts str.split.count
|
||||
10
level6/56.rb
Executable file
10
level6/56.rb
Executable file
@@ -0,0 +1,10 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
문자열이 회문인지 검사
|
||||
=end
|
||||
|
||||
str = "Hello World"
|
||||
|
||||
puts str == str.reverse
|
||||
14
level6/57.rb
Executable file
14
level6/57.rb
Executable file
@@ -0,0 +1,14 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
이메일 형식 간단 검증
|
||||
=end
|
||||
|
||||
str = "hello@examples.com"
|
||||
|
||||
puts str =~ /\A\S+@\S+\.\S+\z/ ? "Valid" : "Invalid"
|
||||
|
||||
# ---
|
||||
|
||||
puts str.match?(/\A\S+@\S+\.\S+\z/) ? "Valid" : "Invalid"
|
||||
10
level6/58.rb
Executable file
10
level6/58.rb
Executable file
@@ -0,0 +1,10 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
파일명에서 확장자 추출
|
||||
=end
|
||||
|
||||
str = "hello.txt"
|
||||
|
||||
puts File.extname(str)
|
||||
19
level6/59.rb
Executable file
19
level6/59.rb
Executable file
@@ -0,0 +1,19 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
snake_case → camelCase 변환
|
||||
=end
|
||||
|
||||
str = "hello_world"
|
||||
|
||||
puts str.split("_")
|
||||
.map { |s| s.capitalize}
|
||||
.join
|
||||
|
||||
# ---
|
||||
|
||||
x = str.gsub(/_[a-z]/) do |s|
|
||||
s.to_s.delete("_").upcase
|
||||
end
|
||||
puts x
|
||||
13
level6/60.rb
Executable file
13
level6/60.rb
Executable file
@@ -0,0 +1,13 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
문자열 압축 (aaabb → a3b2)
|
||||
=end
|
||||
|
||||
str = "hello world"
|
||||
|
||||
|
||||
puts str.chars
|
||||
.chunk{|c| c.itself}
|
||||
.map{|c,a| "#{c}#{a.size}"}.join
|
||||
19
level7/61.rb
Executable file
19
level7/61.rb
Executable file
@@ -0,0 +1,19 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
Person 클래스 정의 (이름, 나이)
|
||||
=end
|
||||
|
||||
class Person
|
||||
attr_accessor :name, :age
|
||||
|
||||
def initialize(name, age)
|
||||
@name = name
|
||||
@age = age
|
||||
end
|
||||
end
|
||||
|
||||
charlie = Person.new("Charlie", 13)
|
||||
p charlie
|
||||
puts charlie.name
|
||||
22
level7/62.rb
Executable file
22
level7/62.rb
Executable file
@@ -0,0 +1,22 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
인스턴스 메서드 작성
|
||||
=end
|
||||
|
||||
class Person
|
||||
attr_accessor :name, :age
|
||||
|
||||
def initialize(name, age)
|
||||
@name = name
|
||||
@age = age
|
||||
end
|
||||
|
||||
def hello
|
||||
puts "Hello, I'm #{name}!"
|
||||
end
|
||||
end
|
||||
|
||||
charlie = Person.new("Charlie", 13)
|
||||
puts charlie.hello
|
||||
19
level7/63.rb
Executable file
19
level7/63.rb
Executable file
@@ -0,0 +1,19 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
생성자(initialize) 사용
|
||||
=end
|
||||
|
||||
class Person
|
||||
attr_accessor :name, :age
|
||||
|
||||
def initialize(name, age)
|
||||
@name = name
|
||||
@age = age
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
charlie = Person.new("Charlie", 13)
|
||||
puts charlie.hello
|
||||
19
level7/64.rb
Executable file
19
level7/64.rb
Executable file
@@ -0,0 +1,19 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
접근자(attr_accessor) 사용
|
||||
=end
|
||||
|
||||
class Person
|
||||
attr_accessor :name, :age
|
||||
|
||||
def initialize(name, age)
|
||||
@name = name
|
||||
@age = age
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
charlie = Person.new("Charlie", 13)
|
||||
puts charlie.hello
|
||||
23
level7/65.rb
Executable file
23
level7/65.rb
Executable file
@@ -0,0 +1,23 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
나이 증가 메서드
|
||||
=end
|
||||
|
||||
class Person
|
||||
attr_accessor :name, :age
|
||||
|
||||
def initialize(name, age)
|
||||
@name = name
|
||||
@age = age
|
||||
end
|
||||
|
||||
def grow_up
|
||||
@age += 1
|
||||
end
|
||||
end
|
||||
|
||||
charlie = Person.new("Charlie", 13)
|
||||
charlie.grow_up
|
||||
p charlie
|
||||
25
level7/66.rb
Executable file
25
level7/66.rb
Executable file
@@ -0,0 +1,25 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
여러 Person 객체를 배열로 관리
|
||||
=end
|
||||
|
||||
class Person
|
||||
attr_accessor :name, :age
|
||||
|
||||
def initialize(name, age)
|
||||
@name = name
|
||||
@age = age
|
||||
end
|
||||
|
||||
def grow_up
|
||||
@age += 1
|
||||
end
|
||||
end
|
||||
|
||||
people = [
|
||||
Person.new("Charlie", 13),
|
||||
Person.new("Steve", 48),
|
||||
Person.new("Anne", 34)
|
||||
]
|
||||
24
level7/67.rb
Executable file
24
level7/67.rb
Executable file
@@ -0,0 +1,24 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
클래스 메서드 작성
|
||||
=end
|
||||
|
||||
class Person
|
||||
attr_accessor :name, :age
|
||||
|
||||
def initialize(name, age)
|
||||
@name = name
|
||||
@age = age
|
||||
end
|
||||
|
||||
def self.species
|
||||
"Homo Sapiens"
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
puts Person.species
|
||||
charlie = Person.new("Charlie", 13)
|
||||
puts charlie.class.species
|
||||
24
level7/68.rb
Executable file
24
level7/68.rb
Executable file
@@ -0,0 +1,24 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
간단한 BankAccount 클래스
|
||||
=end
|
||||
|
||||
class BankAccount
|
||||
def initialize(balance)
|
||||
@balance = balance
|
||||
end
|
||||
|
||||
def deposit(amount)
|
||||
@balance += amount
|
||||
end
|
||||
|
||||
def withdraw(amount)
|
||||
@balance -= amount
|
||||
end
|
||||
|
||||
def get_balance
|
||||
@balance
|
||||
end
|
||||
end
|
||||
24
level7/69.rb
Executable file
24
level7/69.rb
Executable file
@@ -0,0 +1,24 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
간단한 BankAccount 클래스
|
||||
=end
|
||||
|
||||
class BankAccount
|
||||
def initialize(balance)
|
||||
@balance = balance
|
||||
end
|
||||
|
||||
def deposit(amount)
|
||||
@balance += amount
|
||||
end
|
||||
|
||||
def withdraw(amount)
|
||||
@balance -= amount
|
||||
end
|
||||
|
||||
def get_balance
|
||||
@balance
|
||||
end
|
||||
end
|
||||
35
level7/70.rb
Executable file
35
level7/70.rb
Executable file
@@ -0,0 +1,35 @@
|
||||
#!/usr/bin/ruby
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
=begin
|
||||
잔액 부족 예외 처리
|
||||
=end
|
||||
|
||||
class BankAccount
|
||||
def initialize(balance)
|
||||
@balance = balance
|
||||
end
|
||||
|
||||
def deposit(amount)
|
||||
@balance += amount
|
||||
end
|
||||
|
||||
def withdraw(amount)
|
||||
begin
|
||||
raise StandardError if @balance < amount
|
||||
@balance -= amount
|
||||
rescue
|
||||
puts "잔액이 부족합니다."
|
||||
@balance
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
def get_balance
|
||||
@balance
|
||||
end
|
||||
end
|
||||
|
||||
account = BankAccount.new(100)
|
||||
puts account.withdraw(99)
|
||||
puts account.withdraw(5)
|
||||
Reference in New Issue
Block a user