Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
51 lines (31 sloc) 1.3 KB

Scope: A tip about using YAML files

# sample19.rb
require 'yaml'

Shoes.app :width => 200, :height => 100 do
  Gang = Struct.new :name, :country
  gangs = YAML.load_file(Dir.pwd + '/gangs.yml')
  gangs.each{|g| para g.name, g.country, "\n"}
end

sample19.png

sample19.png

The top-level namespace in any Shoes app is Shoes
so in the sample

Gang = Struct.new :name, :country <br>

we actually create a Shoes::Gang struct, not a Gang struct.
To achieve the desired behavior, we will have to modify the statement like this (see sample19-1.rb).

::Gang = Struct.new :name, :country <br>

Here you can see the updated example:

# sample19-1.rb
require 'yaml'

Shoes.app :width => 200, :height => 100 do
  ::Gang = Struct.new :name, :country
  gangs = YAML.load_file(Dir.pwd + '/gangs.yml')
  gangs.each{|g| para g.name, g.country, "\n"}
end

sample19-1.png

sample19-1.png

Policeman

Policeman includes Ruby 1.9. So, need to re-create the yaml file.

Use make_sample19_yaml_for_ruby1.9.rb. It can create gangs-19.yml file. Policeman can read (load) the file.