2

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)
Mast
  • 1,788
  • 4
  • 29
  • 46
kinesis
  • 157
  • 1
  • 2
  • 12

1 Answers1

5

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.

Community
  • 1
  • 1
Andrew Marshall
  • 95,083
  • 20
  • 220
  • 214