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 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 : to access a variable

  • attr_writer : to change it

  • attr_accessor : to make a variable readable and writeable in one fell swoop

    class 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 class

    module ThePresent
      def now
        puts "It's #{Time.new.hour > 12 ? 'PM' : 'AM'} (GMT)."
        end
    end
    class TheHereAnd
      extend ThePresent
    end

Last updated