English 中文(简体)
鲁比拉/拉勒斯:班级不愿意评价该法典
原标题:Ruby/Rails: class_eval doesn t want to evaluate this code

To generate mocks for Omniauth, I Added this method to config/environments/development.rb

  def provides_mocks_for(*providers)
    providers.each do |provider|
      class_eval %Q{
        OmniAuth.config.add_mock(provider, {
          :uid =>  123456 ,
          :provider => provider,
          :nickname =>  nickname ,
          :info => {
             email  => "#{provider}@webs.com",
             name  =>  full_name_  + provider
          }
        })
      }
    end
  end

然后,我在同一档案中发言:

provides_mocks_for :facebook, :twitter, :github, :meetup

But I get:

3.1.3/lib/active_support/core_ext/kernel/singleton_class.rb:11:in `class_eval : can t create instance of singleton class (TypeError)
最佳回答

class_evaland module_eval (which are equivalent) are used to evaluate and immediately execute strings as Ruby code. As such they can be used as a meta-programming facility to dynamically create methods. An example is

class Foo
  %w[foo bar].each do |name|
    self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
      def #{name}
        puts  #{name} 
      end
    RUBY
  end
end

该系统将采用两种方法,即:www.c.org/index.html。 如你所见,我创设了一个载有该职能实际来源守则的示意图,将其通过到“条形状”栏。

虽然这是执行动态制定的守则的非常能干的工具,但必须非常谨慎地加以利用。 如果你在此犯错误,BAD THINGS WILL HAPPENTM。 例如,如果你在生成代码时使用用户提供的价值,那么really确保这些变量只包含预期值。 平均功能通常应作为最后的手段使用。

更清洁和普遍喜欢的备选案文是使用<代码>define_method。 同样:

class Foo
  %w[foo bar].each do |name|
    define_method name do
      puts name
    end
  end
end

(注:MRI是一种比值变量更快的比值。) 但是,与增加的安全和清晰度相比,这只是大部分时间。

现在,在您的法典中,你将守则有效写成可以直接操作的扼杀。 此处使用<代码>}_eval,导致在顶端物体(Kernel)的情况下进行扼杀。 由于这是一种特殊的单一州物体,可以加以实现(类似于零、真实和虚假),因此,你会发现这一错误。

然而,由于你制定了直接可执行守则,你无需使用<条码>_eval。 (或任何形式) 只是像现在一样,把你的法典放在一起。 永远记住:光价公式变数(计算方法属于最坏)只应作为最后的手段使用。

问题回答

暂无回答




相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签