有人能帮我理解下面列出的"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

其中包含的控制器是性能控制器.我一直在搜索谷歌,试图弄清楚那个"表演者"的目的是什么.send(selected_track)确实在做,但感觉就像是在漩涡中划船.

有人能帮我弄清楚这件事吗?

谢谢

推荐答案

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

至于具体的send调用将做什么,Performer类中很可能有一个def method_missing,用于捕获该调用并执行某些操作.

希望这有帮助,祝你好运!

Ruby-on-rails相关问答推荐

RubyOnRail在测试环境中启动失败

如何在不触发每次编译的情况下运行`bin/rails测试`?

Rails 7,返回具有两个单独条件的活动记录关联,一个在父级,一个在子级

关系 fields_for 上不允许的参数

Ruby Hash 中 tap / delete 和 except 的区别

Ruby on Rails 7 与 React 集成

Rails 3 应用程序的 MySQL 集群 (NDB) 与 MySQL 复制 (InnoDB):优点/缺点?

使用 rails 3.1.0 和 ubuntu 安装 Nokogiri 1.5.0 时出错

FactoryGirl 和 Rspec 测试中 attributes_for 的含义

在 Windows 上使用 Ruby 进行开发

在 Ruby on Rails 中获取空临时目录的最佳方法是什么?

使用 ActiveRecord 3 / Arel 查找单个记录的最佳方法?

搭建脚手架时建立关系

是否有不涉及删除 Gemfile.lock 的在任何源中找不到 *gem*错误的修复?

Rspec 与 TestUnit

如何为rails中的number_field添加默认值?

如何测试自定义验证器?

Rails - 如何在用户登录时覆盖设计 SessionsController 以执行特定任务?

Sidekiq Rails 4.2 使用 Active Job 还是 Worker?有什么不同

为什么 Rails 需要 JavaScript 运行时?