申请人是否应该“要求'mad_skills'”或“包含'mad_skills'”?

时间:2009-09-10 15:52:40

标签: ruby inheritance include

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

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

1 个答案:

答案 0 :(得分:3)

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。