【发布时间】:2014-06-11 02:37:23
【问题描述】:
我正在尝试使用随机生成的类型运行不同的代码。创建随机类型 (self.type) 有效。但是,我想使用这些类型:Early、Late 或 On Time 根据返回的字符串 self.type 有条件地运行代码。提前致谢!
require 'rubygems'
require 'forgery'
class Forgery::MyRecord< Forgery
TYPES= Forgery::Extend([
{ :type => "Early" },
{ :type => "Late" },
{ :type => "On Time" }
])
def self.type
TYPES.random[:type]
end
a=self.type
puts a
这行代码有效。它随机返回Early、Late 或On Time。
但是,我如何在我的方法中使用type?谢谢!
def self.deadline
if self.type=="Early"
puts "early"
#removed other code
elsif if self.type=="Late"
puts "late"
#removed other code
elsif if self.type=="On Time"
puts "on time"
#removed other code
else
puts "Missing"
end
end
b = Forgery::MyRecord.deadline
puts b
end
结束
【问题讨论】:
标签: ruby string methods random compare