【问题标题】:Should the Applicant class "require 'mad_skills'" or "include 'mad_skills'"?申请人类应该“要求'mad_skills'”还是“包括'mad_skills'”?
【发布时间】:2010-11-27 05:27:15
【问题描述】:

另外,“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

【问题讨论】:

  • 任何认为学位是必不可少的潜在雇主可能都不值得为之工作。 “代码”在几个地方都是可疑的,足以再次表明他们只是不明白。极客对这些事情有幽默感,但这并没有击中它。我想知道 ":include=>:mostly_women" 到底是什么意思?
  • 我认为“:include=>:mostly_women”意味着这个雇主生产的任何产品都有数百万订阅者,其中大多数是女性。

标签: ruby inheritance include


【解决方案1】:

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。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-17
    • 2011-02-12
    • 1970-01-01
    • 1970-01-01
    • 2019-12-03
    • 1970-01-01
    • 2015-04-28
    • 2015-07-01
    相关资源
    最近更新 更多