# Modules in lib not follwing naming convension

**URL:** <https://www.ruby-forum.com/t/modules-in-lib-not-follwing-naming-convension/111659>\
**Category:** Rails\
**Created:** [August 9, 2007, 4:15pm UTC](https://www.ruby-forum.com/t/modules-in-lib-not-follwing-naming-convension/111659 "2007-08-09T16:15:02Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![kaffepanna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/35a633/32.png) [@kaffepanna](https://www.ruby-forum.com/u/kaffepanna)\
**Post date:** [August 9, 2007, 4:15pm UTC](https://www.ruby-forum.com/t/modules-in-lib-not-follwing-naming-convension/111659/1 "2007-08-09T16:15:02Z")

</div>

Hej! Okey i have a problem, i have earlier written a set of classes in  
ruby implementing a msrp chat client and now im trying to use it in my  
rails app.

The msrp implementation consists of several files and most of the  
functions and classes in those files ar encapsulated in a module lets  
call it X. so for instance they look like this

header.rb

module X  
class Z  
…  
end  
end

The problem now is that rails some how gets confused about the names of  
the files and what modules and classes they contain or something… For  
instance i can instantiate classes in the module but some methods are  
not recognised and all is a mess. So i guess my question is how do i  
include these files in my project in a good way? without  
renaming/reorganizing them.

---

<div class="post-metadata">

**Author:** ![kaffepanna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/35a633/32.png) [@kaffepanna](https://www.ruby-forum.com/u/kaffepanna)\
**Post date:** [August 9, 2007, 5:18pm UTC](https://www.ruby-forum.com/t/modules-in-lib-not-follwing-naming-convension/111659/2 "2007-08-09T17:18:17Z")

</div>

If the naming doesn’t follow what Rails is looking for, just do a  
require ’  
header.rb’ in whatever controller / model you want to use said library  
in.  
Rails already adds lib/ to the load path, so you don’t have to worry  
about  
finding the correct path.

Jason

---

<div class="post-metadata">

**Author:** ![kaffepanna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/35a633/32.png) [@kaffepanna](https://www.ruby-forum.com/u/kaffepanna)\
**Post date:** [August 9, 2007, 6:10pm UTC](https://www.ruby-forum.com/t/modules-in-lib-not-follwing-naming-convension/111659/3 "2007-08-09T18:10:56Z")

</div>

Rails follow a very simple pattern to load you modules. If you are  
looking for a module SmsChat, rails will look for sms\_chat.rb in your  
\*lib directory and in some /vendor directories too, i think. Try this:

1. make a sms\_chat directory in /lib

2. make a file sms\_chat.rb in your /lib directory. This will be  
required whenever you call SmsChat::something.

sms\_chat.rb --------------

module SmsChat  
end

# Required file loader hackery…

lib\_path = File.join(File.dirname( **FILE** ), ‘sms\_chat’)

# will require /lib/sms\_chat/header.rb \>\>

require File.join(lib\_path, ‘header.rb’)

# will require /lib/sms\_chat/other.rb \>\>

require File.join(lib\_path, ‘other’)

-----------------------EOF

1. If you keep having problems, try reference the module from the  
“global namespace”, just adding two colons before the module:

instead of

```
   SmsChat::MsrpURI

```

use

```
   ::SmsChat::MsrpURI
```

---

<div class="post-metadata">

**Author:** ![kaffepanna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/35a633/32.png) [@kaffepanna](https://www.ruby-forum.com/u/kaffepanna)\
**Post date:** [August 9, 2007, 5:58pm UTC](https://www.ruby-forum.com/t/modules-in-lib-not-follwing-naming-convension/111659/4 "2007-08-09T17:58:09Z")

</div>

Thank you for clearing that up to me. Still having troubles tho. It  
seems like methods in one or more of the classes wont be defined on  
requiring the file, works great in the ruby interpreter but in rails i  
get undefined method on methods not declared as a attr\_, also the to\_s  
of MsrpURI class is being ignored and the object standard method get  
called.

module SmsChat  
class MsrpURI  
attr\_accessor :username, :password, :host, :port, :resource,  
:protocol

```
def initialize str
  matches = 

```

/(msrp|sms)://((\w+)(:(\w+))?@)?([\w.]+)(:(\d+))?/?(\w+)?/.match  
str  
raise “Formatting error” unless matches

```
  @protocol = matches[1]
  @username = matches[3]
  @password = matches[5]
  @host = matches[6]
  @port = matches[8].to_i
  @resource = matches[9].to_i

  #port = 13040 unless port > 0

end

```

end

def to\_s  
str = String.new  
str \<\< @protocol;  
str \<\< “://”  
if @username  
str \<\< @username  
str \<\< “:#{@password}” if @password  
str \<\< “@”  
end

```
str << @host
str << ":#{@port}" if @port > 0
 str << "/#{@resource}" if @resource > 0
return str

```

end  
end

---

<div class="post-metadata">

**Author:** ![kaffepanna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/35a633/32.png) [@kaffepanna](https://www.ruby-forum.com/u/kaffepanna)\
**Post date:** [August 9, 2007, 6:19pm UTC](https://www.ruby-forum.com/t/modules-in-lib-not-follwing-naming-convension/111659/5 "2007-08-09T18:19:26Z")

</div>

That all looks fine, what does the code that uses this look like?

Jason

---

<div class="post-metadata">

**Author:** ![kaffepanna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/35a633/32.png) [@kaffepanna](https://www.ruby-forum.com/u/kaffepanna)\
**Post date:** [August 9, 2007, 8:23pm UTC](https://www.ruby-forum.com/t/modules-in-lib-not-follwing-naming-convension/111659/6 "2007-08-09T20:23:49Z")

</div>

Followed your suggestion and still no go, it findes the modules now and  
i can create the uri object but still when i call to\_s function the only  
thing i get is the standard Object.to\_s object reference… 😕

the code that is currently using this is a short test function

class SmschatController \< ApplicationController  
include SmsChat

def test  
uri = MsrpURI.new ‘msrp://test’  
render :text =\> uri.to\_s  
end  
end

Thanks for all the help… Im really getting gray hairs over this

---

<div class="post-metadata">

**Author:** ![kaffepanna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/35a633/32.png) [@kaffepanna](https://www.ruby-forum.com/u/kaffepanna)\
**Post date:** [August 9, 2007, 10:36pm UTC](https://www.ruby-forum.com/t/modules-in-lib-not-follwing-naming-convension/111659/7 "2007-08-09T22:36:59Z")

</div>

Hmm, everything looks good.

I’d say try SmsChat::MsrpURI.new…

and as a sanity check, are you restarting your server for each change to  
this library? Only classes that are auto-loaded by Rails (e.g. not  
yours)  
are reloaded with each request.

Jason

---

<div class="post-metadata">

**Author:** ![kaffepanna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/35a633/32.png) [@kaffepanna](https://www.ruby-forum.com/u/kaffepanna)\
**Post date:** [August 9, 2007, 8:39pm UTC](https://www.ruby-forum.com/t/modules-in-lib-not-follwing-naming-convension/111659/8 "2007-08-09T20:39:37Z")

</div>

hum just realized that your example only works once… If you try to  
visit it twise it cant find the module the second time and you have to  
restart the server! ☹
