프로그래밍루비 pdf를 샀으니 열심히 공부해야지. 그닥 특별한건 없는데. attr_reader랑 attr_writer는 좋네. 속성과 메서드가 거의 동일하게 작동하는게 신기하다. 일단 기록해두고 나중에 다시 참고. -> 스레드 세이프하게 싱글톤만드는거는 필수로 해야됨. $globalParam = “I’ll be always with you” # $param은 전역변수 @instanceParam = “I’ll stand with instance” #@instanceParam은 인스턴스 변수 루비 메서드에서 반환하는 값은 마지막으로 실행된 표현식의 결과값이다. (19페이지) 즉 아래의 메서드를 [ruby] def say(name) sayWhat = "How you doing? #{name} return sayWhat end [/ruby] 이렇게 고칠 수 있다는 말임 [ruby] def say(name) "How you doing? #{name} end [/ruby] 클래스 정의하기 [ruby] class Song #reader 인스턴스 변수는 자동으로 만들어짐 attr_reader :name, :artist, :duration #writer attr_writer :duration def duration_in_minutes @duratino/60.0 #부동소수점으로 설정 end def initialize(name, artist, duration) @name = name @artist = artist @duration = duration end end [/ruby] 상속 [ruby] class KaraokeSong < Song def initialize(name, artist, duration, lyrics) super(name, artist, duration) @lyrics = lyrics end end [/ruby] 클래스 변수 – 그 클래스의 모든 객체가 공유하며 클래스메서드를 통해 접근 할 수 있다. – 클래스 변수는 @@value 처럼 표시 – 클래스 변수는 사용하기전에 반드시 초기화 해야한다. – 클래스 메서드를 선언할 때는 ClassName.methodName 같은 식으로 선언 [ruby] class TestClass def method print "this is instance method" end def TestClass.class_method print "this is class method" end end [/ruby] 싱글톤 클래스 만들기 – 객체를 생성할 수 있는 메서드를 private로 만들고 getInstance메서드로만 생성할 수 있게 한다. -> 그런데 아래와 같이 만드는거는 스레드세이프 하지 않다. -> 스레드 세이프 하게 만들려면 singleton 믹스인을 사용해야됨. – (음..이건 나중에..) [ruby] class TestSingleton private_class_method :new @@classVariable = nil def TestSingleton.getInstance @@classVariable = new unless @@classVariable @@classVariable end end [/ruby] 접근 제어 – public, protected, private – 약간 특이하게 메서드 앞에 붙여주는 방식은 아니고 키워드를 지정하면 그 아래 있는거는 메서드에 다 적용하는 방식과 – 접근제어 키워드 뒤에 메서드를 :{메서드1}, :{메서드2} 이렇게 붙여주는 방식 2가지가 있다. [ruby] class AccessControl //기본값은 public def method1 end protected //여기부터는 protected def method2 end private //여기부터는 private def method3 end [/ruby] [ruby] class AccessControl public :method1, :method2 protected :method3 private :method4 end [/ruby]
카테고리: 프로그래밍
프로그래밍 관련