OS El Capitan(10.11.6)에서 Xcode 7.3.1를 설치한 후, ruby thrift 모듈이 컴파일이 실패했다.



$ sudo gem install thrift


compiling compact_protocol.c

compact_protocol.c:442:41: error: shifting a negative signed value is undefined [-Werror,-Wshift-negative-value]

    rb_exc_raise(get_protocol_exception(INT2FIX(-1), rb_str_new2(but)));

....



xcode 설치 디렉토리 정보는 다음과 같다. (뒷 부분에서 바꾼다).


$xcode-select -p

/Applications/Xcode.app/Contents/Developer




https://developer.apple.com/download/more/ 에 접속하고, 로그인 한 후, 

Command Line Tools OS X 10.11 for Xcode 7.2 설치 파일을 다운받고 설치한다.






OS X 10.11 for Xcode 7.2의 커맨드 라인의 위치를 변경한다.


$ sudo xcode-select -s /Library/Developer/CommandLineTools/



바뀐 xcode 커맨드 라인을 확인한다.


$ xcode-select -p

/Library/Developer/CommandLineTools




$ gem install thrift 를 실행한다. 


더 이상 shifting a negative signed value is undefined [-Werror,-Wshift-negative-value] 에러는 발생하지 않는다.

Posted by '김용환'
,



ruby에서 현재 디렉토리를 얻으려면, Dir 클래스를 사용하면 된다.

spec_path = Dir.pwd + "/spec"
puts spec_path


Posted by '김용환'
,

[ruby] enumeration의 inject

Ruby 2016. 8. 12. 21:58


inject 함수를 배웠다. inject() 의 내부 매개변수는 초기화 값이다. 

간단한게 배열에 대한 값을 쉽게 계산할 수 있다. 


$ irb



[3,6,9].inject() { | sum, element | sum += element  }

=> 18



[3,6,9].inject(10) { | sum, element | sum += element  }

=> 28










Posted by '김용환'
,

(OS X를 싹 밀지 않고 OS X 업그레이드를 하면 ruby 때문에 좀 힘들 수 있다. )


아래 내용의 후속판이다.

http://knight76.tistory.com/entry/mac-os-%EA%B0%9C%EB%B0%9C%EC%9E%90-%EC%9A%94%EC%84%B8%EB%AF%B8%ED%8B%B0%EC%97%90%EC%84%9C-%EC%97%98%EC%BA%90%ED%94%BC%ED%83%84-os-%EC%97%85%EA%B7%B8%EB%A0%88%EC%9D%B4%EB%93%9C%EC%8B%9C-%ED%8C%81tip


Gemfile에 'gem 'zookeeper', '1.4.11''가 추가되어 

$bundle 실행하면, 아래와 같이 You have to install development tools first. 에러가 발생했다.

링크에 있는데로 최신 Xcode를 깔아도 계속 발생했다.


checking for rb_thread_blocking_region()... *** 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:

--with-opt-dir

--without-opt-dir

--with-opt-include

--without-opt-include=${opt-dir}/include

--with-opt-lib

--without-opt-lib=${opt-dir}/lib

--with-make-prog

--without-make-prog

--srcdir=.

--curdir

--ruby=/Users/Samuel/.rbenv/versions/2.1.0/bin/ruby

