send() 方法的用途是什么?

发布于 2024-12-11 15:05:51 字数 2138 浏览 0 评论 0原文

有人可以帮助我理解下面列出的“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]
  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

执行者模型

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

该控制器包含在性能中。我一直在谷歌上搜索,试图找出“@performer.send(selected_track)”的实际用途,但感觉就像在漩涡中划船。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

━╋う一瞬間旳綻放 2024-12-18 15:05:51

send 方法的 Ruby 实现(用于向对象发送方法消息)的工作原理如下:

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

这是基础知识,另外一条重要信息是 send 将允许您发送消息发送到私有方法,而不仅仅是公共方法。

@car.engine_temp  # This doesn't work, it will raise an exception
@car.send(:engine_temp)  # output: Just Right

至于您的特定发送调用将执行的操作,Performer 类中很可能有一个 def method_missing,它被设置为捕获该调用并执行某些操作。

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.

无戏配角 2024-12-18 15:05:51

send 用于将方法(和参数)传递给对象。当您事先不知道方法的名称时,它非常方便,因为它仅表示为字符串或符号。

例如: Performer.find(params[:performer_id])Performer.send(:find, params[:performer_id]) 相同

,请注意,因为在以下情况下依赖 params使用 send 可能很危险:如果用户传递 destroydelete 会怎样?它实际上会删除你的对象。

send is used to pass a method (and arguments) to an object. It's really handy when you don't know in advance the name of the method, because it's represented as a mere string or symbol.

Ex: Performer.find(params[:performer_id]) is the same as Performer.send(:find, params[:performer_id])

Beware here because relying on params when using send could be dangerous: what if users pass destroy or delete? It would actually delete your object.

北风几吹夏 2024-12-18 15:05:51

send 方法相当于调用对象上的给定方法。因此,如果 selected_track 变量的值为 1234,则 @performer.send(selected_track)@performer.1234 相同。或者,如果 selected_track 是“a_whiter_shade_of_pale”,那么就像调用 @performer.a_whiter_shade_of_pale 一样。

那么,Performer 类大概会重写 method_missing ,这样您就可以使用任何曲目(名称或 ID,从上面不清楚)来调用它,并且它会将其解释为对该曲目的搜索该表演者的曲目中的曲目。

The send method is the equivalent of calling the given method on the object. So if the selected_track variable has a value of 1234, then @performer.send(selected_track) is the same as @performer.1234. Or, if selected_track is "a_whiter_shade_of_pale" then it's like calling @performer.a_whiter_shade_of_pale.

Presumably, then, the Performer class overrides method_missing such that you can call it with any track (name or ID, it isn't clear from the above), and it will interpret that as a search for that track within that performer's tracks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文