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
level6/51.rb Executable file
View 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
View 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
View 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
View 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
View 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
View 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
View 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
View 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
View 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
View 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