Short namespace acronym in ruby
Asked Answered
L

2

11

I'm very new to ruby. I use IronRuby and my ruby code has long namespaces:

Company:: Division::Group::Product::Package.new

since I use this ns multiple times is there a way to create a shortcut? In c# I add a using clause so I'm not required to specify the full prefix.

Lurette answered 10/9, 2011 at 16:26 Comment(1)
You might want ot have a look at make an alias for a module in Ruby?Chenopod
J
19

You can simply assign it to another constant, like:

Package = Company::Division::Group::Product::Package
Package.new
Jepson answered 10/9, 2011 at 16:32 Comment(0)
V
8

You can also use the "include" method, which is more Ruby-esk:

include Company::Division::Group::Product
Package.new

The difference between this and the current answer is that this one pulls in all constants under the namespace, where the current answer only pulls in that name.

Vogul answered 6/12, 2011 at 19:45 Comment(2)
This will not however avoid namespace conflicts. In other languages, often you can import [Package] as [short_name] I guess you could declare a class ivar to achieve the same result ? cf #11092240Chenopod
True, this will not avoid naming conflicts, but that's due to how Ruby works.Vogul

© 2022 - 2024 — McMap. All rights reserved.