/Users/Samuel/.rbenv/versions/2.1.0/lib/ruby/2.1.0/mkmf.rb:456:in `try_do': The compiler failed to generate an executable file. (RuntimeError)

You have to install development tools first.


gcc 확인했고..

 $ gcc -v

Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1

Apple LLVM version 7.0.2 (clang-700.1.81)

Target: x86_64-apple-darwin15.0.0

Thread model: posix


XCode 최신 확인..

$ brew config

..

Xcode: 7.2

..



~/.rbenv/versions/2.1.0/gemsets/story-deploy/gems/zookeeper-1.4.11/ext/mkmf.log 를 참조하니, gcc 4.2를 사용하고 있다.

 $ cat /Users/Samuel/.rbenv//versions/2.1.0/gemsets/story-deploy/gems/zookeeper-1.4.11/ext/mkmf.log

"gcc-4.2 -o conftest -I/Users/Samuel/.rbenv/versions/2.1.0/include/ruby-2.1.0/x86_64-darwin13.0 -I/Users/Samuel/.rbenv/versions/2.1.0/include/ruby-2.1.0/ruby/backward -I/Users/Samuel/.rbenv/versions/2.1.0/include/ruby-2.1.0 -I.   -I/Users/Samuel/.rbenv/versions/2.1.0/gemsets/story-deploy/gems/zookeeper-1.4.11/ext/include  -L/Users/Samuel/.rbenv/versions/2.1.0/gemsets/story-deploy/gems/zookeeper-1.4.11/ext/lib -L/Users/Samuel/.rbenv/versions/2.1.0/lib  -O3 -Wno-error=shorten-64-to-32  -pipe conftest.c  -L/Users/Samuel/.rbenv/versions/2.1.0/gemsets/story-deploy/gems/zookeeper-1.4.11/ext/lib  -L/Users/Samuel/.rbenv/versions/2.1.0/gemsets/story-deploy/gems/zookeeper-1.4.11/ext/lib -L/Users/Samuel/.rbenv/versions/2.1.0/lib -L. -L/Users/Samuel/.rbenv/versions/2.1.0/lib  -fstack-protector     -lruby-static -framework CoreFoundation  -lpthread -ldl -lobjc  -lzookeeper_st_gem -lm"

checked program was:

/* begin */

1: #include "ruby.h"

2:

3: int main(int argc, char **argv)

4: {

5:   return 0;

6: }

/* end */


gcc4.2를 지운 상태라, 당연히 에레가 발생했다.

$ gcc-4.2
-bash: gcc-4.2: command not found




그래서 아래와 같이 gcc42와 libv8을 설치하니 잘 된다.


 $ gem install libv8 -v 3.16.14.7 -- --with-system-v8

Fetching: libv8-3.16.14.7-x86_64-darwin-13.gem (100%)

Successfully installed libv8-3.16.14.7-x86_64-darwin-13

Parsing documentation for libv8-3.16.14.7-x86_64-darwin-13

Installing ri documentation for libv8-3.16.14.7-x86_64-darwin-13

Done installing documentation for libv8 after 0 seconds

1 gem installed


 $ brew install homebrew/dupes/apple-gcc42


$ bundle update

(ok)




정확한 문제의 원인은 잘 모르겠지만, ruby는 gcc-4.2가 반드시 필요하다

운영체제 업그레이드할 때는 일부 라이브러리 컴파일시 gcc-4.2가 안좋을 수 있지만, 

bundle update시 ruby rake가 gcc-4.2를 참조하여 native 컴파일을 하도록 되어 있어서, 상황에 맞게 작업해야 할 듯..



참조

https://gist.github.com/jpodlech/837c7e5103c4cf5cb9b1

https://github.com/cowboyd/therubyracer/issues/304


Posted by '김용환'
,


bundle update 하다가 다음 에러를 만났다. 


$ bundle update 


Errno::EACCES: Permission denied @ rb_sysopen - /Users/Google/.rbenv/versions/2.1.0/gemsets/story-deploy/gems/zookeeper-1.4.11/.ctags_paths


아마도 .rbenv 디렉토리에 permission이슈가 있었던 것 같다. chown 실행하니 더이상 Permission 에러가 나지 않는다.


$ sudo chown -R Google ~/.rbenv



Posted by '김용환'
,




맥에서 eventmachine 설치시 open ssl을 잘 읽지 못해서 에러가 발생할 수 있다. 



$ gem install eventmachine -v '1.0.8'



compiling binder.cpp

In file included from binder.cpp:20:

./project.h:116:10: fatal error: 'openssl/ssl.h' file not found

#include <openssl/ssl.h>

         ^

1 error generated.

make: *** [binder.o] Error 1


make failed, exit code 2



에러 발생하면, 


$ brew install openssl

$ xcode-select --install


그래도 안되면.. 강제로 link 한다. 

$ brew link openssl --force


그리고, 다시 설치하면 잘 동작한다. 

$ gem install eventmachine -v '1.0.8
'Building native extensions.  This could take a while...
Successfully installed eventmachine-1.0.8
Parsing documentation for eventmachine-1.0.8
Installing ri documentation for eventmachine-1.0.8
Done installing documentation for eventmachine after 5 seconds
1 gem installed




Posted by '김용환'
,



ruby의 ffi 모듈을 설치하다가 컴파일 에러가 발생했다. 


$ sudo gem install ffi -v '1.9.10'

 

/Users/Samuel/.rbenv/versions/2.1.0/lib/ruby/2.1.0/mkmf.rb:456:in `try_do': The compiler failed to generate an executable file. (RuntimeError)

You have to install development tools first.

from /Users/Samuel/.rbenv/versions/2.1.0/lib/ruby/2.1.0/mkmf.rb:587:in `try_cpp'

from /Users/Samuel/.rbenv/versions/2.1.0/lib/ruby/2.1.0/mkmf.rb:1067:in `block in have_header'

from /Users/Samuel/.rbenv/versions/2.1.0/lib/ruby/2.1.0/mkmf.rb:918:in `block in checking_for'

from /Users/Samuel/.rbenv/versions/2.1.0/lib/ruby/2.1.0/mkmf.rb:351:in `block (2 levels) in postpone'

from /Users/Samuel/.rbenv/versions/2.1.0/lib/ruby/2.1.0/mkmf.rb:321:in `open'

from /Users/Samuel/.rbenv/versions/2.1.0/lib/ruby/2.1.0/mkmf.rb:351:in `block in postpone'

from /Users/Samuel/.rbenv/versions/2.1.0/lib/ruby/2.1.0/mkmf.rb:321:in `open'

from /Users/Samuel/.rbenv/versions/2.1.0/lib/ruby/2.1.0/mkmf.rb:347:in `postpone'

