Method: Weka::Core::Attribute.new_string

Defined in:
lib/weka/core/attribute.rb

.new_string(name) ⇒ Object

Creates a new Attribute instance of type string.

The java class defines the same constructor: Attribute(java.lang.String, java.util.List<java.lang.String>) for nominal and string attributes and handles the type internally based on the second argument.

In Java you would write following code to create a string Attribute:

Attribute attribute = new Attribute("name", (FastVector) null);

When we use a similar approach in JRuby:

attribute = Attribute.new('name', nil)

then a Java::JavaLang::NullPointerException is thrown.

Thus, we use refelection here and call the contructor explicitly, see github.com/jruby/jruby/wiki/CallingJavaFromJRuby#constructors

The object returned from Java constructor only has class Java::JavaObject so we need to cast it to the proper class

See also: stackoverflow.com/questions/1792495/casting-objects-in-jruby



48
49
50
51
52
53
54
55
# File 'lib/weka/core/attribute.rb', line 48

def new_string(name)
  constructor = Attribute.java_class.declared_constructor(
    java.lang.String,
    java.util.List
  )

  constructor.new_instance(name.to_s, nil).to_java(Attribute)
end