Hi, I’m having difficulty working out the following construct:
img.write(“myimage.jpg”) { self.quality = 50 }
In this example (source
RMagick 1.15.0: Common Tasks), img is a
RMagick::Image.
The API says “You may also specify optional arguments by setting
Image::Info http://www.simplesystems.org/RMagick/doc/info.html
attributes in an associated block.” Is this a standard syntax in Ruby or
is it specific to this write method? And how does it work, how can I
make my own method accept blocks too? any advantage over passing a array
of arguments?
Also, I’m trying to pass my own instance variable in order to replace
the sample value of 50. So I’ve tried:
img.write(“myimage.jpg”) { self.quality = @my_object_quality }
Where @my_object_quality is defined as an instance variable on my class
def. But the interpreters seams to look for a @my_object_quality
instance variable within the RMagick class, which vaguely kind of make
sense. So how can I pass this instance variable into this method? and
why does it say “self.quality = 50” in the example and not “@quality =
50”?
Thanks in adance for your help. I’m looking for a genuine explanation of
what’s happening at a Ruby syntax level, not just a code sample that
would work with this RMagick example.
Nic