SORU
5 Mart 2009, PERŞEMBE


Ortak Ruby Cümleler

Ruby hakkında sevdiğim tek şey çoğunlukla çok okunabilir bir dil kendi kendine tanım kod için büyük olan) olmasıdır

Ancak, bu soruya ilham: http://stackoverflow.com/questions/609612/ruby-code-explained ||= ruby nasıl çalıştığını açıklaması, açıkçası, tam olarak onları grok Yok Kullanmıyorum ruby cümleler düşünüyordum.

Benim sorum, başvurulan soru örneğe benzer, ne ortak, ama açık, ruby cümleler haberdar olmak için gerçekten yeterli bir ruby programcısı olmam gerekiyor mu?

Başvurulan soru bu arada

a ||= b

olur denk

if a == nil || a == false
  a = b
end

(Düzeltme için Ian Terrell için teşekkürler)

Düzenleme:Bu noktada tamamen sorunsuz değilmiş. Doğru genişleme aslında

(a || (a = (b)))

Neden bu bağlantılara bakın:

Bu işaret için W Mittag Jörg için teşekkürler.

CEVAP
5 Mart 2009, PERŞEMBE


Sihirli aynı dosyayı bir kütüphane olarak hizmet sağlayan veya bir komut: ıf

if __FILE__ == $0
  # this library may be run as a standalone script
end

Toplama ve açma diziler:

# put the first two words in a and b and the rest in arr
a,b,*arr = *%w{a dog was following me, but then he decided to chase bob}
# this holds for method definitions to
def catall(first, *rest)
  rest.map { |word| first   rest }
end
catall( 'franken', 'stein', 'berry', 'sense' ) #=> [ 'frankenstein', 'frankenberry', 'frankensense' ]

Yöntem bağımsız değişkenleri olarak sağlamaları için syntatical şeker

this(:is => :the, :same => :as)
this({:is => :the, :same => :as})

Karma başlatıcılar:

# this
animals = Hash.new { [] }
animals[:dogs] << :Scooby
animals[:dogs] << :Scrappy
animals[:dogs] << :DynoMutt
animals[:squirrels] << :Rocket
animals[:squirrels] << :Secret
animals #=> {}
# is not the same as this
animals = Hash.new { |_animals, type| _animals[type] = [] }
animals[:dogs] << :Scooby
animals[:dogs] << :Scrappy
animals[:dogs] << :DynoMutt
animals[:squirrels] << :Rocket
animals[:squirrels] << :Secret
animals #=> {:squirrels=>[:Rocket, :Secret], :dogs=>[:Scooby, :Scrappy, :DynoMutt]}

metaclass sözdizimi

x = Array.new
y = Array.new
class << x
  # this acts like a class definition, but only applies to x
  def custom_method
     :pow
  end
end
x.custom_method #=> :pow
y.custom_method # raises NoMethodError

sınıf örnek değişkenler

class Ticket
  @remaining = 3
  def self.new
    if @remaining > 0
      @remaining -= 1
      super
    else
      "IOU"
    end
  end
end
Ticket.new #=> Ticket
Ticket.new #=> Ticket
Ticket.new #=> Ticket
Ticket.new #=> "IOU"

Bloklar, yordamlara ve Lambda. Canlı ve nefes.

 # know how to pack them into an object
 block = lambda { |e| puts e }
 # unpack them for a method
 %w{ and then what? }.each(&block)
 # create them as needed
 %w{ I saw a ghost! }.each { |w| puts w.upcase }
 # and from the method side, how to call them
 def ok
   yield :ok
 end
 # or pack them into a block to give to someone else
 def ok_dokey_ok(&block)
    ok(&block)
    block[:dokey] # same as block.call(:dokey)
    ok(&block)
 end
 # know where the parentheses go when a method takes arguments and a block.
 %w{ a bunch of words }.inject(0) { |size,w| size   1 } #=> 4
 pusher = lambda { |array, word| array.unshift(word) }
 %w{ eat more fish }.inject([], &pusher) #=> ['fish', 'more', 'eat' ]

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

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Glove and Boots

    Glove and Bo

    1 ŞUBAT 2007
  • GoProTutorials

    GoProTutoria

    18 NİSAN 2011
  • SRT Photoshop Tutorials

    SRT Photosho

    19 Aralık 2012