http://www.mongodb.org/display/DOCS/Ruby+Language+Center
를 보고 몽고DB를 연계하려고 작업중, 아래와 같은 오류가 나버렸다.
주로 윈도우에서나타나는 현상인데....

잘 읽어보면 development-kit 를 사용하라는 말이다.
http://github.com/oneclick/rubyinstaller/wiki/development-kit

C:\>gem update --system
Latest version currently installed. Aborting.

C:\>gem install bson_ext
Building native extensions.  This could take a while...
ERROR:  Error installing bson_ext:
        ERROR: Failed to build gem native extension.

        C:/Ruby191/bin/ruby.exe extconf.rb
checking for asprintf()... *** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers.  Check the mkmf.log file for more
details.  You may need configuration options.

Provided configuration options:


첨엔 뭔소린가 해서 다운로드 받고 생쇼했는데....
압축을 풀고나면 이런 파일이 있는데...msys.bat를 실행하면 유닉스 쉘같은 환경이 뜬다.



커멘드창에서 실패했던 "gem install bson_ext" 명령어를 여기서 치면 OK





몇 가지 참고하자면 골뱅이 붙은 변수를 멤버변수로 생각하면 된다.
@fname 이 멤버변수...
class관련 일부 문법은 개인적인 느낌이지만 자바스크립트랑 VB랑 뭔가 짬뽕된 느낌도 든다.

1. 생성자
자바는 class명과 맞춰주지만, initialize 가 생성자다.
class Test
    def
initialize(fname, lname)
        @fname = fname
        @lname = lname
    end
end

2. Getter, Setter
여러가지 스타일이 존재한다. (참고 http://juixe.com/techknow/index.php/2007/01/22/ruby-class-tutorial/ )
하지만 가장 손쉽게 쓴다면 attr_accessor 를 쓰는것이 가장 손이 덜 탄다.
class Test
   attr_accessor :name, :age
end

## 테스트코드 ##
test1 = Test.new
test1.name = "gildong"
test1.name = 17


3. 접근제한자 (private, protected, public)
c++ 스타일에 가깝다.
접근제한자를 쓴 이후의 메소드는 해당 접근제한자로 인식된다, (디폴트는 public)

class Sample
    public
    def getName()
        return @name
    end
    def getAge()
        return @age
    end

    private
    def ageCheck(age)
         return  age>=0 && age<200
    end
end





뭔가 잘안되었는데.. 뭔가 이거저거 하고
다시 컴파일하니까 잘되는 느낌이다. ㄱ-)
뭔가 라이브러리 설치도 들어가서 그런지 root권한이 있어야 되는 느낌이다.

이론상으로는 
  > 1단계: ruby를 받아서 컴파일하고,
  > 2단계: rails 설치 (gem install rails)
  > 3단계: 샘플실행 (rails new sample      ,   cd sample  rails server)
이지만, 이게 말처럼 되지 않는다.





1. ruby 컴파일하기
wget http://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.3-p0.tar.gz
tar xzvf ruby-1.9.3-p0.tar.gz
cd ruby-1.9.3-p0
 ./configure --prefix=/usr/local --enable-shared --disable-install-doc --with-opt-dir=/usr/local/lib
make 
make install
https://github.com/sstephenson/ruby-build/issues/119
http://www.ruby-forum.com/topic/161926
http://serverfault.com/questions/196313/how-to-setup-ruby-and-rubygems-on-centos
http://aaronvb.com/articles/38-setting-up-an-ubuntu-server-with-ruby-1-9-2-and-rails-3-1
http://allenjeon.tistory.com/352



2. centos에서 컴파일이 안된다.
cp Makefile Makefile.bak
sed -e "s/-fvisibility=hidden/-fvisibility=default/" Makefile.bak > Makefile
make clean
make

http://permalink.gmane.org/gmane.comp.lang.ruby.bio/450




3. rails server에서 실행시 ExecJS::RuntimeUnavailable오류
$ rails new sample
$ cd sample
$ rails server

