`
messi_18
  • 浏览: 96491 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

eval in Ruby

    博客分类:
  • ruby
 
阅读更多
今天把之前做的一个工具集成到我的一个工具框架中,结果遇到了问题。我的工具和框架都是用ruby写的,框架通过eval方法调用注册的工具。而我的工具中有一个对于Array的monkey patch。
class Array do
	alias old_zip zip
	def zip(another_array)
		if another_array and another_array.size > size
			(another_array.size-size).times{ self << nil }
		end
		
		old_zip(another_array)
	end
end


我单独运行工具的时候,一切正常。但是,集成之后却报一个错:module Tools.Methods.Array 没有方法zip。

原来是我在一个自定义的module中调用的eval.而传入的string就是我的那个工具原文件,这样看来,monkey patch没有找到Array这样一个类。所以也就无法找到一个zip方法。
我把代码改成这样就好了:
Array.class_eval do
	alias old_zip zip
	def zip(another_array)
		if another_array and another_array.size > size
			(another_array.size-size).times{ self << nil }
		end
		
		old_zip(another_array)
	end
end
分享到:
评论

相关推荐

    indifferent_eval:Ruby 库 DSL 的块评估模式

    当您制作 Ruby DSL 时,您通常必须在以下各项之间做出决定: # Use a block variable to access the MyConfig instance. # This gives you the benefit of having access to the outer "self" inside of the block...

    详解Ruby中的instance_eval方法及其与class_eval的对比

    instance_eval方法 这个BasicObject#instance_eval有点类似JS中的bind方法,不同的时,bind是将this传入到对象中,而instance_eval则是将代码块(上下文探针Context Probe)传入到指定的对象中,一个是传对象,一个是...

    ruby语法基础教程

    §12.3.3 使用eval方法 88 §12.3.4 性能 88 §12.4 Hook和回调方法 89 §12.4.1 什么是Hook 89 §12.4.2 Ruby中的Hook 89 §11.4.2 回调方法 90 §12.5 跟踪程序的运行 90 §12.5.1 set_trace_func 90 §12.5.2 ...

    ruby 异常处理:rescue

    ruby&gt; file = open(“some_file”) ERR: (eval):1:in `open’: No such file or directory – some_file 一个健壮的程序会合理并漂亮的处理这些问题.面对那些异常是一件讨人厌的工作.C程序员被要求...

    wo:用于wo服务器的CLI和Ruby客户端

    用于CLI和Ruby客户端 指令说明 将此行添加到您的应用程序的Gemfile中: gem 'wo' 然后执行: $ bundle 或将其自己安装为: $ gem install wo 另外,请复制以下内容 zsh的情况 eval "$(wo hook zsh)" 案例 去...

    GVim 7.2.441

    +emacs_tags +eval +ex_extra +extra_search +farsi +file_in_path +find_in_path +float +folding -footer +gettext/dyn -hangul_input +iconv/dyn +insert_expand +jumplist +keymap +langmap +libcall +...

    带lua支持的gvim编辑器

    +file_in_path +find_in_path +float +folding -footer +gettext/dyn -hangul_input +iconv/dyn +insert_expand +jumplist +keymap +langmap +libcall +linebreak +lispindent +listcmds +localmap +lua +menu +...

    gvim 7.4.110 for Windows 64 位,带 +lua 及 Lua 库(开箱即用)

    脚本语言支持有:Python 2.7、Python 3.3、Ruby 2.0.0、Lua 5.2(只需要能找到 lua52.dll 文件已经自带 lua52.dll 文件,方便各位使用 neocomplete 插件)。 md5sum: 1acd40c636111e838e0ec20792c04ff3 VIM - Vi ...

    gvim 7.4.110 for Windows 32 位,带 +lua 及 Lua 库(开箱即用)

    脚本语言支持有:Python 2.7、Python 3.3、Ruby 2.0.0、Lua 5.2(只需要能找到 lua52.dll 文件已经自带 lua52.dll 文件,方便各位使用 neocomplete 插件)。 md5sum: 2cfd2cbc8048c6b2c5483248e02e7ad0 VIM - Vi ...

    hadoop_the_definitive_guide_3nd_edition

    Ruby 37 Python 40 iii www.it-ebooks.info Hadoop Pipes 41 Compiling and Running 42 3. The Hadoop Distributed Filesystem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 ...

    scheherazade:Rails 的娱乐装置

    穿得不多(最小的 DSL,没有instance_eval ) 简单示例 # Say we have a model like: class Department belongs_to :company has_many :employees validates_presence_of :company, :name end # Without any ...

Global site tag (gtag.js) - Google Analytics