Project1

标题: draw_text引用换行符脚本出错 [打印本页]

作者: 破碎记忆    时间: 2017-6-17 21:36
标题: draw_text引用换行符脚本出错
本帖最后由 RyanBern 于 2017-6-19 09:37 编辑

这个脚本是用来draw_text可以使用换行符的,但导入出错了
报错下面有图片
这是脚本
RUBY 代码复制
  1. class Bitmap
  2.   alias :draw_text2 :draw_text unless defined? :draw_text2
  3.   def draw_text(*args)
  4.     case  args.size
  5.     when 2
  6.       rect = args[0]
  7.       text = args[1].split("\n")
  8.       align = 0
  9.     when 3
  10.       rect = args[0]
  11.       text = args[1].split("\n")
  12.       align = args[2]%3
  13.     when 5
  14.       rect = Rect.new(*(args[0,4]))
  15.       text = args[4].split("\n")
  16.       align = 0
  17.     when 6
  18.       rect = Rect.new(*(args[0,4]))
  19.       text = args[4].split("\n")
  20.       align = args[5]%3
  21.     else
  22.       raise ArgumentError,"错误的参数个数(#{args.size} to 2,3,5,6)."
  23.     end
  24.     return if text.empty?
  25.     wmax = [text.map{|t| self.text_size(t).width}.max,rect.width].min
  26.     h = self.text_size(text[0]).height
  27.     hmax = [h*text.size,rect.height].min
  28.     y = rect.y+(rect.height-hmax)/2
  29.     x = align==0 ? rect.x : align==2 ? rect.x+rect.width-wmax : rect.x+(rect.width-wmax)/2
  30.     text.each_with_index{|t,i|
  31.       draw_text2(x,y+i*h,wmax,h,t,align)
  32.     }
  33.     self
  34.   end
  35. end

捕获.PNG (26.57 KB, 下载次数: 5)

捕获.PNG

作者: guoxiaomi    时间: 2017-6-18 06:12
会不会是,defined? 的参数是方法调用,而不是方法名(symbol对象),去掉defined?
后面的冒号看看
作者: 破碎记忆    时间: 2017-6-18 12:04
guoxiaomi 发表于 2017-6-18 06:12
会不会是,defined? 的参数是方法调用,而不是方法名(symbol对象),去掉defined?
后面的冒号看看 ...

嗯,确实是,大神啊。
作者: RyanBern    时间: 2017-6-18 19:10
以下内容来自 rubydoc.org
纠正一下猫叔的说法,defined? 是关键字不是一个方法,所以说“参数”不太合适。改成“语法”[Synopsis]比较好。
defined?

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:

def x; end
p defined?(x)            # "method"





欢迎光临 Project1 (https://rpg.blue/) Powered by Discuz! X3.1