يجب على الطبقة الطلب "تتطلب" mad_skills "أو" تشمل "mad_skills"؟

StackOverflow https://stackoverflow.com/questions/1406123

  •  05-07-2019
  •  | 
  •  

سؤال

وأيضا، ما يعني "self.send attr" تفعل؟ وattr يفترض أن يكون متغير المثال الخاص من فئة ActiveEngineer؟ هل هناك أي قضايا أخرى مع هذا الرمز من حيث المنطق روبي؟

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
هل كانت مفيدة؟

المحلول

وrequire 'mad_skills' يحمل رمز في mad_skills.rb (أو أنه يحمل mad_skills.so/.dll اعتمادا على أي واحد موجود). تحتاج إلى تتطلب الملف قبل التمكن من استخدام الطبقات وطرق الخ المحددة في هذا الملف (وإن كان في القضبان يتم تحميل الملفات تلقائيا عند محاولة الوصول إلى الفئات التي لها نفس اسم الملف). وضع يتطلب داخل تعريف فئة، لا يغير سلوكها على الإطلاق (أي وضعه في الجزء العلوي من الملف لن تحدث فرقا).

وinclude MadSkills يأخذ MadSkills وحدة ويشمل ذلك إلى سلسلة الميراث Applicant ل، أي أنه يجعل كل الطرق في MadSkills متاحة للحالات Applicant.

وself.send attr يقوم بتنفيذ الأسلوب مع الاسم المحدد في ATTR على النفس وإرجاع قيمة عودتها. مثلا سوف attr = "hello"; self.send(attr) تكون هي نفسها كما self.hello. في هذه الحالة يقوم بتنفيذ أساليب smart، highly_productive، curious، driven، وteam_player والشيكات أن جميع من لهم بالعودة صحيح.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top