另外,"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发布于 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相同。在本例中,它执行方法smart、highly_productive、curious、driven和team_player,并检查它们是否都返回true。
https://stackoverflow.com/questions/1406123
复制相似问题