【发布时间】:2017-01-15 02:21:51
【问题描述】:
我目前正在使用 Jekyll 制作我的博客。 Jekyll 自定义插件使用 Ruby,而 Jekyll 使用 Liquid。我目前正在通过自定义液体标签获取输入并在那里进行处理。我想检查字符串是否包含整数。所以我有以下代码。我意识到输入不是 String 类型,而是 Jekyll::Token 类型。所以我将输入更改为字符串,但我无法检测字符串是否包含整数。这是我的代码:
module Jekyll
class TypecheckTag < Liquid::Tag
def is_int(word)
return word.count("0-3000") > 0
end
def initialize(tag_name, word, tokens)
super
@word = word.to_s
end
def render(context)
if /\A\d+\z/.match(@word)
@result = 'int'
else
@result = 'string'
end
end
end
end
Liquid::Template.register_tag('typecheck', Jekyll::TypecheckTag)
不幸的是,即使我们有一个字符串“16”,它也总是返回'string'。
【问题讨论】: