SORU
1 AĞUSTOS 2009, CUMARTESİ


Göster(&:adı Ruby anlamı nedir?

a RailsCast bu kodu buldum:

def tag_names
  @tag_names || tags.map(&:name).join(' ')
end

map(&:name) (&:name) ne anlama geliyor?

CEVAP
1 AĞUSTOS 2009, CUMARTESİ


tags.map(&:name.to_proc).join(' ') steno

Eğer foo bir nesne ile to_proc yöntem, daha sonra geçmek için bir yöntem olarak &foo, çağrı foo.to_proc ve kullanan olarak yöntemin blok.

Symbol#to_proc Bu yöntem aslında ActiveSupport tarafından eklendi ama Ruby 1.8.7 entegre olmuştur. Bu uygulanması

class Symbol
  def to_proc
    Proc.new do |obj, *args|
      obj.send self, *args
    end
  end
end

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Excel Video Tutorials

    Excel Video

    6 Aralık 2012
  • Matthew Pearce

    Matthew Pear

    9 AĞUSTOS 2009
  • the one am radio

    the one am r

    6 Mayıs 2006