In my lib folder I have billede.rb:
class Billede
require 'RMagick'
#some code that creates a watermark for a image
image.write(out)
end
How do I call/activate the class? Is the only way to change it to a Rake task?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Code ‘inside a class’ is run just like any other code. If you have a Ruby file like this:
and you run the file (either via
ruby foo.rbon the command line orrequire "./foo"orload "foo.rb"in a script) it then you will see the output:If you want to load a utility that ‘does something’ that you can then invoke from a REPL like IRB or the Rails console, then do this:
You can
require "./mystuff"to load the code, and when you’re ready to run it typeMyStuff.do_itAnd, as you may guess, you can also create methods that accept arguments.
If you want to define a file that can be included in others (with no immediate side effects) but which also “does its thing” whenever the file is run by itself, you can do this:
Now if you
requireorloadthis file therun!method won’t be invoked, but if you typeruby mystuff.rbfrom the command line it will.