落書きノート

ふと自分が気になった事を書いてます

Ruby資格技術者認定問題集

ブロック,Proc,lambda,yield,eachメソッド等々です。

irb(main):001:0> def func x
irb(main):002:1> x + yield
irb(main):003:1> end
=> :func
irb(main):004:0> p func(1){2}
3
=> 3
irb(main):005:0> def func y
irb(main):006:1> y + yield
irb(main):007:1> end
=> :func
irb(main):008:0> func(1) do
irb(main):009:1* x = 2
irb(main):010:1> end
=> 3
irb(main):011:0> p x
NameError: undefined local variable or method `x' for main:Object
  from (irb):11
  from /Users/hirotoshi/.rbenv/versions/2.2.2/bin/irb:11:in `<main>'
irb(main):012:0> def func y
irb(main):013:1> y + yield
irb(main):014:1> end
=> :func
irb(main):015:0> x = 2
=> 2
irb(main):016:0> p func(1){x+=2}
5
=> 5
irb(main):017:0> p x
4
=> 4
irb(main):018:0> def func a, b
irb(main):019:1> a + yield(b,3)
irb(main):020:1> end
=> :func
irb(main):021:0> p func(1,2){|x,y| x + y}
6
=> 6
irb(main):022:0> def func
irb(main):023:1> return 1 if block_given?
irb(main):024:1> 
irb(main):025:1* 2
irb(main):026:1> end
=> :func
irb(main):031:0> p func(){}
1
=> 1
irb(main):032:0> p func
2
=> 2
irb(main):033:0> proc = Proc.new{|x| p x}
=> #<Proc:0x007f9f2400d488@(irb):33>
irb(main):034:0> proc.call(1)
1
=> 1
irb(main):035:0> def get_counter start
irb(main):036:1> Proc.new{|up| start += up}
irb(main):037:1> end
=> :get_counter
irb(main):038:0> count_up = get_counter(1)
=> #<Proc:0x007f9f2321e868@(irb):36>
irb(main):039:0> count_up.call(1)
=> 2
irb(main):040:0> count_up.call(3)
=> 5
irb(main):041:0> def func x
irb(main):042:1> x + yield
irb(main):043:1> end
=> :func
irb(main):044:0> proc = Proc.new{2}
=> #<Proc:0x007f9f231f47e8@(irb):44>
irb(main):045:0> func(1, &proc)
=> 3
irb(main):046:0> def func x, &proc
irb(main):047:1> x + proc.call
irb(main):048:1> end
=> :func
irb(main):049:0> func(1) do
irb(main):050:1* 2
irb(main):051:1> end
=> 3
irb(main):052:0> lmd = lambda{|x| p x}
=> #<Proc:0x007f9f23198e98@(irb):52 (lambda)>
irb(main):053:0> lmd = ->(x){ p x }
=> #<Proc:0x007f9f23189f38@(irb):53 (lambda)>
irb(main):054:0> lmd.call(1)
1
=> 1
irb(main):055:0> def func
irb(main):056:1> proc = Proc.new{ return 1 }
irb(main):057:1> proc.call
irb(main):058:1> 2
irb(main):059:1> end
=> :func
irb(main):060:0> p func
1
=> 1
irb(main):061:0> def func
irb(main):062:1> proc = lambda{return 1}
irb(main):063:1> proc.call
irb(main):064:1> 2
irb(main):065:1> end
=> :func
irb(main):066:0> p func
2
=> 2
irb(main):067:0> p1 = Proc.new{|x,y| y}
=> #<Proc:0x007f9f23123b20@(irb):67>
irb(main):068:0> p p1.call(1)
nil
=> nil
irb(main):069:0> p1 = lambda{|x,y| y}
=> #<Proc:0x007f9f23110700@(irb):69 (lambda)>
irb(main):070:0> p p1.call(1)
ArgumentError: wrong number of arguments (1 for 2)
  from (irb):69:in `block in irb_binding'
    from (irb):70:in `call'
  from (irb):70
  from /Users/hirotoshi/.rbenv/versions/2.2.2/bin/irb:11:in `<main>'
irb(main):073:0> p1 = ->(x,y){ p x + y }
=> #<Proc:0x007f9f23077e88@(irb):73 (lambda)>
irb(main):075:0> p p1.call(1,2)
3
3
=> 3
irb(main):076:0> p1 = ->(x,y){ p x + y }
=> #<Proc:0x007f9f23046db0@(irb):76 (lambda)>
irb(main):079:0> p1.call(1,2)
3
=> 3
irb(main):080:0> [1,2,3].each do |value|
irb(main):081:1* p value
irb(main):082:1> end
1
2
3
=> [1, 2, 3]
irb(main):083:0> [3,4,5].each_with_index do |value, index|
irb(main):084:1* p value + index
irb(main):085:1> end
3
5
7
=> [3, 4, 5]
irb(main):086:0> {:a => 1, :b => 2}.each do |key, value|
irb(main):087:1* p "#{key}:#{value}"
irb(main):088:1> end
"a:1"
"b:2"
=> {:a=>1, :b=>2}
irb(main):092:0> {:a => 1, :b => 2}.each_key do |key|
irb(main):093:1* p "#{key}"
irb(main):094:1> end
"a"
"b"
=> {:a=>1, :b=>2}
irb(main):095:0> {:a => 1, :b => 2}.each_value do |value|
irb(main):096:1* p "#{value}"
irb(main):097:1> end
"1"
"2"
=> {:a=>1, :b=>2}
irb(main):098:0> ("a".."z").each do |value|
irb(main):099:1* p value
irb(main):100:1> end
"a"
"b"
"c"
"d"
"e"
"f"
"g"
"h"
"i"
"j"
"k"
"l"
"m"
"n"
"o"
"p"
"q"
"r"
"s"
"t"
"u"
"v"
"w"
"x"
"y"
"z"
=> "a".."z"
irb(main):101:0> 2.upto(4) do |i|
irb(main):102:1* p i
irb(main):103:1> end
2
3
4
=> 2
irb(main):104:0> 5.downto(1) do |i|
irb(main):105:1* p i
irb(main):106:1> end
5
4
3
2
1
=> 5
irb(main):107:0> 4.times do |i|
irb(main):108:1* p i
irb(main):109:1> end
0
1
2
3
=> 4

次回はRubyにおけるスレッドの予定です。