Ruby Examples
This commit is contained in:
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