English 中文(简体)
• 如何将所有班级名称带在鲁比拉的一个名称空间?
原标题:How to get all class names in a namespace in Ruby?
  • 时间:2010-09-09 11:56:41
  •  标签:
  • ruby

页: 1 Foo,它为诸如等许多班级的名称空间。 Foo:Bar,Foo:Baz等。

是否有办法按<代码>重新命名所有类别名称。 Foo

最佳回答
Foo.constants

页: 1 Foo。 这包括但不限于类别名称。 如果你只想人种名,你可以使用。

Foo.constants.select {|c| Foo.const_get(c).is_a? Class}

如果你想要分类和模块名称,你可以使用<代码>is_a? 页: 1 类别

问题回答

如果你不喜欢“点名,就希望课堂本身,你可以这样做:

Foo.constants.map(&Foo.method(:const_get)).grep(Class)

In short no. However, you can show all classes that have been loaded. So first you have to load all classfiles in the namespace:

Dir["#{File.dirname(__FILE__)}/lib/foo/*.rb"].each {|file| load file}

然后,你可以采用诸如Jörg W Mittag等方法,列出这些班级。

Foo.constants.map(&Foo.method(:const_get))。grep(Class)

This will only return the loaded constants under the given namespace because ruby uses a lazy load approach. So, if you type

Foo.constants.select {|c| Foo.const_get(c).is_a? Class}

页: 1

[]

但在打字之后:

Foo::Bar

页: 1

[:Bar]

这一替代解决办法将装上all<>m>m>。 <代码>Foo: , 包括“嵌入”类别,例如Foo:Bar:Bar<>/code>。

Dir["#{File.dirname(__FILE__)}/lib/foo/**/*.rb"].each { |file| load file }
ObjectSpace.each_object(Class).select { |c| c.to_s.start_with? "Foo::" }

注:**/*.rb可追溯生效。

请列入<代码> 缩略语/代码类别本身,请将<条码>电子显示/编码>改为:<条码>;>;>;>;<>;>;>; >; >; ......





相关问题
Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

multiple ruby extension modules under one directory

Can sources for discrete ruby extension modules live in the same directory, controlled by the same extconf.rb script? Background: I ve a project with two extension modules, foo.so and bar.so which ...

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...