我有一个字符串,如hello _there_.我想,以取代两个下划线<div>并</div>分别使用JavaScript的.输出(因此)看起来像hello <div>there</div>.该字符串可能包含多对下划线.
我所寻找的是一个方法来无论是运行在每场比赛,红宝石做它的方式的函数:
"hello _there_".gsub(/_.*?_/) { |m| "<div>" + m[1..-2] + "</div>" }
Run Code Online (Sandbox Code Playgroud)
或者能够引用匹配的组,再次以红宝石的方式完成:
"hello _there_".gsub(/_(.*?)_/, "<div>\\1</div>")
Run Code Online (Sandbox Code Playgroud)
任何想法或建议?
我有一个用ruby编写的应用程序(在JRuby VM中运行).在分析它时,我意识到它花了很多时间(实际上几乎全部)将一些哈希转换成哈希JSON.
这些哈希包含符号键,其他类似哈希值,数组,字符串和数字.
是否有适合此类输入的序列化方法,并且通常比JSON运行得更快?如果它也具有Java或JRuby兼容的gem,那将是更好的选择.
我目前正在使用jruby-jsongem,这是JSONJRuby中最快的实现(据我所知),因此移动很可能是一个不同的序列化方法,而不仅仅是一个不同的库.
任何帮助表示赞赏!谢谢.
是否有一个版本的requireruby可以加载整个文件,或者什么也没有?
问题是需要从顶部开始加载,如果它遇到问题,你最终得到未完成的定义,例如,class A即使module C未定义,以下仍然会加载:
class B
include C
end
Run Code Online (Sandbox Code Playgroud)
在我的特定情况下,我有一大堆相互依赖的文件,以及一个加载这些文件的加载器.举例来说,我将文件集简化为4个文件(a.rb,b.rb,c.rb和w.rb).以下是这些文件的列表:
# In file a.rb
class A
@foo = []
@foo.push("in A")
def self.inherited(subclass)
foo = @foo.dup
subclass.instance_eval do
@foo = foo
end
end
def self.get_foo
@foo
end
end
# In file b.rb
class B < A
include C # if C is not already defined, the following line will not get executed although B will be defined.
@foo.push("in B")
end
# In file c.rb …Run Code Online (Sandbox Code Playgroud)