Question

I'm sure I'm missing something basic since I don't use ruby that much.

I have this class (truncated for simplicity):

require 'koala'
require 'time'
class FBRank
    def initialize_with(access_token, photo_rank_weight=150, link_rank_weight=30, post_rank_weight=140, tagged_rank_weight=70, liked_photo_weight=150)
        puts "hello?"
        @graph_api = Koala::Facebook::API.new(access_token)
        @photo_rank_weight = photo_rank_weight
        @link_rank_weight = link_rank_weight        
        @post_rank_weight  = post_rank_weight
        @tagged_rank_weight = tagged_rank_weight
        @liked_photo_weight = liked_photo_weight
    end
end

Then I:

:young-waters-3693[104] > irb
1.9.3p429 :002 > require './ranking'
1.9.3p429 :002 > FBRank.initialize_with('CAAJzb7....')
NoMethodError: undefined method `initialize_with' for FBRank:Class

What have I not instantiated correctly? I tried creating an object that is an instance of this class but that didn't work either, but that could have been because I did it wrong...

Was it helpful?

Solution

Class methods are defined by prepending self.:

class FBRank
  def self.initialize_with(...)
  end
end

However, you should return an instance, something like:

def self.initialize_with(access_token, photo_rank_weight=150, link_rank_weight=30, post_rank_weight=140, tagged_rank_weight=70, liked_photo_weight=150)
  rank = FBRank.new
  rank.access_token = access_token
  # ...
  rank
end

Or even better use Ruby's initialize method:

def initialize(access_token, photo_rank_weight=150, link_rank_weight=30, post_rank_weight=140, tagged_rank_weight=70, liked_photo_weight=150)
  @access_token = access_token
  # ...
end

You can then create a FBRank instance with:

FBRank.new('CAAJzb7', 150, 30, 140, 70, 150)

It might also be a good idea to use a hash for passing the options:

def initialize(access_token, options={})
  @access_token = access_token
  @photo_rank_weight = options.fetch(:photo_rank_weight, 150)
  @link_rank_weight = options.fetch(:link_rank_weight, 30)
  @post_rank_weight = options.fetch(:post_rank_weight, 140)
  @tagged_rank_weight = options.fetch(:tagged_rank_weight, 70)
  @liked_photo_weight = options.fetch(:liked_photo_weight, 150)
end

and initialize it via:

FBRank.new('CAAJzb7',
           photo_rank_weight: 150,
           link_rank_weight: 30,
           post_rank_weight: 140,
           tagged_rank_weight: 70,
           liked_photo_weight: 150)

OTHER TIPS

You are calling an instance method from the Class itself which is why you're getting no method error. If you're trying to set it up with an initialize_with to prep an instance then you should change this:

FBRank.initialize_with('CAAJzb7....')

to this:

FBRank.new.initialize_with('CAAJzb7....') 

Calling new creates a new FBRank instance and then you use your initialize_with to configure the instance variables; however, you would not be able to chain this call and assign a variable like this unless you set initialize_with to return self at the end.

Although, creating your own initialize seems kind of pointless, just use def initialize and then you pass your arguments to new. After all that is the point of the initialize block is to prepare an instance for use.

FBRank.new('CAAJzb7....')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top