64. x.times{|i|....}
C:ruby-processing>jirb -r irb/completion
irb(main):001:0> 5.times {|i| p i }
0
1
2
3
4
=> 5
irb(main):002:0>
Ruby Business Commons
2008 11 8 64
65.
def draw
translate width/2, height/2
5.times do |i|
i += 1
fill 30, i*20, 99
sz = 60 - i*10
rect 0, 0, sz, sz
end
end
Ruby Business Commons
2008 11 8 65
70.
def draw
translate width/2, height/2
rotate radians(45)
5.times do |i|
i += 1
fill 30, i*20, 99
sz = 60 - i*10
rect 0, 0, sz, sz
end
end
Ruby Business Commons
2008 11 8 70
71.
Ruby Business Commons
2008 11 8 71
72.
require 'ruby-processing'
class RotateRectangles < Processing::App
include Math
def radians(x)
return x*(PI/180)
end
Ruby Business Commons
2008 11 8 72
79. def draw def draw
translate width/2, height/2 translate width/2, height/2
ary = (0..11).map{|a| a*30}
ary.each do |r2|
push_matrix
rotate radians(45) rotate radians(r2)
translate 50, 0
h = random 100
5.times do |i| 5.times do |i|
i += 1 i += 1
fill 30, i*20, 99 fill h, i*20, 99, 80
sz = 60 - i*10 sz = 60 - i*10
rect 0, 0, sz, sz rect 0, 0, sz, sz
end end
pop_matrix
end
end end
Ruby Business Commons
2008 11 8 79
85.
def draw
translate width/2, height/2
def draw ary = (0..11).map{ |a| a *30 }
translate width/2, height/2 ary.each do |r1|
ary = (0..11).map{|a| a*30} push_matrix
rotate radians(r1)
translate 100, 0
h = random 100
Ruby Business Commons
2008 11 8 85
86. ary.each do|r2| ary.each do |r2|
push_matrix push_matrix
rotate radians(r2) rotate radians(r2)
translate 50, 0 translate 50, 0
h= random 100
5.times do |i| 5.times do |i|
i += 1 i += 1
fill h, i*20, 99, 80 fill h, i*20, 99, 80
sz = 60 - i*10 sz = 60 - i*10
rect 0, 0, sz, sz rect 0, 0, sz, sz
end end
pop_matrix pop_matrix
end
end
pop_matrix
end
end
end
Ruby Business Commons
2008 11 8 86
100. Position
require 'ruby-processing'
class Position
end
class Rectangle < Position
end class MovableDot...
class Dot
end
class MovableDot < Processing::App
Ruby Business Commons
2008 11 8 100
104. require 'ruby-processing'
class Position
attr_accessor :x, :y
end
class Rectangle < Position
end
Ruby Business Commons
2008 11 8 104
105. class Position
attr_accessor :x, :y
def initialize(x = 0, y = 0, &block)
move_to(x, y, &block)
end
end
2008 11 8 105
106. class Position
attr_accessor :x, :y
def initialize(x = 0, y = 0, &block)
move_to(x, y, &block)
end
def move_to(x, y)
@x = block_given? ? yield(x) : x
@y = block_given? ? yield(y) : y
end
end
2008 11 8 106
117. Symbol
def foo(t, x)
case x
when 1 then t.strftime(quot;%Y/%m/%dquot;)
when 2 then t.strftime(quot;%H:%M:%Squot;)
end
end
foo(Time.now, 1)
foo(Time.now, 2)
def foo(t, x)
case x
when :date then t.strftime(quot;%Y/%m/%dquot;)
when :time then t.strftime(quot;%H:%M:%Squot;)
end
end
foo(Time.now, :time)
foo(Time.now, :date)
Ruby Business Commons
2008 11 8 117
118. def initialize(x = 0, y = 0, &block)
move_to(x, y, &block)
end
Ruby Business Commons
2008 11 8 118
119. new --> initialize
irb(main):001:0> class Foo
irb(main):002:1> attr_accessor :x, :y
irb(main):003:1> def initialize(x = 0, y = 0)
irb(main):004:2> @x, @y = x, y
irb(main):005:2> end
irb(main):006:1> end
irb(main):007:0> foo = Foo.new
=> #<Foo:0x1c8b884 @y=0, @x=0>
irb(main):008:0> foo.x
=> 0
irb(main):009:0> foo.y
=> 0
irb(main):010:0> bar = Foo.new(1, 2)
=> #<Foo:0x1a9883d @y=2, @x=1>
irb(main):011:0> bar.x
=> 1
irb(main):012:0> bar.y
=> 2
2008 11 8 119
120. def move_to(x, y)
@x = block_given? ? yield(x) : x
@y = block_given? ? yield(y) : y
end
Ruby Business Commons
2008 11 8 120
124. yield
irb(main):062:0> block_1[1, 2, 3]
3
=> nil
irb(main):063:0> block_2[1, 2, 3]
ArgumentError: wrong number of arguments(3 for 2)
irb(main):064:0> block_3[1, 2, 3]
ArgumentError: wrong number of arguments(3 for 2)
2008 11 8 124
125. yield
irb(main):071:0> def multiple(a, b, &block)
irb(main):072:1> block[a, b]
irb(main):073:1> end
=> nil
irb(main):074:0> multiple(1, 2){|x, y| puts x + y}
3
=> nil
irb(main):075:0> def multiple(a, b)
irb(main):076:1> yield a, b
irb(main):077:1> end
=> nil
irb(main):078:0> multiple(1, 2){|x, y| puts x + y}
3
=> nil
2008 11 8 125
126. yield
irb(main):079:0> def multiple(a, b, &block)
irb(main):080:1> block[a, b, 3]
irb(main):081:1> end
=> nil
irb(main):082:0> multiple(1, 2){|x, y| puts x + y}
3
=> nil
irb(main):083:0> def multiple(a, b)
irb(main):084:1> yield a, b, 3
irb(main):085:1> end
=> nil
irb(main):086:0> multiple(1, 2){|x, y| puts x + y}
3
=> nil
2008 11 8 126
127. Rectangle
class Rectangle < Position
attr_accessor :width, :height
def initialize(x, y, width, height)
super(x, y)
@width, @height = width, height
end
Position
def to_a
[@x, @y, @width, @height]
end
end
Ruby Business Commons
2008 11 8 127
135. @app.rect(*@rect.to_a)
)
irb(main):001:0> def to_a
irb(main):002:1> [100, 200, 300, 400]
irb(main):003:1> end
irb(main):004:0> def foo(a, b=nil, c=nil, d=nil)
irb(main):005:1> p a
irb(main):006:1> p b
irb(main):007:1> p c
irb(main):008:1> p d
irb(main):009:1> end
irb(main):010:0> foo(to_a)
irb(main):011:0> foo(*to_a)
2008 11 8 135
136.
Ruby Business Commons
2008 11 8 136
137. class MovableDot < Processing::App
THE_NUM_OF_DOT = 20
def setup
@dots = []
dot_size, dot_margin = Dot::DEFAULT_SIZE, Dot::DEFAULT_MARGIN
THE_NUM_OF_DOT.times do |i|
initial_position = Position.new(width, height) do |wh_value|
i * (dot_size + dot_margin) + wh_value/2 - THE_NUM_OF_DOT *
((dot_size + dot_margin)/2)
end
@dots << Dot.new(initial_position)
end
end Ruby Business Commons
2008 11 8 137
138. def draw
background 0
@dots.each {|dot| dot.display}
end
end
MovableDot.new :title => quot;Movable Dotquot;, :width => 300, :height => 300
Ruby Business Commons
2008 11 8 138
142. irb>> foo, bar = 1, 2
=> [1, 2]
irb>> p foo
1
irb>> p bar
2
irb>> foo, bar = [5,6]
=> [5, 6]
irb>> p foo
5
irb>> p bar
6
Ruby Business Commons
2008 11 8 142
143.
def display
c1 = @app.color(255, 255, 255, 255)
@app.fill c1
@app.rect(*@rect.to_a)
end
def display
c1 = @app.color(*(0..3).map{rand(255)})
@app.fill c1
@app.rect(*@rect.to_a)
end
Ruby Business Commons
2008 11 8 143
162. require 'ruby-processing'
class LoopQueue
def initialize(*items) LoopQueue
@items = items Dot
@index = 0
end
def next
@index += 1
@index = 0 if @index == @items.length
current
end
def current
@items[@index]
end
end
2008 11 8 162
171. case
irb(main):002:0> def case_test(o)
irb(main):003:1> case o
irb(main):004:2> when String : quot;This is String.quot;
irb(main):005:2> when Fixnum : quot;This is Fixnum.quot;
irb(main):006:2> when Array : quot;This is Array.quot;
irb(main):007:2> end
irb(main):008:1> end
=> nil
irb(main):009:0> case_test(1) Attention!!
=> quot;This is Fixnum.quot; Ruby1.9
irb(main):010:0> case_test([1,2])
=> quot;This is Array.quot;
irb(main):011:0> case_test(quot;RBCquot;)
=> quot;This is String.quot;
irb(main):012:0>
Ruby Business Commons
2008 11 8 171