Paladini
Paladini

Reputation: 4572

undefined method `hashtag_url' for class `Module'

I sought a lot in StackOverflow and Google and I can't find any solution for my problem. I'm trying to override a method from Twitter-text gem, but it's a little bit more complicated than that, I think.

I edited the Twitter-text gem and created a new one to use in my project. In this new gem I change somethings and add a method called "hashtag_url" to Autolink module.

def hashtag_url(hashtagName) 

end 

The method where I call "hashtag_url" is this (this method is on the gem, too):

def link_to_hashtag(entity, chars, options = {})
    (...)

     href = hashtag_url(hashtag)
end

Hashtag_url is blank because I need access my database, so, I just declared this method in Twitter::Autolink module and want to override this method in my project. Now, let's take a look at the code of my project. I followed some tips from stackoverflow questions, and I got this result:

/lib/ritter.rb (just a test method)

require 'ritter'

Twitter::Autolink::ClassMethods.module_eval do
    def hashtag_url(hashtagName)
        puts "I think I can't do that..."
        if hashtag = Hashtag.find_by_name(hashtagName)
            puts "I'm ok guys!"
            return "http://localhost:3000/hashtags/#{hashtag.slug}"
        end
    end
end

When I try to check if the method was overrided, I use this in Rails Console:

Twitter::Autolink.method(:hashtag_url).source_location

And I got:

NameError: undefined method `hashtag_url' for class `Module'
    from (irb):1:in `method'
    from (irb):1
    from /usr/lib/ruby/gems/1.9.1/gems/railties-3.2.3/lib/rails/commands/console.rb:47:in `start'
    from /usr/lib/ruby/gems/1.9.1/gems/railties-3.2.3/lib/rails/commands/console.rb:8:in `start'
    from /usr/lib/ruby/gems/1.9.1/gems/railties-3.2.3/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

P.S: I don't call, require or include the "Ritter.rb" in any place, I need do this? My Ritter.rb is on the right path, and the code inside this is right? How can I fix this problem?

I am available to talk more about why I need the database, if necessary. Thanks, any comment and answer is welcome!

Upvotes: 0

Views: 276

Answers (1)

ovhaag
ovhaag

Reputation: 1278

If you want to add a method to Twitter::Autolink, you shold call module_eval directly on Twitter::Autolink.

Twitter::Autolink.module_eval do 
  # ..
end

Upvotes: 2

Related Questions