Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #YAML file
- ---
- Object_1 : [red , blue , green ]
- Object_2 : [ green , orange, blue ]
- Object_3 : [ green , orange , red ]
- Thing_1 : [ black , white, red ]
- Thing_2 : [ blue, white, green ]
- Thing_3 : [ green, white, blue ]
- ## Etc...
- ## Object.rb => Class containing my objects. Stuct : 3 colors.
- class Object
- Object = Struct.new(:color1 , :color2, :color3)
- class << self
- attr_accessor :object
- end
- self.object = []
- def self.add(color1, color2, color3)
- object << Object.new(color1, color2, color3)
- end
- def self.all
- object
- end
- end
- ## The "Thing" class is exactly the same (with another name so). But I have to make them both different, hard to explain to you without the rest of the code.
- ## CreateObjects.rb => Read the YAML file and add Objects/Thing according to it.
- require 'rubygems'
- require 'yaml'
- require 'Object'
- require 'Thing'
- class GetIt
- def initialize(filename)
- @file = filename
- readfile
- end
- def readfile
- begin
- config_file = YAML::load_file(@file)
- parsefile(config_file)
- rescue SystemCallError => e
- puts "#{e.message}"
- end
- end
- def parsefile(file)
- file.each_pair do |key,value|
- ## Here I want to add the objects : if the key contains 'Object_', then it's an object and let's add it, if it's a Thing, same thing. I don't know how anyway :/
- end
- end
- end
- GetIt.new('config.yaml')
Add Comment
Please, Sign In to add comment