1

Edit: To be clear, I'm trying to use this kind of generator (i.e. with a 'yield' statement) not a Rails generator.

I have the following (simplified) initializer mixin in a Rails project that I could use some help with. What I wanted to do was create a generator to track Twitter API calls (this is just for debugging, I know about rate_limit_status).

require 'generator'
# Be sure to restart your server when you modify this file.
module TwitterMixin
  def get_auth
    @auth ||= Twitter::HTTPAuth.new(ENV['TWITTER_USER'], ENV['TWITTER_PASS'])
  end
  def count
    @counter ||= generator.new
    @counter.yield
  end
  def client
    p "#{count} Twitter API calls this iteration"
    @client ||= Twitter::Base.new(get_auth)
  end
end

The problem is that I'm getting the following error:

dlopen(/Users/john/.gem/ruby/1.8/gems/json-1.2.0/ext/json/ext/generator.bundle, 9): no suitable image found.  Did find:
/Users/john/.gem/ruby/1.8/gems/json-1.2.0/ext/json/ext/generator.bundle: mach-o, but wrong architecture - /Users/john/.gem/ruby/1.8/gems/json-1.2.0/ext/json/ext/generator.bundle

Which seems like a collision with the json generator, which is probably in a more enclosing scope. The main question is how to I ensure a Ruby standard library class (specifically the generator class) is called?

I'm still new to Ruby, BTW, and searching for "generators in Rails" is pretty dominated by Rails::Generator, so this may be fairly obvious. Also, I'm open to more elegant solutions to this problem that I may have missed. Thanks.

2
  • generator is part of the Ruby 1.8 standard library, there's some documentation at ruby-doc.org/stdlib/libdoc/generator/rdoc/index.html if that's any use. It's not in 1.9, though - I haven't fully understood why. Commented Jan 27, 2010 at 19:03
  • I imagine it's because you can use yield directly? Not sure how you'd subclass it though. Probably just build yield into a method of the class like Python. Commented Jan 28, 2010 at 4:34

3 Answers 3

1

Do you have a ruby gem on your system called "generator"? It sounds like ruby is having trouble locating the gem based on your require. If you're using the Rubigen generator, then you need:

require 'rubigen'

and so forth. try running:

gem list generator

If it doesn't pull anything up, you're probably calling the library by the wrong name.

Sign up to request clarification or add additional context in comments.

2 Comments

didn't realize I had to use a gem, I was thinking that it would be a standard library class as in: ruby-doc.org/core/classes/Generator.html Maybe I'm misunderstanding something about the standard library?
Oh, I now see that might be more name collision with Rails::Generator which is why I can't find any solutions searching.
0

You don't have to build a generator, you can use the yield method directly, which I didn't realize. This code works.

# Be sure to restart your server when you modify this file.
module TwitterMixin
  def get_auth
    @auth ||= Twitter::HTTPAuth.new(ENV['TWITTER_USER'], ENV['TWITTER_PASS'])
  end
  def count
    @num ||= 0
    while true
      num += 1
      yield num
    end
  end
  def client
    p "#{count} Twitter API calls this iteration"
    @client ||= Twitter::Base.new(get_auth)
  end
end

Comments

0

Let me reject my earlier answer and say that it was rather stupid of me to use "yield" thinking that it was a generator builtin similar to Python's yield statement. I learned today at our first RubyGorge meeting about Ruby blocks. Thus, I think I still seek a solution to this problem.

1 Comment

Selecting this as an answer because after learning more about Ruby (hand about a week under my belt at this question) I've realized that I was asking the wrong question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.