Rails 100% newb 问题 - send() 方法 [英] Rails 100% newb issue - send() method
问题描述
有人可以帮我理解下面列出的send()"方法的用途吗?下面的代码,当我阅读它时,它的用途是没有意义的.
这是一个使用 Ruby 1.8.7 和 Rails 1.2.3 的 Rails 应用程序.升级的事情请不要骂我,这是客户的环境,我没有那种闲暇.
不过不用说,我所指的语句是这样的;
def do_schedule@performance = Performance.new(params[:performance])@performer = Performer.find(params[:performer_id])selected_track = params[:selected_track]如果 FileTest.exists?(File.expand_path(@performer.photo))@performance.photo = File.open(File.expand_path(@performer.photo))结尾@performance.audio = File.open(File.expand_path(@performer.send(selected_track)))如果@performance.saveflash[:notice] = '已安排表演者.'重定向到:控制器 =>:表演者,:动作=>:指数别的渲染:动作=>'日程'结尾结尾
<块引用>
表演模特
class Performer /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/ivalidates_confirmation_of :emailvalidates_presence_of :audio_one, :audio_two, :audio_three, :photo, :if =>:提交after_create :salt_access_key序列化:可用性序列化:阶段attr_accessor :other_type_of_musicbefore_save :set_other_typedef set_other_type如果 type_of_music == '其他'self.type_of_music = "Other - #{other_type_of_music}" 除非 other_type_of_music.blank?结尾结尾def salt_access_keyupdate_attribute(:access_key, Digest::SHA1.hexdigest("--#{self.id}--#{self.name}--#{self.festival.year}"))结尾def preferred_stages阶段 = []节日 = Festival.find(self.festival_id.to_i)self.stages.collect { |键,值 |id = key.gsub(/[\D]/, '').to_i如果 id >0阶段<
包含它的控制器是性能.我一直在搜索 Google,试图弄清楚 '@performer.send(selected_track)' 的实际目的是什么,但感觉就像在漩涡中划船.
有人可以帮助我更好地理解这一点吗?
谢谢.
send
方法的 Ruby 实现,用于向对象发送方法消息,其工作方式如下:>
class Car定义开始把vroom"结尾私人的def engine_temp把恰到好处"结尾结尾@car = Car.new@car.start # 输出:vroom@car.send(:start) # 输出:vroom
这是基础知识,另外一条重要信息是 send 将允许您将消息发送到 PRIVATE 方法,而不仅仅是公共方法.
@car.engine_temp # 这个不行,会抛出异常@car.send(:engine_temp) # 输出:恰到好处
至于您的特定发送调用将执行的操作,Performer
类中很可能有一个 def method_missing
用于捕获该调用并执行某些操作.
希望这会有所帮助,祝你好运!
Could someone please help me to understand what the 'send()' method listed below is used for? The code below, when I am reading it, makes no sense what purpose it's serving.
It's a Rails app using Ruby 1.8.7 with Rails 1.2.3. Please don't harp on me about upgrading, it's a client's environment, so I don't have that sort of leisure.
Needless to say though, the statement I am referring to is like this;
def do_schedule
@performance = Performance.new(params[:performance])
@performer = Performer.find(params[:performer_id])
selected_track = params[:selected_track]
if FileTest.exists?(File.expand_path(@performer.photo))
@performance.photo = File.open(File.expand_path(@performer.photo))
end
@performance.audio = File.open(File.expand_path(@performer.send(selected_track)))
if @performance.save
flash[:notice] = 'Performer scheduled.'
redirect_to :controller => :performer, :action => :index
else
render :action => 'schedule'
end
end
Performer Model
class Performer < ActiveRecord::Base
file_column :audio_one
file_column :audio_two
file_column :audio_three
file_column :photo
belongs_to :festival
validates_presence_of :name, :first_name, :last_name, :address, :city, :state, :zip, :daytime_phone, :availability, :stages
validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
validates_confirmation_of :email
validates_presence_of :audio_one, :audio_two, :audio_three, :photo, :if => :submitted
after_create :salt_access_key
serialize :availability
serialize :stages
attr_accessor :other_type_of_music
before_save :set_other_type
def set_other_type
if type_of_music == 'Other'
self.type_of_music = "Other - #{other_type_of_music}" unless other_type_of_music.blank?
end
end
def salt_access_key
update_attribute(:access_key, Digest::SHA1.hexdigest("--#{self.id}--#{self.name}--#{self.festival.year}"))
end
def preferred_stages
stages = []
festival = Festival.find(self.festival_id.to_i)
self.stages.collect { | key, value |
id = key.gsub(/[\D]/, '').to_i
if id > 0
stages << festival.performance_stages.find(id).name
end
}
return stages
end
end
The controller that this is contained in is Performance. I have been scouring Google trying to figure out what purpose that '@performer.send(selected_track)' is actually doing, but feel like I'm rowing against a whirlpool.
Can someone please help me make better sense out of this?
Thanks.
The Ruby implementation for the send
method, which is used to send a method message to an object, works like this:
class Car
def start
puts "vroom"
end
private
def engine_temp
puts "Just Right"
end
end
@car = Car.new
@car.start # output: vroom
@car.send(:start) # output: vroom
That's the basics, an additional piece of important information is that send will allow you you send in messages to PRIVATE methods, not just public ones.
@car.engine_temp # This doesn't work, it will raise an exception
@car.send(:engine_temp) # output: Just Right
As for what your specific send call will do, more than likely there is a def method_missing
in the Performer
class that is setup to catch that and perform some action.
Hope this helps, good luck!
这篇关于Rails 100% newb 问题 - send() 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!