Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Ruby/2011년스터디/세미나

From ZeroWiki
Revision as of 08:01, 6 March 2011 by imported>rabierre

RubyLanguage 소개

RubyLanguage 소개와, 기초 문법을 설명하는 파트

  1. 루비란 무엇인가
    • 루비의 개발 배경
    • 루비의 특징
    • 루비의 역사
  1. 루비와의 첫 만남
  1. 루비 써보기
    • 계산기로서의 루비
    • Hello World!
    • 간단한 코드 따라하기

irb 에 버그이씀. tab누르지마셈 ㅜㅜ

  • Hello Ruby!
    • 루비는 진정한 객체지향 언어다?!
  *루비의 모든것은 객체로써 존재한다

123.method "문자열".method

  *함수조차 nilClass의 멤버함수
      • nilClass의 인스턴스는 nil하나, prototype은 없음. 다른 함수들은 가지고있음
      • 멤버변수도 객체, 객체안의 객체!
    • 루비의 신기한 배열. 음의 인덱스를 가질 수 있다


  • Ruby.new
  • 이제 객체를 만들어보자
    • 루비에서는 클래스명이 대문자로 시작해야한다!(아니면 에러)
    • 루비의 변수명 선언 규칙
    • initialize 함수로 객체 선언하기
        • 루비의 생성자 initialize. ( 디폴트 생성자가 있나봄)

class Some def initialize # init variables end end

    • 멤버변수 선언하기(@)

def initialize @var # this is the way how declaring variable end

    • 루비는 객체의 동적인 변경이 가능하다!
      • 이미 생성된 인스턴스에 멤버함수 추가하기

class Some def initialize # init variables end end

def Some.function # dynamic function declare end

    • 상속

class Some2 < Some def initialize # this is overriding end end

    • 함수의 오버라이딩
      • 부모 클래스를 동적 변경하면, 자식 클래스의 인스턴스는 어떻게 될까?

Some2.function2 # undefined method def Some.function2 # new function end Some.function2 # works!

      • 루비의 클래스/메소드 체이닝을 가르칠까요?
    • attr_reader
  • 메서드를 만들어보자! (루비는 return문이 없어도 돼)
    • 맨 마지막 연산식의 결과가 리턴됨
    • 메서드는 {} 대신 def/end로
    • 루비의 함수는 () 생략가능!
    • 문자열 반복출력 메소드 만들어보기
      • while
      • do/while
      • for
    • 블럭을 이용한 반복자 함수
      • 루비의 블럭 넘기기
yield : get the block and run it a = 1, 2, 3, 4, 5 a.each {|x| puts x+1} # bloack은 복사 전달인가?(maybe)
    • 기본 입출력
      • 입력받은 문자열 반복 출력해보기
  • 정규표현식(생략하는게 나을 듯)
  • 예외처리하기 (세번째 세미나에서 필요할까?)
    • if/else로 예외 처리해보자
    • rescue로 만들기
  • 유닛테스트(생략가능)
    • 상속을 통한 테스트 케이스 만들기
UnitTest::
  • 여러분은 이제 루비 유저입니다!
    • 자축
    • 호잇호이호!!

Ruby로 만들어보기

  • RRobots를 이용한 RubyLanguage Robocode
  • 간단하게 만드는 robot 코드
require 'robot'

class NervousDuck
   include Robot

  def tick events
    turn_radar 1 if time == 0
    turn_gun 30 if time < 3
    accelerate 1
    turn 2
    fire 3 unless events['robot_scanned'].empty? 
  end
end
  • Methods
battlefield_height battlefield의 높이
battlefield_width battlefield의 너비
energy 남은 energy (0 보다 적어지면 죽음)
gun_heading gun이 향한 방향(동쪽 : 0, 북쪽 : 90, 서쪽 : 180, 남쪽 : 270)
gun_heat your gun heat, if this is above 0 you can't shoot
heading robot이 향한 방향(동쪽 : 0, 북쪽 : 90, 서쪽 : 180, 남쪽 : 270)
size robot의 반지름(x <= size 면 왼쪽 벽에 닿는다)
radar_heading radar가 향한 방향(동쪽 : 0, 북쪽 : 90, 서쪽 : 180, 남쪽 : 270)
time 시작하고부터 흐른 시간
speed 속도 (-8 <= speed <= 8)
x x축 상에서의 위치(0 <= x <= battlefield_width)
y y축 상에서의 위치(0 <= y <= battlefield_height)
accelerate(param) 가속도(1 <= accelerate <= -1)
stop accelerates negativ if moving forward (and vice versa), may take 8 ticks to stop (and you have to call it every tick)
fire(power) gun에서 총알 발사(0.1 <= power <= 3)
turn(degrees) robot을 지정한 degrees만큼 돌린다(max 10 degrees per tick)
turn_gun(degrees) gun을 지정한 degrees만큼 돌린다(max 30 degrees per tick)
turn_radar(degrees) radar를 지정한 degrees만큼 돌린다(max 60 degrees per tick)
dead 죽으면 true를 반환한다
say(msg) robot 위에 msg를 띄운다
broadcast(msg) 모든 robot에게 msg를 broadcast로 전달한다

회고


Ruby/2011년스터디