Class
Class
μ μ
ν΄λμ€ λͺ μ μλ¬Έ λλ¬Έμλ‘ μμν΄μΌνλ€.
μΈμ€ν΄μ€
(μΈμ€ν΄μ€) λ©μλ = ν΄λμ€ μ μ λ΄μ μ μν λ©μλλ ν΄λΉ ν΄λμ€μ μΈμ€ν΄μ€λ‘ νΈμΆλ μ μλ€.
μΈμ€ν΄μ€ μμ± / νΈμΆ
classλ μμνκ±°λ μμ λ μ μλ€.
루λΉλ κΈ°λ³Ένμ΄ μ‘΄μ¬νμ§ μκ³ λͺ¨λ κ²μ΄ κ°μ²΄μ΄λ€.
1κ³Ό κ°μ μ«μλ true/falseμ κ°μ μ§λ¦Ώ κ°λ κ°μ²΄μ΄λ€. κ°μ²΄μ΄κΈ° λλ¬Έμ λ©μλλ νΈμΆ ν μ μλ€.
λ¬Έμμ΄, μμΉ λΏλ§ μλλΌ true/false, nilκ³Ό κ°μ κ°μ²΄μλ ν΄λμ€κ° μ‘΄μ¬νλ€. Objectμ ν΄λμ€μ μμ κ΄κ³λ‘ ꡬμ±λλ€.
BasicObjectκ° μ΅μμ ν΄λμ€μ΄λ€.
λ³μμ μμ
μ§μλ³μ
μ€μ½ν(μ°Έμ‘° κ°λ₯ λ²μ)κ° κ°μ₯ μ’μ μ’
λ₯μ λ³μλ€. μ§μ λ³μλͺ
μ ruby
λ _ruby
μ²λΌ 첫κΈμλ μλ¬Έμ μμ΄ λλ μΈλλ°μ¬μΌνλ€.
λΈλ‘
λ©μλ μ μ
ν΄λμ€/λͺ¨λ μ μ
ν± λ 벨
μ€μ½ν λ°μ μλ μ§μλ³μλ₯Ό μ°Έμ‘°νλ©΄ μλ¬κ° λ°μνλ€. λΈλ‘ μμ΄λ©΄ λΈλ‘ λ°μμ μ μλ μ§μ λ³μλ₯Ό μ°Έμ‘°ν μ μμ§λ§, λ°λλ‘ λΈλ‘μμμ μ μλ μ§μ λ³μλ λΈλ‘ λ°μμ μ°Έμ¬ν μ μλ€.
μ μλ³μ($)
μ΄λμμλ μ°Έμ‘° λ° λ³κ²½μ΄ κ°λ₯ν λ³μμ΄λ€. νλ‘κ·Έλ¨ κ·λͺ¨κ° 컀μ§λ©΄ 컀μ§μλ‘ μ μ λ³μκ° μ‘΄μ¬νλ μ½λλ ν΄μμ΄ μ΄λ €μμ§λ€.
μ λ§λ‘ νμν κ²½μ°κ° μλλΌλ©΄ μ¬μ©μ μμ νλ κ²μ΄ μ’λ€.
$undefined
μ κ°μ΄ μ‘΄μ¬νμ§ μλ μ μλ³μλ₯Ό μ°Έμ‘°νλ©΄ nil
μ΄ λ°νλλ€.
μΈμ€ν΄μ€ λ³μ(@)
μΈμ€ν΄μ€ λ΄μμλ§ μ°Έμ‘°ν μ μλ λ³μμ΄λ€. μ΄κ²μ μ¬μ©ν΄μ κ°μ²΄μ μνλ₯Ό μ μ₯ν μ μλ€. μΈλΆμμ μΈμ€ν΄μ€ λ³μμ μ κ·Όνλ €λ©΄ λ³λμ λ©μλλ₯Ό μ μν΄μΌνλ€.
μ€λͺ
μ μν΄μ λ©μλλ₯Ό μ μνμΌλ μ€μ λ‘λ attr_accessor
λ₯Ό μ΄μ©νλ€.
λ₯Ό νλ©΄ Ruler#length=λΌλ μΈμ€ν΄μ€ λ³μκ° μ μλλ€.
ν΄λμ€ λ³μ(@@)
ν΄λμ€μ ν΄λΉ μΈμ€ν΄μ€λ₯Ό λ²μλ‘ νλ λ³μμ΄λ€.
ν΄λμ€ λ³μλ ν΄λμ€ μ μ μμ΄λ ν΄λμ€ λ©μλμμ μ°Έμ‘°ν μ μλ€.
self
selfλ μΈμ€ν΄μ€λ₯Ό μ°Έμ‘°νλ€. λ©μλ λ΄λΆμμ receiverλ₯Ό μλ΅νλ©΄ defaultλ‘ selfκ° λ¦¬μλ²κ° λλ€.
μ΄κΈ°ν(μμ±μ)
μΈμ€ν΄μ€λ₯Ό μμ±ν λ μΈμ€ν΄μ€ μ΄κΈ°νκ° νμν κ²½μ°κ° μλ€. μ΄λ initialize
λ©μλλ₯Ό μ μνλ©΄ λλ€.
Create an instance of class .new
ν΄λμ€ λ©μλ
ν΄λμ€ λ©μλλ λ©μλλͺ
μμ self
λ₯Ό λΆμ¬μ μ μνλ€.
ν΄λμ€ λ©μλ μμ μλ selfλ ν΄λΉ λ©μλκ° μν΄ μλ ν΄λμ€λ₯Ό κ°λ¦¬ν¨λ€. ν΄λμ€ λ©μλλ ν΄λμ€ λͺ μΌλ‘ μ κ·Ό ν μ μλ€.
Inheritance (μμ) <
<
<
λ§κ³ λ Class.new
μμλ μΈμμ μμνκ³ μΆμ λΆλͺ¨ ν΄λμ€λ₯Ό μ§μ νλ©΄ λλ€.
μμλ ν¨μλ λ³μλ₯Ό overrideν μ μλ€.
μ€λ²λΌμ΄λ
super ν΄λμ€μμ μ΄λ―Έ μ μλ λ©μλλ₯Ό sub ν΄λμ€μμ λ€μ μ μνλ κ²μ λ©μλ μ€λ²λΌμ΄λλΌκ³ νλ€.
super
super
superλ λΆλͺ¨ν΄λμ€(super class)μ μ μλμ΄μλ initialize, λ©μλλ₯Ό νΈμΆν μ μλ€.
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!)
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 swoopThat
name=
allowed to put an = sign in a method name.
module
μΈμ€ν΄μ€ν ν μ μλ ν΄λμ€μ κ°μ κ²μ΄λ€.
ModuleNameκ·μΉ
-λͺ¨λμ μμν λ λλ¬Έμ! ex)Math -λͺ¨λμ constantλ λͺ¨λ λ¬Έμλ₯Ό λλ¬Έμλ‘ ex)PI
λ΄ν¬ν ν΄λμ€λ λͺ¨λμ
::
λ₯Ό ν΅ν΄μ μ κ·Όν μ μλ€.λͺ¨λ λ΄μμλ λ©μλλ₯Ό μ μν μ μλ€. κ·Έλ¬λ ν΄λμ€μ μ μν λ©μλλμ λ€λ₯Έ μ©λλ‘ μ¬μ©λλ€.
νΉμ λ©μλλ₯Ό μΈμ€ν΄μ€ λ©μλλ‘ ν¬ν¨
νΉμ κ°μ²΄μ λ©μλλ‘ ν¬ν¨
λͺ¨λ ν¨μ μ¬μ©
--require
: already present in the interperter
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!)extend
: mixes a moduleβs methods at the class
Last updated
Was this helpful?