首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >申请类应该是"require 'mad_skills'“还是"include 'mad_skills'"?

申请类应该是"require 'mad_skills'“还是"include 'mad_skills'"?
EN

Stack Overflow用户
提问于 2009-09-10 15:52:40
回答 1查看 70关注 0票数 0

另外,"self.send attr“是做什么的?是否假设attr是ActiveEngineer类的私有实例变量?在Ruby逻辑方面,这段代码还有其他问题吗?

代码语言:javascript
复制
class Applicant < ActiveEngineer

  require 'ruby'
  require 'mad_skills'
  require 'oo_design'
  require 'mysql'

  validates :bachelors_degree

  def qualified?
    [:smart, :highly_productive, :curious, :driven, :team_player ].all? do
|attr|
      self.send attr
    end
  end
end

class Employer
  include TopTalent
  has_millions :subscribers, :include=>:mostly_women
  has_many :profits, :revenue
  has_many :recent_press, :through=>[:today_show, :good_morning_america,
                                     :new_york_times, :oprah_magazine]
  belongs_to :south_park_sf
  has_many :employees, :limit=>10

  def apply(you)
    unless you.build_successful_startups
      raise "Not wanted"
    end
    unless you.enjoy_working_at_scale
      raise "Don't bother"
    end
  end

  def work
    with small_team do
      our_offerings.extend you
      subscribers.send :thrill
      [:scaling, :recommendation_engines, :   ].each do |challenge|
        assert intellectual_challenges.include? challenge
      end
      %w(analytics ui collaborative_filtering scraping).each{|task|
task.build }
    end
  end

end

def to_apply
  include CoverLetter
  include Resume
end
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2009-09-10 16:04:16

require 'mad_skills'加载mad_skills.rb中的代码(或者它加载mad_skills.so/.dll,这取决于存在哪一个)。在使用该文件中定义的类、方法等之前,您需要一个文件(尽管在rails中,当试图访问与文件同名的类时,会自动加载该文件)。将require放在类定义中,根本不会改变它的行为(即把它放在文件的顶部不会有什么不同)。

include MadSkills接受模块MadSkills并将其包含在Applicant的继承链中,即它使MadSkills中的所有方法都可用于Applicant的实例。

self.send attr使用在attr on self中指定的名称执行方法,并返回其返回值。例如,attr = "hello"; self.send(attr)将与self.hello相同。在本例中,它执行方法smarthighly_productivecuriousdriventeam_player,并检查它们是否都返回true。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1406123

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档