Proc만들기
 
irb(main):002:0> temp_proc = Proc.new{}
=> #<Proc:0x8520248@(irb):2>
irb(main):003:0> temp_proc.call
=> nil
irb(main):004:0> temp_proc = Proc.new{ puts 'inside a block' }
=> #<Proc:0x84f3110@(irb):4>
irb(main):005:0> temp_proc.call
inside a block
=> nil

lambda만들기 ,lambda도 Proc이군
 
irb(main):006:0> temp_lambda = lambda { puts 'inside a block '}
=> #<Proc:0x84db4c4@(irb):6 (lambda)>
irb(main):007:0> temp_lambda.call
inside a block
=> nil
irb(main):008:0> temp_lambda.class
=> Proc


인자값을 가지도록 해보자
 
irb(main):012:0> temp_proc = Proc.new{ |a,b| puts "sum is #{a+b}" }
=> #<Proc:0x82db14c@(irb):12>
irb(main):013:0> temp_lambda = lambda {|a,b| puts "sum is #{a+b}"}
=> #<Proc:0x82c17cc@(irb):13 (lambda)>
irb(main):014:0> temp_proc.call(2,3)
sum is 5
=> nil
irb(main):015:0> temp_lambda.call(2,3)
sum is 5
=> nil

에러를 발생시켜 보자.
#Proc
irb(main):016:0> temp_proc.call(2)
TypeError: nil can't be coerced into Fixnum
        from (irb):12:in `+'
        from (irb):12:in `block in irb_binding'
        from (irb):16:in `call'
        from (irb):16
        from /usr/local/ruby/bin/irb:13:in `<main>'
irb(main):017:0> temp_proc.call(2,3,4)
sum is 5
=> nil

#lambda
irb(main):019:0> temp_lambda.call(2,3,5)
ArgumentError: wrong number of arguments (3 for 2)
        from (irb):19:in `call'
        from (irb):19
        from /usr/local/ruby/bin/irb:13:in `<main>'
irb(main):020:0> temp_lambda.call(5)
ArgumentError: wrong number of arguments (1 for 2)
        from (irb):20:in `call'
        from (irb):20
        from /usr/local/ruby/bin/irb:13:in `<main>'

리턴문을 사용해 보자

irb(main):021:0> def return_proc
irb(main):022:1>    temp_proc = Proc.new { return " It's from Proc" }
irb(main):023:1>    temp_proc.call
irb(main):024:1>    return "It's from return_proc"
irb(main):025:1> end
=> nil
irb(main):026:0> puts return_proc
 It's from Proc
=> nil
irb(main):027:0> def return_lambda
irb(main):028:1>    temp_lambda = lambda { return "It's from lambda" }
irb(main):029:1>    temp_lambda.call
irb(main):030:1>    return "It's from return_lambda"
irb(main):031:1> end
=> nil
irb(main):032:0> puts return_lambda
It's from return_lambda
=> nil


저작자 표시 비영리 동일 조건 변경 허락
신고

'JRuby/Ruby' 카테고리의 다른 글

Proc vs Lambda (1)  (0) 2011.11.22
HTTP를 써볼까?  (0) 2011.11.10
watir-webdriver  (0) 2011.11.10
OptionParser  (0) 2011.11.10
XPath를 테스트하다  (0) 2011.11.10
자바 시그너쳐 사용  (0) 2011.09.06
      JRuby/Ruby  |  2011.11.22 16:58




sync; echo 3 > /proc/sys/vm/drop_caches
 
저작자 표시 비영리 동일 조건 변경 허락
신고

'Linux/Unix' 카테고리의 다른 글

sync사용  (0) 2011.11.22
      Linux/Unix  |  2011.11.22 16:44




apt-get install mysql-server

gem install mysql 

-- 에러--
ruby extconf.rb install mysql
checking for mysql_query() in -lmysqlclient… no
checking for main() in -lm… yes
checking for mysql_query() in -lmysqlclient… no
checking for main() in -lz… yes
checking for mysql_query() in -lmysqlclient… no
checking for main() in -lsocket… no
checking for mysql_query() in -lmysqlclient… no
checking for main() in -lnsl… yes
checking for mysql_query() in -lmysqlclient… no
*** extconf.rb failed *** 

 apt-get install libmysqlclient5-dev (우분투 11.04버젼 기준) 아니면 apt-cache search로 찾아본다.


 -- test용 루비스크립트 --
require 'mysql'
mysql = Mysql.new('localhost', 'user','password','db', 3306)

puts mysql.get_host_info

참조 http://www.tmtm.org/en/mysql/ruby/

 
저작자 표시 비영리 동일 조건 변경 허락
신고

'우분투' 카테고리의 다른 글

MySQL of Interoperability in Ruby  (0) 2011.11.21
      Tag - mysql gem
      우분투  |  2011.11.21 15:32



clonekim's Blog is powered by Daum

 

티스토리 툴바