kinesis
kinesis

Reputation: 157

How do you pass arguments to class << self in Ruby?

How do you pass arguments to class << self in Ruby? I have a snippet I'm working with below and I am trying to generate a picture using RMagick.

#!/usr/bin/env ruby
%w[ rubygems RMagick ].each{|l| require l  }
%w[ Magick ].each{|i| require i }

module ImgGen
  class << self
    def start
      stripes = ImageList.new
      puts "hi"
    end

  end
end

WIDTH=650
HEIGHT=40
FILENAME="output.png"
FONT="winvga1.ttf"
ImgGen.start(WIDTH, HEIGHT, FILENAME, FONT)

Upvotes: 2

Views: 2116

Answers (1)

Andrew Marshall
Andrew Marshall

Reputation: 96954

The arguments don't get passed to class << self, they get passed to the method:

module ImgGen
  class << self
    def start(width, height, filename, font)
      stripes = ImageList.new
      puts "hi"
    end
  end
end

You can read a detailed description of what class << self does if it confuses you, but in short: it opens up the class's singleton class so you can add methods to it.

Upvotes: 5

Related Questions