SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Jun 2009
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    RubyInline Errors (Calling Static Inline Methods?)

    I'm trying to learn Ruby from a book. Unfortunately, I'm having alot of trouble with Rubyline. The book provides the following code:
    Code:
    require 'rubygems'
    require 'inline'
    require 'benchmark'
    
    class CFactorial
      class << self
        inline do |builder|
          builder.c &#37;q{
          long factorial(int value){
            long result = 1, i = 1;
            for(i = 1; i <= value; ++i)
              result *= i;
            return result;
          }
        }
        end
      end
    end

    Trying to call factorial via:
    Code:
    puts CFactorial.factorial(8)
    yields the error message:
    Code:
    /usr/local/lib/ruby/gems/1.9.1/gems/RubyInline-3.8.1/lib/inline.rb:365:in `module_name': undefined method `gsub' for nil:NilClass (NoMethodError)
    	from /usr/local/lib/ruby/gems/1.9.1/gems/RubyInline-3.8.1/lib/inline.rb:375:in `so_name'
    	from /usr/local/lib/ruby/gems/1.9.1/gems/RubyInline-3.8.1/lib/inline.rb:498:in `load_cache'
    	from /usr/local/lib/ruby/gems/1.9.1/gems/RubyInline-3.8.1/lib/inline.rb:818:in `inline'
    	from /home/craig/ruby/inline.rb:8:in `singletonclass'
    	from /home/craig/ruby/inline.rb:7:in `<class:CFactorial>'
    	from /home/craig/ruby/inline.rb:6:in `<main>'
    Removing the class << self (which is not explained), but appears to designate a block of code that is static, allows the inline code to work via an instantiated object. But I'd like the inline code to be static to the class. Does anyone know what the problem is?

  2. #2
    SitePoint Addict
    Join Date
    Feb 2007
    Posts
    270
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oy vey.

    I guess I'm mystified on many fronts, not the least of which is is why you'd want to learn Ruby (your stated goal) by writing C. (Rubyinline lets you write C in ruby.) Shouldn't you be writing ruby instead? What book are you using that takes such a scenic route to the objective?

    But, to get back to the problem at hand:

    "class << self" simply defines a singleton method (a static method, which can be run without instantiating the object). This is why it instantiates it when you remove it. As for the bugs in rubyinline, you should probably check with its author. It's not part of ruby and I'd never seen the package before today.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •