11月8日イケテルjruby勉強会@東京

2,243
-1

Published on

RubyProcessingの説明

Published in: Technology
1 Comment
2 Likes
Statistics
Notes
  • 参考コードはこちら
    http://code.google.com/p/rbc-incubator/source/browse/takeMe2theStarlet/studySettionData/trunk/rubyProcessing/
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total Views
2,243
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
20
Comments
1
Likes
2
Embeds 0
No embeds

No notes for slide

11月8日イケテルjruby勉強会@東京

  1. 1.           Ruby Business Commons 2008 11 8 1
  2. 2.           Ruby Business Commons 2008 11 8 2
  3. 3.          Ruby Business Commons 2008 11 8 3
  4. 4. JRuby http://java.sun.com/javase/ja/6/download.html Ruby Business Commons 2008 11 8 4
  5. 5. Mac dmg http://www.apple.com/jp/ftp-info/reference/ javaformacosx105update2.html Ruby Business Commons 2008 11 8 5
  6. 6. Windows Mac http://dist.codehaus.org/jruby/ Ruby Business Commons 2008 11 8 6
  7. 7.       Ruby Business Commons 2008 11 8 7
  8. 8.   2008 11 8 8
  9. 9. 2 2008 11 8 9
  10. 10. %JAVA_HOME%bin;%JRUBY_HOME%bin; 2008 11 8 10
  11. 11. JAVA_HOME, JRUBY_HOME, PATH Mac export JAVA_HOME=JDK export JRUBY_HOME=JRuby export PATH=$JRUBY_HOME/bin:$PATH Ruby Business Commons 2008 11 8 11
  12. 12.           Ruby Business Commons 2008 11 8 12
  13. 13. Ruby Business Commons 2008 11 8 13
  14. 14. RBC Ruby Business Commons 2008 11 8 14
  15. 15.     ruby-processing   Ruby Business Commons 2008 11 8 15
  16. 16. Ruby Business Commons 2008 11 8 16
  17. 17.     “ Ruby Business Commons 2008 11 8 17
  18. 18. Ruby Business Commons 2008 11 8 18
  19. 19. Ruby Business Commons 2008 11 8 19
  20. 20. Ruby Business Commons 2008 11 8 20
  21. 21. Ruby Business Commons 2008 11 8 21
  22. 22.         Ruby Business Commons 2008 11 8 22
  23. 23.          Ruby Business Commons 2008 11 8 23
  24. 24. java version quot;1.6.0quot; jruby 1.1.4 Ruby Business Commons 2008 11 8 24
  25. 25. JRuby http://java.sun.com/javase/ja/6/download.html Ruby Business Commons 2008 11 8 25
  26. 26. Mac dmg http://www.apple.com/jp/ftp-info/reference/ javaformacosx105update2.html Ruby Business Commons 2008 11 8 26
  27. 27. Windows Mac http://dist.codehaus.org/jruby/ Ruby Business Commons 2008 11 8 27
  28. 28.   2008 11 8 28
  29. 29. 2 2008 11 8 29
  30. 30. %JAVA_HOME%bin;%JRUBY_HOME%bin; 2008 11 8 30
  31. 31. JAVA_HOME, JRUBY_HOME, PATH Mac export JAVA_HOME=JDK export JRUBY_HOME=JRuby export PATH=$JRUBY_HOME/bin:$PATH Ruby Business Commons 2008 11 8 31
  32. 32.         Ruby Business Commons 2008 11 8 32
  33. 33.       Ruby Business Commons 2008 11 8 33
  34. 34.         Ruby Business Commons 2008 11 8 34
  35. 35.     Ruby Business Commons 2008 11 8 35
  36. 36.       Ruby Business Commons 2008 11 8 36
  37. 37.   http://www.haque.co.uk/    Ruby Business Commons 2008 11 8 37
  38. 38.         Ruby Business Commons 2008 11 8 38
  39. 39. rbc Ruby Business Commons 2008 11 8 39
  40. 40.       Ruby Business Commons 2008 11 8 40
  41. 41.                       Ruby Business Commons 2008 11 8 41
  42. 42.       Ruby Business Commons 2008 11 8 42
  43. 43.     Ruby Business Commons 2008 11 8 43
  44. 44. rbc Ruby Business Commons 2008 11 8 44
  45. 45. C:ruby-processing>jruby script/generate rbc/rotate_rectangles.rb 400 400     Ruby Business Commons 2008 11 8 45
  46. 46. rbc rotate_rectangles.rb           Ruby Business Commons 2008 11 8 46
  47. 47. Ruby Business Commons 2008 11 8 47
  48. 48. 1. require 'ruby-processing' class RotateRectangles < Processing::App def setup end def draw end end RotateRectangles.new :title => quot;Rotate Rectanglesquot;, :width => 400, :height => 400 Ruby Business Commons 2008 11 8 48
  49. 49. Windows generate Win Ruby Business Commons 2008 11 8 49
  50. 50. C:ruby-processing> jruby script/generate rbc/rotate_rectangles.rb 400 400   400x400 Ruby Business Commons 2008 11 8 50
  51. 51. generate generate rotate_rectangles.rb   Ruby Business Commons 2008 11 8 51
  52. 52. C:ruby-processing>jruby rbc/rotate_rectangles.rb       Ruby Business Commons 2008 11 8 52
  53. 53.   Ruby Business Commons 2008 11 8 53
  54. 54. ... def setup # # end 400x400 def draw # end ... Ruby Business Commons 2008 11 8 54
  55. 55.             Ruby Business Commons 2008 11 8 55
  56. 56. def setup color_mode HSB, 100 background 0 no_stroke end def draw 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 56
  57. 57. USB Ruby Business Commons 2008 11 8 57
  58. 58. Ruby Business Commons 2008 11 8 58
  59. 59. C:ruby-processing>jruby rbc/rotate_rectangles.rb Ruby Business Commons 2008 11 8 59
  60. 60. Ruby Business Commons 2008 11 8 60
  61. 61. Ruby Business Commons 2008 11 8 61
  62. 62. Ruby Business Commons 2008 11 8 62
  63. 63. C:ruby-processingruby-processing.rb C:ruby-processing>jirb -r irb/completion irb(main):001:0> Dir.pwd => quot;C:/ruby-processingquot; Pretty Print! irb(main):002:0> require 'pp' => true irb(main):003:0> pp $LOAD_PATH [quot;C:/jruby-1.1.4/lib/ruby/site_ruby/1.8quot;, quot;C:/jruby-1.1.4/lib/ruby/site_rubyquot;, quot;C:/jruby-1.1.4/lib/ruby/1.8quot;, quot;C:/jruby-1.1.4/lib/ruby/1.8/javaquot;, quot;lib/ruby/1.8quot;, $LOAD_PATH quot;.quot;] => nil Ruby Business Commons 2008 11 8 63
  64. 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. 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
  66. 66. Ruby Business Commons 2008 11 8 66
  67. 67. C:ruby-processing>jruby rbc/rotate_rectangles.rb Ruby Business Commons 2008 11 8 67
  68. 68. Ruby Business Commons 2008 11 8 68
  69. 69.       Ruby Business Commons 2008 11 8 69
  70. 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. 71.             Ruby Business Commons 2008 11 8 71
  72. 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
  73. 73. Ruby Business Commons 2008 11 8 73
  74. 74. C:ruby-processing>jruby rbc/rotate_rectangles.rb Ruby Business Commons 2008 11 8 74
  75. 75. Ruby Business Commons 2008 11 8 75
  76. 76. Ruby Business Commons 2008 11 8 76
  77. 77. require http://qwik.rubybizcommons.jp/518.html 2008 11 8 77
  78. 78. Q&A http://groups.google.com/group/rbc-incubator 2008 11 8 78
  79. 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
  80. 80. Ruby Business Commons 2008 11 8 80
  81. 81. C:ruby-processing>jruby rbc/rotate_rectangles.rb       Ruby Business Commons 2008 11 8 81
  82. 82. Ruby Business Commons 2008 11 8 82
  83. 83. ary = (0..11).map{|a| a*30} ary.each do |r2| push_martix Ruby Business Commons 2008 11 8 83
  84. 84.  (0..11).map{|a| a * 30} C:>jirb -r irb/completion irb(main):001:0> ary = (0..11).map{|a| a * 30} => [0, 30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330] irb(main):002:0> ary = (0..11).inject([]){ |a,n| a << n*30 } => [0, 30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330] Ruby Business Commons 2008 11 8 84
  85. 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. 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
  87. 87. Ruby Business Commons 2008 11 8 87
  88. 88. C:ruby-processing>jruby rbc/rotate_rectangles.rb Ruby Business Commons 2008 11 8 88
  89. 89. Ruby Business Commons 2008 11 8 89
  90. 90.   Ruby Business Commons 2008 11 8 90
  91. 91. 1.   Ruby Business Commons 2008 11 8 91
  92. 92. Ruby Business Commons 2008 11 8 92
  93. 93. rbc   Ruby Business Commons 2008 11 8 93
  94. 94. C:ruby-processing>jruby script/generate rbc/movable_dot.rb 300 300   Ruby Business Commons 2008 11 8 94
  95. 95. movable_dot.rb           Ruby Business Commons 2008 11 8 95
  96. 96. 300×300   Ruby Business Commons 2008 11 8 96
  97. 97. require 'ruby-processing' class MovableDot < Processing::App def setup end def draw end end MovableDot.new :title => quot;Movable Dotquot;, :width => 300, :height => 300 Ruby Business Commons 2008 11 8 97
  98. 98. Ruby Business Commons 2008 11 8 98
  99. 99.             Ruby Business Commons 2008 11 8 99
  100. 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
  101. 101. Ruby Business Commons 2008 11 8 101
  102. 102. Ruby Business Commons 2008 11 8 102
  103. 103.       Ruby Business Commons 2008 11 8 103
  104. 104. require 'ruby-processing' class Position   attr_accessor :x, :y end     class Rectangle < Position end       Ruby Business Commons 2008 11 8 104
  105. 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. 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
  107. 107. Ruby Business Commons 2008 11 8 107
  108. 108. C:ruby-processing>jruby rbc/movable_dot.rb         Ruby Business Commons 2008 11 8 108
  109. 109.   Ruby Business Commons 2008 11 8 109
  110. 110.       Ruby Business Commons 2008 11 8 110
  111. 111. irb(main):003:0> class Foo irb(main):004:1> attr_accessor :name irb(main):005:1> end => nil attr_accessor irb(main):006:0> foo = Foo.new => #<Foo:0x12ddfe4> irb(main):007:0> foo.name = quot;Barquot; => quot;Barquot; irb(main):008:0> puts foo.name Bar => nil Ruby Business Commons 2008 11 8 111
  112. 112. :name Symbol irb(main):001:0> quot;fooquot;.to_s => quot;fooquot; irb(main):002:0> quot;fooquot;.to_sym => :foo irb(main):003:0> :foo.to_sym => :foo irb(main):004:0> :foo.to_s => quot;fooquot; irb(main):005:0> quot;fooquot;.is_a?(Symbol) => false irb(main):006:0> :foo.is_a?(String) => false Ruby Business Commons 2008 11 8 112
  113. 113. Symbol Symbol irb(main):001:0> quot;fooquot;.object_id => 285710 irb(main):002:0> quot;fooquot;.object_id => 278950 # irb(main):003:0> :foo.object_id => 145858 irb(main):004:0> :foo.object_id => 145858 # # Ruby Business Commons 2008 11 8 113
  114. 114. Symbol irb(main):026:0> quot;fooquot;.methods.size => 153 irb(main):027:0> :foo.methods.size => 46 Ruby Business Commons 2008 11 8 114
  115. 115. Symbol irb(main):001:0> hash = {:x => 100, :y => 200} => {:x=>100, :y=>200} irb(main):002:0> hash[:x] => 100 irb(main):002:0> hash[:y] => 200 irb(main):002:0> hash[:z] => nil Ruby Business Commons 2008 11 8 115
  116. 116. Symbol irb(main):013:0> def foo(options = {}) irb(main):014:1> options[:bar] irb(main):015:1> end => nil irb(main):016:0> foo(:bar => 1) => 1 irb(main):017:0> foo(:hoge => 2) => nil Ruby Business Commons 2008 11 8 116
  117. 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. 118.   def initialize(x = 0, y = 0, &block) move_to(x, y, &block) end   Ruby Business Commons 2008 11 8 118
  119. 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. 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
  121. 121. irb(main):026:0> num = 1 => 1 irb(main):027:0> num % 2 == 0 ? quot;evenquot; : quot;oddquot; => quot;oddquot; irb(main):028:0> num = 2 => 2 irb(main):029:0> num % 2 == 0 ? quot;evenquot; : quot;oddquot; => quot;evenquot; 2008 11 8 121
  122. 122. yield irb(main):050:0> block_1 = Proc.new {|x, y| puts x + y} => #<Proc:0x13a8eb1@(irb):51> irb(main):051:0> block_2 = proc {|x, y| puts x + y} => #<Proc:0x15200be@(irb):52> irb(main):052:0> block_3 = lambda {|x, y| puts x + y} => #<Proc:0x11b50a1@(irb):53> 2008 11 8 122
  123. 123. yield irb(main):053:0> block_1.call(1, 2) 3 => nil irb(main):054:0> block_1[1, 2] 3 => nil irb(main):055:0> block_2[1, 2] 3 => nil irb(main):056:0> block_3[1, 2] 3 => nil 2008 11 8 123
  124. 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. 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. 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. 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
  128. 128. Ruby Business Commons 2008 11 8 128
  129. 129.   Ruby Business Commons 2008 11 8 129
  130. 130.       Ruby Business Commons 2008 11 8 130
  131. 131. class Dot include Math DEFAULT_SIZE = 10 DEFAULT_MARGIN = 2 def initialize(position, w = DEFAULT_SIZE , h = DEFAULT_SIZE) @app = Processing::App.current @rect = Rectangle.new(position.x, position.y, w, h) end def display c1 = @app.color(255, 255, 255, 255) @app.fill c1 @app.rect(*@rect.to_a) end end Ruby Business Commons 2008 11 8 131
  132. 132. Ruby Business Commons 2008 11 8 132
  133. 133. C:ruby-processing>jruby rbc/movable_dot.rb         Ruby Business Commons 2008 11 8 133
  134. 134.     Ruby Business Commons 2008 11 8 134
  135. 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. 136.       Ruby Business Commons 2008 11 8 136
  137. 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. 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
  139. 139. Ruby Business Commons 2008 11 8 139
  140. 140. C:ruby-processing>jruby rbc/movable_dot.rb       Ruby Business Commons 2008 11 8 140
  141. 141.   2008 11 8 141
  142. 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. 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
  144. 144. Ruby Business Commons 2008 11 8 144
  145. 145. C:ruby-processing>jruby rbc/movable_dot.rb       Ruby Business Commons 2008 11 8 145
  146. 146. 2008 11 8 146
  147. 147. *(0..3).map{rand(255)} irb(main):005:0> p (0..3).map{rand(255)} [36, 185, 183, 242] => nil irb(main):006:0> p *(0..3).map{rand(255)} 98 3 227 88 => nil 2008 11 8 147
  148. 148.     class MovableDot < Processing::App THE_NUM_OF_DOT = 20 def setup frame_rate 1 @dots = [] Ruby Business Commons 2008 11 8 148
  149. 149. Ruby Business Commons 2008 11 8 149
  150. 150. C:ruby-processing>jruby rbc/movable_dot.rb         Ruby Business Commons 2008 11 8 150
  151. 151. 2008 11 8 151
  152. 152.     def display c1 = @app.color(*(0..3).map{rand(255)}) @app.fill c1 @app.stroke c1 @app.rect(*@rect.to_a) end Ruby Business Commons 2008 11 8 152
  153. 153. Ruby Business Commons 2008 11 8 153
  154. 154. C:ruby-processing>jruby rbc/movable_dot.rb         Ruby Business Commons 2008 11 8 154
  155. 155. 2008 11 8 155
  156. 156.             2008 11 8 156
  157. 157.   @dots = [] dot_size, dot_margin = Dot::DEFAULT_SIZE, Dot::DEFAULT_MARGIN THE_NUM_OF_DOT.times do 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 THE_NUM_OF_DOT end Ruby Business Commons 2008 11 8 157
  158. 158.   Ruby Business Commons 2008 11 8 158
  159. 159. C:ruby-processing>jruby rbc/movable_dot.rb Ruby Business Commons 2008 11 8 159
  160. 160. 2008 11 8 160
  161. 161.       Ruby Business Commons 2008 11 8 161
  162. 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
  163. 163. Ruby Business Commons 2008 11 8 163
  164. 164.   yield def move_to(x, y) @x = block_given? ? yield(x, :x, @x) : x @y = block_given? ? yield(y, :y, @y) : y end def move_by(dx, dy) @x += block_given? ? yield(dx, :x, @x) : dx @y += block_given? ? yield(dy, :y, @y) : dy end end class Rectangle < Position 2008 11 8 164
  165. 165.     def initialize(position, w = DEFAULT_SIZE , h = DEFAULT_SIZE) @app = Processing::App.current @rect = Rectangle.new(position.x, position.y, w, h) @original_position = position.dup @@dot_count ||= 0 @dot_num = (@@dot_count += 1) @speed = 0.05 @next @next = Position.new end 2008 11 8 165
  166. 166. Numeric   irb(main):001:0> 10 / 3 => 3 irb(main):002:0> 10 / 3.0 => 3.3333333333333335 irb(main):003:0> 10.0 / 3 => 3.3333333333333335 irb(main):004:0> (10/3).class => Fixnum irb(main):005:0> (10/0.3).class => Float irb(main):006:0> (10.0/3).class => Float irb(main):007:0> (10/3).to_f => 3.0 Ruby Business Commons 2008 11 8 166
  167. 167.   def move case @app.patterns.current when :line @next.move_to(@original_position.x, @original_position.y) when :circle @next.move_to(@app.width, @app.height) do |wh_value, attr| val, sin_or_cos = (attr == :x) ? [10, 'sin'] : [20, 'cos'] (wh_value/2-val) * self.__send__(sin_or_cos, radians(@dot_num * 360/ @@dot_count)) + wh_value/2 end else raise NameError, quot;Unsupported pattern: #{@app.patterns.current.inspect}quot; end @rect.move_by(@rect.x - @next.x, @rect.y - @next.y) do |value, attr| -(value * @speed) end end 2008 11 8 167
  168. 168.     end end def radians(x) x * (PI/180) end def display c1 = @app.color(*(0..3).map{rand(255)}) 2008 11 8 168
  169. 169. Ruby Business Commons 2008 11 8 169
  170. 170. (wh_value/2-val) * self.__send__(sin_or_cos, radians(@dot_num * 360/@@dot_count)) + wh_value/2 x = y =     2008 11 8 170
  171. 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
  172. 172. send irb(main):001:0> t = Time.now => Wed Nov 08 00:07:41 +0900 2008 irb(main):002:0> t.year => 2008 send irb(main):003:0> t.month => 11 irb(main):004:0> t.day => 8 irb(main):005:0> t.send(:year) => 2008 irb(main):006:0> t.send(:month) => 11 irb(main):007:0> t.send(:day) => 8 Ruby Business Commons 2008 11 8 172
  173. 173. send irb(main):001:0> t = Time.now => Wed Nov 08 00:07:41 +0900 2008 irb(main):002:0> t.strftime('%Y/%m/%d %H:%M:%S') => quot;2008/11/05 00:07:41quot; irb(main):003:0> t.send(:strfrime, '%Y/%m/%d %H:%M:%S') => quot;2008/11/05 00:07:41quot; Ruby Business Commons 2008 11 8 173
  174. 174. send irb(main):001:0> class Foo irb(main):002:1> attr_accessor :bar irb(main):003:1> end => nil irb(main):004:0> foo = Foo.new => #<Foo:0x7e1596> irb(main):005:0> foo.bar = quot;AAAquot; => quot;AAAquot; irb(main):006:0> foo.send(:bar=, quot;BBBquot;) => quot;BBBquot; irb(main):007:0> foo.bar => quot;BBBquot; Ruby Business Commons 2008 11 8 174
  175. 175.     class MovableDot < Processing::App attr_accessor :patterns frame_rate THE_NUM_OF_DOT = 20 def setup 5 frame_rate @patterns = LoopQueue.new(:line, :circle) @dots = [] 2008 11 8 175
  176. 176.       def draw background 0 @dots.each {|dot| dot.display; dot.move} end def mouse_pressed @patterns.next end 2008 11 8 176
  177. 177. Ruby Business Commons 2008 11 8 177
  178. 178. C:ruby-processing>jruby rbc/movable_dot.rb   Ruby Business Commons 2008 11 8 178
  179. 179. 2008 11 8 179
  180. 180.       val, sin_or_cos = (attr == :x) ? [10, 'sin'] : [20, 'cos'] (wh_value/2-val) * self.__send__(sin_or_cos, radians(@dot_num * 360/ @@dot_count)) + wh_value/2 end when :center @next.move_to(@app.width/2, @app.height/2) when :random @next.move_by(@app.width/8, @app.height/8) do |value, attr| @app.random(-value, value) end else raise NameError, quot;Unsupported pattern: #{@app.patterns.current.inspect}quot; Ruby Business Commons 2008 11 8 180
  181. 181.   def setup frame_rate 5 @patterns = LoopQueue.new(:line, :circle, :center, :random) @dots = []     Ruby Business Commons 2008 11 8 181
  182. 182. Ruby Business Commons 2008 11 8 182
  183. 183. C:ruby-processing>jruby rbc/movable_dot.rb       Ruby Business Commons 2008 11 8 183
  184. 184. 400   Ruby Business Commons 2008 11 8 184
  185. 185. Ruby Business Commons 2008 11 8 185
  186. 186. 2008 11 8 186
  187. 187. Ruby Business Commons 2008 11 8 187
  188. 188. Ruby Business Commons 2008 11 8 188
  189. 189. Ruby Business Commons 2008 11 8 189
  190. 190. Ruby http://qwik.rubybizcommons.jp/members/2.html Ruby Business Commons 2008 11 8 190
  191. 191. Ruby Business Commons 2008 11 8 191
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×