태그 : ruby

Ruby에서 //와 Regexp.compile 로 생성된 패턴의 source 차이 발견

패턴 내에 이스케이프 문자가 있는 경우

"</xxxxxx>"

를 패턴화 할 때

/<\/xxxxxx>/.source.length
=> 10

Regexp.compile("</xxxxxx>").source.length
=> 9

결과를 보면

/<\/xxxxxx>/.source
=> <\\/xxxxxx>

Regexp.compile("</xxxxxx>").source
=> </xxxxxx>

패턴의 source 스트링을 참조하는 경우 주의해야 할 듯!

by Augie | 2008/04/03 10:56 | 트랙백

Manifest Reader in Ruby

http://www.lunar-ocean.com/blog/manifest-reader-in-ruby/

OSGi 번들 jar 파일의 manifest 를 읽기 위한 ruby 구현에 대한 정보


by Augie | 2008/02/26 09:33 | 트랙백

Ruby의 class가 대문자로 시작하는 이유

Ruby의 class가 대문자로 시작하는 이유

ruby에서 대문자로 시작하는 오브젝트는 constant 이다.
class정의도 유일무이하므로 constant라고 볼 수 있다.
그래서 ruby의 class 는 대문자로 선언한다.


initialize

루비 클래스에서의 생성자.
루비에서 클래스의 객체 생성시에 "initialize" 이름의 메소드를 찾는다.
현재 클래스 정의에 없으면 클래스 상속트리를 거슬러 올라 처음 만나는 "initialize" 메소드를 호출하고 탐색을 멈춘다.


맴버변수

@로 시작한다.
별도로 선언하지 않아도 initialize 메소드 내부에서 최초로 사용될 수 있다.



by Augie | 2008/02/25 22:55 | 트랙백

읽어볼 루비 기사

How I learned Ruby

Test Driven Learning

from R4J

by Augie | 2008/02/20 23:03 | 트랙백

lambda와 Proc

class Array
    def matches_block( &some_block )
        find_all( &some_block)
    end
    def matches_proc( some_proc )
        find_all( some_proc )
    end
end

irb(main):009:0> digits = (0..9).to_a
=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
irb(main):010:0> digits.matches_block { |x| x > 5 }
=> [6, 7, 8, 9]
irb(main):011:0> digits.matches_proc( lambda { |x| x > 5 } )
=> [6, 7, 8, 9]

메소드가 block을 받게 하려면 '&'를 붙여 주어야 한다.
lambda 메소드에 block을 넘긴 결과가 Proc 이 된다.
전체 결과를 보면 block을 넘기느냐 Proc을 넘기느냐의 차이뿐 결과는 같다.


by Augie | 2008/02/01 14:05 | 트랙백

Ruby 에서 시스템 command 호출 방법

``     // backticks
    리턴값 : 호출 결과

system("command")
    리턴값 : true / false

   


by Augie | 2008/01/29 13:38 | 트랙백

here doc

기본

string = <<END_OF_STRING
You can enter as many
lines as you like here
but will end this string
with...
END_OF_STRING


Dynamic String

planet = 'Earth'
string = <<END_OF_STRING
Hello, #{planet}
END_OF_STRING

puts string # Hello, Earth


Indenting
: Tag가 Indent를 가진 경우 루비는 기본적으로 인식하지 못하고 에러난다.
  Tag에 '-'를 붙여주면 된다.
  Indent 깊이에 관계없이 '-' 는 하나만 붙인다.

string = <<-END_OF_STRING
    Notice the hyphen after the
    << characters above
    END_OF_STRING

by Augie | 2008/01/23 11:37 | 트랙백

irb 에 자동완성 기능 사용

require 'irb/completion'


홈디렉토리에

.irbrc 파일 작성

require 'irb/completion' 구문을 추가


irb 에서 ri 호출하기

.irbrc 파일에

def ri(*names)
  system(%{ri.bat #{names.map {|name| name.to_s}.join(" ")}})
end

구문 추가



forum 기사

by Augie | 2008/01/22 17:39 | 트랙백

irb 에서 rubygem 을 require 하려면???

먼저

require 'rubygem'

을 하고

require 'rake'
....

해야 함!!!

by Augie | 2008/01/21 17:09 | 트랙백

◀ 이전 페이지          다음 페이지 ▶