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:
- http://DABlog.RubyPAL.Com/2008/3/25/a-short-circuit-edge-case/
- http://DABlog.RubyPAL.Com/2008/3/26/short-circuit-post-correction/
- http://ProcNew.Com/ruby-short-circuit-edge-case-response.html
Bu işaret için W Mittag Jörg için teşekkürler.
CEVAP
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' ]
Ruby'de String birleştirme...
Raylar üzerinde Ruby belirleyici bir y...
Arasındaki fark "veya" Ruby ...
Ruby: tarih dizesi dönüştürmek...
Ruby, 'seçin birleştiren bir Dizi...