Class
Class
์ ์
class NewClass
end
class NewClass; end
ํด๋์ค ๋ช ์ ์๋ฌธ ๋๋ฌธ์๋ก ์์ํด์ผํ๋ค.
์ธ์คํด์ค
(์ธ์คํด์ค) ๋ฉ์๋ = ํด๋์ค ์ ์ ๋ด์ ์ ์ํ ๋ฉ์๋๋ ํด๋น ํด๋์ค์ ์ธ์คํด์ค๋ก ํธ์ถ๋ ์ ์๋ค.
class Myclass def hello puts 'Hello' end end
์ธ์คํด์ค ์์ฑ / ํธ์ถ
ํด๋์ค๋ช
.new
my_object = Myclass.new
my_object.hello
class๋ ์์ํ๊ฑฐ๋ ์์ ๋ ์ ์๋ค.
๋ฃจ๋น๋ ๊ธฐ๋ณธํ์ด ์กด์ฌํ์ง ์๊ณ ๋ชจ๋ ๊ฒ์ด ๊ฐ์ฒด์ด๋ค.
p 1.to_s
#=> "1"
p true.to_s
#=> "true"
1๊ณผ ๊ฐ์ ์ซ์๋ true/false์ ๊ฐ์ ์ง๋ฆฟ ๊ฐ๋ ๊ฐ์ฒด์ด๋ค. ๊ฐ์ฒด์ด๊ธฐ ๋๋ฌธ์ ๋ฉ์๋๋ ํธ์ถ ํ ์ ์๋ค.
p 'hello'.class
#=> String
p String.ancestors
#=> [String, Comparable, Object, Kernel, BasicObject]
p 10.class
#=> Integer
Integer.ancestors
#=> [Integer, Numeric, Comparable, Object, Kernel, BasicObject]
p true.class
#=> TrueClass
TrueClass.ancestors
#=> [TrueClass, Object, Kernel, BasicObject]
๋ฌธ์์ด, ์์น ๋ฟ๋ง ์๋๋ผ true/false, nil๊ณผ ๊ฐ์ ๊ฐ์ฒด์๋ ํด๋์ค๊ฐ ์กด์ฌํ๋ค. Object์ ํด๋์ค์ ์์ ๊ด๊ณ๋ก ๊ตฌ์ฑ๋๋ค.
BasicObject๊ฐ ์ต์์ ํด๋์ค์ด๋ค.
๋ณ์์ ์์
์ง์ญ๋ณ์
์ค์ฝํ(์ฐธ์กฐ ๊ฐ๋ฅ ๋ฒ์)๊ฐ ๊ฐ์ฅ ์ข์ ์ข
๋ฅ์ ๋ณ์๋ค. ์ง์ญ ๋ณ์๋ช
์ ruby
๋ _ruby
์ฒ๋ผ ์ฒซ๊ธ์๋ ์๋ฌธ์ ์์ด ๋๋ ์ธ๋๋ฐ์ฌ์ผํ๋ค.
๋ธ๋ก
๋ฉ์๋ ์ ์
ํด๋์ค/๋ชจ๋ ์ ์
ํฑ ๋ ๋ฒจ
์ค์ฝํ ๋ฐ์ ์๋ ์ง์ญ๋ณ์๋ฅผ ์ฐธ์กฐํ๋ฉด ์๋ฌ๊ฐ ๋ฐ์ํ๋ค. ๋ธ๋ก ์์ด๋ฉด ๋ธ๋ก ๋ฐ์์ ์ ์๋ ์ง์ญ ๋ณ์๋ฅผ ์ฐธ์กฐํ ์ ์์ง๋ง, ๋ฐ๋๋ก ๋ธ๋ก์์์ ์ ์๋ ์ง์ญ ๋ณ์๋ ๋ธ๋ก ๋ฐ์์ ์ฐธ์ฌํ ์ ์๋ค.
greeting = "Hello, "
people = %w[๋คํ ํ๊ฒฝ ํ์ง]
people.each do |p|
puts greeting + p
end
#=> Hello, ๋คํ
#=> Hello, ํ๊ฒฝ
#=> Hello, ํ์ง
puts p
#=> nil
์ ์ญ๋ณ์($)
์ด๋์์๋ ์ฐธ์กฐ ๋ฐ ๋ณ๊ฒฝ์ด ๊ฐ๋ฅํ ๋ณ์์ด๋ค. ํ๋ก๊ทธ๋จ ๊ท๋ชจ๊ฐ ์ปค์ง๋ฉด ์ปค์ง์๋ก ์ ์ญ ๋ณ์๊ฐ ์กด์ฌํ๋ ์ฝ๋๋ ํด์์ด ์ด๋ ค์์ง๋ค.
์ ๋ง๋ก ํ์ํ ๊ฒฝ์ฐ๊ฐ ์๋๋ผ๋ฉด ์ฌ์ฉ์ ์์ ํ๋ ๊ฒ์ด ์ข๋ค.
$lion = "like"
$undefined
#=> nil
$undefined
์ ๊ฐ์ด ์กด์ฌํ์ง ์๋ ์ ์ญ๋ณ์๋ฅผ ์ฐธ์กฐํ๋ฉด nil
์ด ๋ฐํ๋๋ค.
์ธ์คํด์ค ๋ณ์(@)
์ธ์คํด์ค ๋ด์์๋ง ์ฐธ์กฐํ ์ ์๋ ๋ณ์์ด๋ค. ์ด๊ฒ์ ์ฌ์ฉํด์ ๊ฐ์ฒด์ ์ํ๋ฅผ ์ ์ฅํ ์ ์๋ค. ์ธ๋ถ์์ ์ธ์คํด์ค ๋ณ์์ ์ ๊ทผํ๋ ค๋ฉด ๋ณ๋์ ๋ฉ์๋๋ฅผ ์ ์ํด์ผํ๋ค.
# setter
class Ruler
def length=(val)
@length = val
end
def length
@length
end
end
ruler = Ruler.new
ruler.length = 30
ruler.length
์ค๋ช
์ ์ํด์ ๋ฉ์๋๋ฅผ ์ ์ํ์ผ๋ ์ค์ ๋ก๋ attr_accessor
๋ฅผ ์ด์ฉํ๋ค.
class Ruler
attr_accessor :length
end
๋ฅผ ํ๋ฉด Ruler#length=๋ผ๋ ์ธ์คํด์ค ๋ณ์๊ฐ ์ ์๋๋ค.
ํด๋์ค ๋ณ์(@@)
ํด๋์ค์ ํด๋น ์ธ์คํด์ค๋ฅผ ๋ฒ์๋ก ํ๋ ๋ณ์์ด๋ค.
ํด๋์ค ๋ณ์๋ ํด๋์ค ์ ์ ์์ด๋ ํด๋์ค ๋ฉ์๋์์ ์ฐธ์กฐํ ์ ์๋ค.
class MyClass
@@cvar = 'class variable'
def cvarin
puts @@cvar
end
def self.cvarin_class
puts @@cvar
end
end
self
self๋ ์ธ์คํด์ค๋ฅผ ์ฐธ์กฐํ๋ค. ๋ฉ์๋ ๋ด๋ถ์์ receiver๋ฅผ ์๋ตํ๋ฉด default๋ก self๊ฐ ๋ฆฌ์๋ฒ๊ฐ ๋๋ค.
class Ruler
attr_accessor :length
def set_default_length
self.length = 30
#self๋ฅผ ์๋ตํ๋ฉด length๋ผ๋ ์ง์ญ๋ณ์๊ฐ ์ ์ธ๋จ
end
end
์ด๊ธฐํ(์์ฑ์)
์ธ์คํด์ค๋ฅผ ์์ฑํ ๋ ์ธ์คํด์ค ์ด๊ธฐํ๊ฐ ํ์ํ ๊ฒฝ์ฐ๊ฐ ์๋ค. ์ด๋ initialize
๋ฉ์๋๋ฅผ ์ ์ํ๋ฉด ๋๋ค.
class Car
def initialize(make, model)
@make = make
@model = model
end
end
Create an instance of class .new
kitt = Car.new("Pontiac", "Trans Am")
ํด๋์ค ๋ฉ์๋
ํด๋์ค ๋ฉ์๋๋ ๋ฉ์๋๋ช
์์ self
๋ฅผ ๋ถ์ฌ์ ์ ์ํ๋ค.
class Ruler
attr_accessor :length
def self.pair
[Ruler.new, Ruler.new]
end
end
Ruler.pair
#=> [#<Ruler:0x00007ff8530e86b8>, #<Ruler:0x00007ff8530e8690>]
ํด๋์ค ๋ฉ์๋ ์์ ์๋ self๋ ํด๋น ๋ฉ์๋๊ฐ ์ํด ์๋ ํด๋์ค๋ฅผ ๊ฐ๋ฆฌํจ๋ค. ํด๋์ค ๋ฉ์๋๋ ํด๋์ค ๋ช ์ผ๋ก ์ ๊ทผ ํ ์ ์๋ค.
Inheritance (์์) <
<
class ์๋ธ ํด๋์ค < ์ํผ ํด๋์ค๋ช
# Some stuff!
end
<
๋ง๊ณ ๋ Class.new
์์๋ ์ธ์์ ์์ํ๊ณ ์ถ์ ๋ถ๋ชจ ํด๋์ค๋ฅผ ์ง์ ํ๋ฉด ๋๋ค.
SecondClass=Class.new(FirstClass)
#==> secondclass(์์ ํด๋์ค)
SecondClass.superclass
#==> firstclass(๋ถ๋ชจ ํด๋์ค)
์์๋ ํจ์๋ ๋ณ์๋ฅผ overrideํ ์ ์๋ค.
class Creature
def initialize(name)
@name = name
end
def fight
return "Punch to the chops!"
end
end
class Dragon<Creature
# ์ค๋ฒ๋ผ์ด๋ฉ
def fight
return "Breathes fire!"
end
end
์ค๋ฒ๋ผ์ด๋
super ํด๋์ค์์ ์ด๋ฏธ ์ ์๋ ๋ฉ์๋๋ฅผ sub ํด๋์ค์์ ๋ค์ ์ ์ํ๋ ๊ฒ์ ๋ฉ์๋ ์ค๋ฒ๋ผ์ด๋๋ผ๊ณ ํ๋ค.
super
super
super๋ ๋ถ๋ชจํด๋์ค(super class)์ ์ ์๋์ด์๋ initialize, ๋ฉ์๋๋ฅผ ํธ์ถํ ์ ์๋ค.
class DerivedClass < Base
def some_method
super(optional args)
# Some stuff
end
end
end
Public and Private
public
: allow for an interface with the rest of the program( This method can be called from outside the class )private
: for your classes to do their own work undisturbed( This method can't!)class ClassName # Some class stuff public # Public methods go here def public_method; end private # Private methods go here def private_method; end end
attr_reader
/ attr_writer
/attr_accessor
attr_reader
/ attr_writer
/attr_accessor
attr_reader
: to access a variableattr_writer
: to change itattr_accessor
: to make a variable readable and writeable in one fell swoopclass Person attr_reader :name attr_writer :name def initialize(name) @name = same end end
def name @name end def name=(value) @name = value end
That
name=
allowed to put an = sign in a method name.
module
์ธ์คํด์คํ ํ ์ ์๋ ํด๋์ค์ ๊ฐ์ ๊ฒ์ด๋ค.
module ModuleName
# Bits 'n pieces
end
ModuleName๊ท์น
-๋ชจ๋์ ์์ํ ๋ ๋๋ฌธ์! ex)Math -๋ชจ๋์ constant๋ ๋ชจ๋ ๋ฌธ์๋ฅผ ๋๋ฌธ์๋ก ex)PI
๋ดํฌํ ํด๋์ค๋ ๋ชจ๋์
::
๋ฅผ ํตํด์ ์ ๊ทผํ ์ ์๋ค.Math::PI
๋ชจ๋ ๋ด์์๋ ๋ฉ์๋๋ฅผ ์ ์ํ ์ ์๋ค. ๊ทธ๋ฌ๋ ํด๋์ค์ ์ ์ํ ๋ฉ์๋๋์ ๋ค๋ฅธ ์ฉ๋๋ก ์ฌ์ฉ๋๋ค.
ํน์ ๋ฉ์๋๋ฅผ ์ธ์คํด์ค ๋ฉ์๋๋ก ํฌํจ
ํน์ ๊ฐ์ฒด์ ๋ฉ์๋๋ก ํฌํจ
๋ชจ๋ ํจ์ ์ฌ์ฉ
--require
: already present in the interperter
require โmoduleโ
Mixin
: when module is used to mix additional behavior and information into a class
include
: mixes a moduleโs methods( any class that includes a certain module can use those module's methods!)class Angle include Math attr_accessor :radians def initialize(radians) @radians = radians end def cosine cos(@radians) end end
extend
: mixes a moduleโs methods at the classmodule ThePresent def now puts "It's #{Time.new.hour > 12 ? 'PM' : 'AM'} (GMT)." end end class TheHereAnd extend ThePresent end
Last updated
Was this helpful?