这个脚本是用来draw_text可以使用换行符的,但导入出错了
报错下面有图片
这是脚本
[pre lang="Ruby"]
class Bitmap
alias :draw_text2 :draw_text unless defined? :draw_text2
def draw_text(*args)
case args.size
when 2
rect = args[0]
text = args[1].split("\n")
align = 0
when 3
rect = args[0]
text = args[1].split("\n")
align = args[2]%3
when 5
rect = Rect.new(*(args[0,4]))
text = args[4].split("\n")
align = 0
when 6
rect = Rect.new(*(args[0,4]))
text = args[4].split("\n")
align = args[5]%3
else
raise ArgumentError,"错误的参数个数(#{args.size} to 2,3,5,6)."
end
return if text.empty?
wmax = [text.map{|t| self.text_size(t).width}.max,rect.width].min
h = self.text_size(text[0]).height
hmax = [h*text.size,rect.height].min
y = rect.y+(rect.height-hmax)/2
x = align==0 ? rect.x : align==2 ? rect.x+rect.width-wmax : rect.x+(rect.width-wmax)/2
text.each_with_index{|t,i|
draw_text2(x,y+i*h,wmax,h,t,align)
}
self
end
end
[/pre]
defined? expression tests whether or not expression refers to anything recognizable (literal object, local variable that has been initialized, method name visible from the current scope, etc.). The return value is nil if the expression cannot be resolved. Otherwise, the return value provides information about the expression.
Note that the expression is not executed.
p defined?(def x; end) # "expression"
x # error: undefined method or variable
p defined?(@x=1) # "assignment"
p @x # nil
Assignment to a local variable will, however, have the usually result of initializing the variable to nil by virtue of the assignment expression itself:
p defined?(x=1) # assignment
p x # nil
In most cases, the argument to defined? will be a single identifier: