Ruby를 배우는 Java 개발자가 알아야 할 10가지 part 3

Java 개발자가 알아야 할 10가지

Item #4 믹스인(Mix-In)을 사용한 클래스 확장

루비는 인터페이스가 없지만 모듈로 정의된 믹스인이 있다.

모듈이란..

  • 네임스페이스이다. (클래스와 유사)
  • 메소드를 가질 수 있다. (클래스와 유사)
  • 객체화될 수 없다. (클래스와 다름)
  • 클래스에 포함될 수 있다.
  • - 모듈의 메소드는 클래스의 메소드가 된다.

믹스인 재사용

module LessComparable
  def >(other)
    other < self
  end
  # Other methods defined in terms of less than:
  #     <=, >=, ==
end
class Pair
  include LessComparable
  attr_accessor :first, :second
  # ...
  def <(other)
    (first < other.first) ||
      (first == other.first && second < other.second)
  end
end

Item #3 구문(closure) 감싸기

  • Iteration
  •   [1,2,3].each do |item| puts item end
    
  • Resource Management
  •   file_contents = open(file_name) { |f| f.read }
    
  • Callbacks
  •   widget.on_button_press { puts "Got Button Press" }
    

Item #2 ri 는 당신의 친구, irb 도 당신의 친구

  • ri
  • 루비 정보(Ruby Information). 표준 루비 객체에 대한 메뉴얼 페이지(Man pages)

    $ ri Array
    ---------------------------------------------------------- Module: Array
         Arrays are ordered, integer-indexed collections of any object.
         Array indexing starts at 0, as in C or Java. A negative index is
         assumed to be relative to the end of the array---that is, an index
         of -1 indicates the last element of the array, -2 is the next to
         last element in the array, and so on.
    ------------------------------------------------------------------------
    Includes:
    ---------
         Enumerable(all?, any?, collect, detect, each_with_index, entries,
         find, find_all, grep, include?, inject, map, max, member?, min,
         partition, reject, select, sort, sort_by, to_a, zip)
    Class methods:
    --------------
         [], new
    Instance methods:
    -----------------
         &, *, +, -, <<, <=>, ==, [], []=, assoc, at, clear, collect,
         collect!, compact, compact!, concat, delete, delete_at, delete_if,
         each, each_index, empty?, eql?, fetch, fill, first, flatten,
         flatten!, frozen?, hash, include?, index, indexes, indices, insert,
         inspect, join, last, length, map, map!, nitems, pack, pop, push,
         rassoc, reject, reject!, replace, reverse, reverse!, reverse_each,
         rindex, select, shift, slice, slice!, sort, sort!, to_a, to_ary,
         to_s, transpose, uniq, uniq!, unshift, values_at, zip, |
    
    $ ri Array#last
    ------------------------------------------------------------- Array#last
         array.last     =>  obj or nil
         array.last(n)  =>  an_array
    ------------------------------------------------------------------------
         Returns the last element(s) of _self_. If the array is empty, the
         first form returns +nil+.
    
            [ "w", "x", "y", "z" ].last   #=> "z"
    
  • irb
  • 루비 콘솔(Interactive Ruby). 콘솔 기반의 루비 실행기(Ruby interpreter)

    $ irb --simple-prompt
    >> 1 + 2
    => 3
    >> Proc.instance_methods(false)
    => ["[]", "==", "dup", "call", "binding", "to_s",
        "clone", "to_proc", "arity"]
    

Item #1 이제 길고 긴 코드 작성은 그만!

동료 개발자 왈...

난 문제 해결을 위해 루비를 사용해보기로 했다. 그리고 코드를 좀 작성했는데, 이런~ 그 순간 이미 문제가 다 해결됐음을 알게됐다.

예제 코드
* Copland (Hivemind의 Ruby 버젼) VS Needle Libraries
* Rake (Make의 Ruby 버젼)

This entry was posted in Ruby & Languages and tagged . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>