from /Users/Samuel/.rbenv/versions/2.1.0/lib/ruby/2.1.0/mkmf.rb:917:in `checking_for'

from /Users/Samuel/.rbenv/versions/2.1.0/lib/ruby/2.1.0/mkmf.rb:1066:in `have_header'

from extconf.rb:16:in `<main>'


extconf failed, exit code 1



xcode 개발자 툴 설치는 간단히 xcode-select 커맨드로 해결할 수 있다. 


$ xcode-select --install

 // xcode 개발툴 설치




$ sudo gem install ffi -v '1.9.10'

Building native extensions.  This could take a while...

Successfully installed ffi-1.9.10

Parsing documentation for ffi-1.9.10

Installing ri documentation for ffi-1.9.10

Done installing documentation for ffi after 0 seconds

1 gem installed



Posted by '김용환'
,

[ruby] csv 처리

Ruby 2015. 12. 10. 20:11



ruby에서 mysql 데이터를 처리할 수 있는데,,

간단하게 mysql workbench를 통해서 csv로 다운받고, 이를 처리할 수 있다.



require 'csv'

CSV.read("test.csv")


내용 출력..



각 필드별로 제어(manipulation)하려면 CSV.foreach()문을 사용할 수 있다. 


CSV.foreach('test.csv') do |row| ; puts row.inspect ; end

 내용 출력..



필드 하나는 배열로 접근할 수 있다. row[0] 로 쓸 수 있으며, 이를 parsing하는 방식으로 테스트할 수 있다. 


 CSV.foreach('test.csv', { :col_sep => ',' }) { |row| print row[0] + ";" + row[1]; #logic#  }




Posted by '김용환'
,

[ruby] rescue

Ruby 2015. 12. 10. 18:10


eval()은 문자열을 수식또는 리터럴로 변환해주는데, exception이 발생할 수 있다.

자바의 try - catch 문과 동일하게 ruby는 rescue라는 문을 제공한다.

특이한 것은 모든 error와 exception을 rescue가 처리하지 않는다. 


1+1 이라는 문자열을 eval()로 연산하는 코드이다. 


1+1d 문자열이기 때문에 eval()시 rescue에서 syntax error 에러가 발생하는 것을 확인할 수 있다. 


=> nil

>> a="1+1" ; begin ; puts eval(a) ; rescue => se ; puts "rescued!!" ; end ; puts "GOGO"

2

GOGO


=> nil

>> a="1+1d" ; begin ; puts eval(a) ; rescue => se ; puts "rescued!!" ; end ; puts "GOGO"

SyntaxError: (eval):1: syntax error, unexpected tIDENTIFIER, expecting end-of-input

from (irb):86:in `eval'

from (irb):86

from /Users/Samuel/.rbenv/versions/2.1.0/bin/irb:11:in `<main>'



이를 해결하기 위해 Exception을 명시하니까 잘 동작했다. 


>> a="1+1d" ; begin ; puts eval(a) ; rescue Exception => se ; puts "rescued!!" ; end ; puts "GOGO"

rescued!!

GOGO

=> nil

>> a="1+1" ; begin ; puts eval(a) ; rescue Exception => se ; puts "rescued!!" ; end ; puts "GOGO"

2

GOGO

=> nil



참고로 StandardError 에러에 대해서는 rescue 처리를 하지 못한다.


>> a="1+1d" ; begin ; puts eval(a) ; rescue StandardError => se ; puts "rescued!!" ; end ; puts "GOGO"

SyntaxError: (eval):1: syntax error, unexpected tIDENTIFIER, expecting end-of-input

from (irb):90:in `eval'

from (irb):90

from /Users/Samuel/.rbenv/versions/2.1.0/bin/irb:11:in `<main>'


Posted by '김용환'
,


문자열을 특어 단어를 기준으로 나누는 작업을 하고 싶다면, partition과 split를 사용한다.

앞부분은 first, 뒤부분은 last로 얻거나 배열 원소로 값을 얻을 수 있다. 


>> "a=b,c=d".partition(",").first

=> "a=b"

>> "a=b,c=d".partition(",").last

=> "c=d"

>> "a=b,c=d".split(",").last

=> "c=d"

>> "a=b,c=d".split(",").first

=> "a=b"


>> "a=b,c=d".partition(",")[0]

=> "a=b"

>> "a=b,c=d".partition(",")[1]

=> ","

>> "a=b,c=d".split(",")[0]

=> "a=b"

>> "a=b,c=d".split(",")[1]

=> "c=d"



이 둘의 차이는 다음과 같다.


>> "a=b,c=d".split(",")

=> ["a=b", "c=d"]

>> "a=b,c=d".partition(",")

=> ["a=b", ",", "c=d"]


'Ruby' 카테고리의 다른 글

[ruby] csv 처리  (0) 2015.12.10
[ruby] rescue  (0) 2015.12.10
[ruby] 문자열에서 라인 또는 탭 삭제  (0) 2015.12.10
[ruby] unicode code 문자열을 eval로 한글로 변환하기  (0) 2015.12.10
[ruby] 역슬래쉬  (0) 2015.12.10
Posted by '김용환'
,