Ruby Examples

This commit is contained in:
2026-01-15 14:01:21 +09:00
commit cf3d7d3296
76 changed files with 1191 additions and 0 deletions

10
level5/41.rb Executable file
View 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
View 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
View 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
View 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
View 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
View 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
View 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
View 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
View 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
View 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!"}