/usr/local/lib/ruby/gems/1.9.1/gems/execjs-1.3.0/lib/execjs/runtimes.rb:50:in `autodetect': Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes. (ExecJS::RuntimeUnavailable)
        from /usr/local/lib/ruby/gems/1.9.1/gems/execjs-1.3.0/lib/execjs.rb:5:in `<module:ExecJS>'
        from /usr/local/lib/ruby/gems/1.9.1/gems/execjs-1.3.0/lib/execjs.rb:4:in `<top (required)>'
        from /usr/local/lib/ruby/gems/1.9.1/gems/coffee-script-2.2.0/lib/coffee_script.rb:1:in `require'
        from /usr/local/lib/ruby/gems/1.9.1/gems/coffee-script-2.2.0/lib/coffee_script.rb:1:in `<top (required)>'
        from /usr/local/lib/ruby/gems/1.9.1/gems/coffee-script-2.2.0/lib/coffee-script.rb:1:in `require'
        from /usr/local/lib/ruby/gems/1.9.1/gems/coffee-script-2.2.0/lib/coffee-script.rb:1:in `<top (required)>'
        from /usr/local/lib/ruby/gems/1.9.1/gems/coffee-rails-3.2.2/lib/coffee-rails.rb:1:in `require'
        from /usr/local/lib/ruby/gems/1.9.1/gems/coffee-rails-3.2.2/lib/coffee-rails.rb:1:in `<top (required)>'
        from /usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.22/lib/bundler/runtime.rb:68:in `require'
        from /usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.22/lib/bundler/runtime.rb:68:in `block (2 levels) in require'
        from /usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.22/lib/bundler/runtime.rb:66:in `each'
        from /usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.22/lib/bundler/runtime.rb:66:in `block in require'
        from /usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.22/lib/bundler/runtime.rb:55:in `each'
        from /usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.22/lib/bundler/runtime.rb:55:in `require'
        from /usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.22/lib/bundler.rb:122:in `require'
        from /home1/user1/tost/Code/sample/config/application.rb:7:in `<top (required)>'
        from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.2.1/lib/rails/commands.rb:53:in `require'
        from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.2.1/lib/rails/commands.rb:53:in `block in <top (required)>'
        from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.2.1/lib/rails/commands.rb:50:in `tap'
        from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.2.1/lib/rails/commands.rb:50:in `<top (required)>'
        from script/rails:6:in `require'
        from script/rails:6:in `<main>'

[해결법]
$ vi Gemfile
...
gem 'execjs'
gem
'therubyracer'
...

$ bundle install
Using rake (0.9.2.2)
Using i18n (0.6.0)
Using multi_json (1.1.0)
Using activesupport (3.2.1)
Using builder (3.0.0)
...





4. ruby on rails 실행시 : Address bind오류

[메시지]
$ rails server
/usr/local/lib/ruby/gems/1.9.1/gems/railties-3.2.1/lib/rails/script_rails_loader.rb:11: warning: Insecure world writable dir /usr/local/apache-maven-2.2.1/bin in PATH, mode 047777
/usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.22/lib/bundler/runtime.rb:136: warning: Insecure world writable dir /usr/local/apache-maven-2.2.1/bin in PATH, mode 047777
=> Booting WEBrick
=> Rails 3.2.1 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2012-02-23 14:43:25] INFO  WEBrick 1.3.1
[2012-02-23 14:43:25] INFO  ruby 1.9.3 (2011-10-30) [x86_64-linux]
[2012-02-23 14:43:25] WARN  TCPServer Error: Address already in use - bind(2)
Exiting
/usr/local/lib/ruby/1.9.1/webrick/utils.rb:85:in `initialize': Address already in use - bind(2) (Errno::EADDRINUSE)
        from /usr/local/lib/ruby/1.9.1/webrick/utils.rb:85:in `new'
        from /usr/local/lib/ruby/1.9.1/webrick/utils.rb:85:in `block in create_listeners'
        from /usr/local/lib/ruby/1.9.1/webrick/utils.rb:82:in `each'
        from /usr/local/lib/ruby/1.9.1/webrick/utils.rb:82:in `create_listeners'
        from /usr/local/lib/ruby/1.9.1/webrick/server.rb:82:in `listen'
        from /usr/local/lib/ruby/1.9.1/webrick/server.rb:70:in `initialize'
        from /usr/local/lib/ruby/1.9.1/webrick/httpserver.rb:45:in `initialize'
        from /usr/local/lib/ruby/gems/1.9.1/gems/rack-1.4.1/lib/rack/handler/webrick.rb:10:in `new'
        from /usr/local/lib/ruby/gems/1.9.1/gems/rack-1.4.1/lib/rack/handler/webrick.rb:10:in `run'
        from /usr/local/lib/ruby/gems/1.9.1/gems/rack-1.4.1/lib/rack/server.rb:265:in `start'
        from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.2.1/lib/rails/commands/server.rb:70:in `start'
        from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.2.1/lib/rails/commands.rb:55:in `block in <top (required)>'
        from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.2.1/lib/rails/commands.rb:50:in `tap'
        from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.2.1/lib/rails/commands.rb:50:in `<top (required)>'
        from script/rails:6:in `require'
        from script/rails:6:in `<main>'

[해결법]
서버를 종료할때 ctrl+c로 종료해야하는데,
ctrl+z로 나갔거나 그러면 프로세스가 살아있어서 그럴확률이 높다.
다음과 같이 프로세스를 확인해서 죽여버리자...

$ ps -ef | grep "rails"
root      1767  1429  0 14:36 pts/4    00:00:03 /usr/local/bin/ruby script/rails server
root      1945  1429  0 14:45 pts/4    00:00:00 grep rails

$ ps -9 1767
$ rails server
/usr/local/lib/ruby/gems/1.9.1/gems/railties-3.2.1/lib/rails/script_rails_loader.rb:11: warning: Insecure world writable dir /usr/local/apache-maven-2.2.1/bin in PATH, mode 047777
/usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.22/lib/bundler/runtime.rb:136: warning: Insecure world writable dir /usr/local/apache-maven-2.2.1/bin in PATH, mode 047777
=> Booting WEBrick
=> Rails 3.2.1 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2012-02-23 14:48:04] INFO  WEBrick 1.3.1
[2012-02-23 14:48:04] INFO  ruby 1.9.3 (2011-10-30) [x86_64-linux]
[2012-02-23 14:48:04] INFO  WEBrick::HTTPServer#start: pid=1989 port=3000






5. rails 설치시 yaml관련 오류가 난다면?
gem install rails 설치시 "yaml" 관련 오류가 날때가 있다.
아래와 같이 yaml을 받아서 컴파일해보자.
그래도 안된다면, 루비도 다시 컴파일해보자

wget http://pyyaml.org/download/libyaml/yaml-0.1.4.tar.gz
tar xzvf yaml-0.1.4.tar.gz
cd yaml-0.1.4
./configure --prefix=/usr/local
make 
make install


+ Recent posts