views:

123

answers:

1

I have installed the gem 'simple_uuid' but nothing seems to be working.

Using irb and running the following:

require 'rubygems'
require 'simple_uuid'

is fine, both return true. But running the following:

// Class added by simple_uuid
UUID.new

returns

NameError: uninitialized constant UUID
    from (irb):3
    from :0

I'm a ruby newbie, so don't assume much in the answers. Thanks.

+3  A: 

The class is inside a module SimpleUUID. So either do "include SimpleUUID" after the require, or refer to the class with the full namespace: SimpleUUID::UUID

require 'rubygems'
require 'simple_uuid'

> UUID.new
NameError: uninitialized constant UUID
        from (irb):3

> SimpleUUID::UUID.new
 => <UUID#70305762670060 time: Sat May 01 21:11:28 +0200 2010, usecs: 843284 jitter: 13605115058679102872> 
duncan
So, gems aren't like .NET/Java name spaces?
tgandrews
No. A gem is a library which can include multiple classes and modules. Require includes a file, and the file can define whatever it wants. Usually this is driven by conventions (like most stuff in ruby).I figured the module name by looking at the file: http://github.com/ryanking/simple_uuid/blob/master/lib/simple_uuid.rbThere you can see the class enclosed in "module". Modules are not just namespaces. I recommend you read about mixins in classes.
duncan
@Tom: you can work around this by doing `include SimpleUUID` to include it into the `main` workspace.
Ryan Bigg
@Ryan_Bigg that's what I did in the end. This daft mismatch betweeen gems and namespaces is somewhat annoying. But RoR and Ruby are too powerful to ignore.
tgandrews