tags:

views:

166

answers:

3

(sorry I should have been clearer with the code the first time I posted this. Hope this makes sense)

File "size_specification.rb"

class SizeSpecification
  def fits?     
  end
end

File "some_module.rb"

require 'size_specification'

module SomeModule
  def self.sizes
    YAML.load_file(File.dirname(__FILE__) + '/size_specification_data.yml')
  end
end

File "size_specification_data.yml

--- 
- !ruby/object:SizeSpecification 
  height: 250
  width: 300

Then when I call

SomeModule.sizes.first.fits?

I get an exception because "sizes" are Object's not SizeSpecification's so they don't have a "fits" function.

A: 

Hi Tim,
On second reading I'm a little confused, you seem to want to mix the class into module, which is porbably not so advisable. Also is the YAML supposed to load an array of the SizeSpecifications?

It appears to be that you're not mixing the Module into your class. If I run the test in irb then the require throws a LoadError. So I assume you've put two files together, if not dump it.

Normally you'd write the functionality in the module, then mix that into the class. so you may modify your code like this:

class SizeSpecification
  include SomeModule
  def fits?     
  end
end

Which will allow you to then say:

SizeSpecification::SomeModule.sizes

I think you should also be able to say:

SizeSpecification.sizes

However that requires you to take the self off the prefix of the sizes method definition.

Does that help?

robertpostill
+1  A: 

Are your settings and ruby installation ok? I created those 3 files and wrote what follows in "test.rb"

require 'yaml'
require "some_module"

SomeModule.sizes.first.fits?

Then I ran it.

$ ruby --version
ruby 1.8.6 (2008-06-20 patchlevel 230) [i486-linux]
$ ruby -w test.rb 
$

No errors!

anshul
Ditto. The code as posted works perfectly fine for me.
Jörg W Mittag
A: 

The question code got me a little confused.

In general with Ruby, if that happens it's a good sign that I am trying to do things the wrong way.

It might be better to ask a question related to your actual intended outcome, rather than the specifics of a particular 'attack' on your problem. They we can say 'nonono, don't do that, do THIS' or 'ahhhhh, now I understand what you wanna do'

fatgeekuk