public
Description: Shoes Tutorial Note : HTML version
Homepage:
Clone URL: git://github.com/ashbb/shoes_tutorial_html.git
shoes_tutorial_html / mdowns / 00535_Scope__local_variable_and_instance_variable.mdown
100644 35 lines (25 sloc) 0.969 kb

Scope: local variable and instance variable

We need to learn the scope, the difference between local variable and instance variable, over and over again.

# sample60.rb
Shoes.app :width => 300, :height => 300 do
  i = 45
  button 'new'do
    i += 5
    box = rand(2) == 0 ? rect(i, i, 20) : oval(i, i, 20)
    box.style :fill => send(COLORS.keys.map{|sym|sym.to_s}[rand(COLORS.keys.size)])

    @flag = false
    box.click{@flag = true; @box = box}
    box.release{@flag = false}
    motion{|left, top| @box.move(left-10, top-10) if @flag}
  end

end

sample60.png

sample60.png

This snapshot is created by the following setps. - clicked 'new' button 30 times - picked up and drew 6 ovals to the lower side - picked up and drew 7 rects to the upper side

Policeman

The sample60.rb worked well with Shoes-0.r1263