Ruby Equivalent of C # 'using' Statement

I have been doing Ruby for the past few months, but another thing I haven't figured out yet is that Ruby is the equivalent of the C # operator (and other languages) using .

I use the require statement to declare my dependencies on Gems, but I get lazy and prefer not to fully qualify my commonly used class names with their namespace (namespace).

Of course, this is possible, right? I should not use the correct terminology since Google did not give me anything useful.

+4
syntax c # ruby module namespaces
Dec 03 '09 at 4:55
source share
2 answers
 >> Math::PI => 3.14159265358979 >> PI NameError: uninitialized constant PI from (irb):3 >> include Math => Object >> PI => 3.14159265358979 

OTOH, if the problem is just the names of the overlay classes, think that, as they say, "Class is an object, and Object is a class."

So:

 >> require 'csv' >> r = CSV::Reader >> r.parse 'what,ever' do |e| pe end ["what", "ever"] 

Yes, in Ruby, a class name is just a reference, like any other, to an object of class Class .

+6
Dec 03 '09 at 5:02
source share

I do not believe that there is a direct syntactic mapping. You can probably approximate it, since you can assign references to variables about almost everything, including modules and classes.

 module UsingExampleNamespace module SubExampleNamespace CON = "STANT" end end sen = UsingExampleNamespace::SubExampleNamespace puts sen::CON >> "STANT" 
+1
Dec 03 '09 at 5:34
source share



All Articles