Your SlideShare is downloading. ×
Ruby singleton class
Ruby singleton class
Ruby singleton class
Ruby singleton class
Ruby singleton class
Ruby singleton class
Ruby singleton class
Ruby singleton class
Ruby singleton class
Ruby singleton class
Ruby singleton class
Ruby singleton class
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Ruby singleton class

468

Published on

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
468
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
10
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide
  • \n
  • 2 array instantsi, meetod “hello” defineeritakse ainult instants “a”-le\n
  • Siin on jällegi näha kuidas “muki” instantsil defineeritakse üle meetod “speak”\n\nStaatilistest keeltest tulenev lähenemine reeglina ette, et on olemas klassid milledel defineeritakse meetodid mida selle klassi objektid kasutada saavad.\n\nKuhu antud juhul “muki.speak” meetod defineeritakse?\n\nKui see definitsioon tehtaks “muki” klassile milleks on “Dog” siis peaks “speak” meetodi defineerimine laienema ka instantsile “puff”\n\n\n
  • muki instantsile luuakse Singleton klass sellel hetkel kui Ruby interpreter näeb vajadust instantsi spetsiifiliseks loogikaks\n
  • Mis tegelikult juhtub klassi meetodi defineerimisel on see, et see meetod defineeritakse samuti klass A singleton klassi\n\nMiks see nii on?\n\n\n\n\n\n\n\n\n\n
  • Singleton klassi omadused:\n\n1. Nähtamatu pärinevuse jadas\n2. Singleton klassi ei saa instantiate’ida\n\n\n\n\n\n
  • Singleton klassi omadused:\n\n1. Nähtamatu pärinevuse jadas\n2. Singleton klassi ei saa instantiate’ida\n\nTODO: show inheritance chain\n\n\n\n
  • 1. Ruby’s on kõik objekt\n\n2. Ruby vajab klassimeetodeid kuid toetab ainult instantsimeetodeid.\n\nA on klassi Class subklass. Objekt-orienteeritust silmas pidades peaks objekti A klassimeetod defineeritama tema superklassi. Seda ei saa aga defineerida klassi Class kuna sellisel juhul oleks antud meetod kõikidele objektidele kättesaadav.\n\nSeega tulenevalt sellest kuidas Ruby method dispatch töötab luuakse objekti A pärinevusejadasse ‘A kus defineeritakse tema klassimeetodid.\n
  • Kõige lihtsam näide instantsi spetsiifilisest loogikast on Testides meetodite mock’imine\n\nLisaks:\nSingleton class on üheks talaks mis teeb Ruby’st high-level metaprogrammeerimise keele.\nMida see tähendab on see, et see võimaldab koodi kirjutada \nabstraktselt, domeenispetsiifiliselt, lihtsamalt, loogilisemalt\n\n\n\n\n\n
  • Kõige lihtsam näide instantsi spetsiifilisest loogikast on Testides meetodite mock’imine\n\nLisaks:\nSingleton class on üheks talaks mis teeb Ruby’st high-level metaprogrammeerimise keele.\nMida see tähendab on see, et see võimaldab koodi kirjutada \nabstraktselt, domeenispetsiifiliselt, lihtsamalt, loogilisemalt\n\n\n\n\n\n\n\n\n\n\n
  • Kõige lihtsam näide instantsi spetsiifilisest loogikast on Testides meetodite mock’imine\n\nLisaks:\nSingleton class on üheks talaks mis teeb Ruby’st high-level metaprogrammeerimise keele.\nMida see tähendab on see, et see võimaldab koodi kirjutada \nabstraktselt, domeenispetsiifiliselt, lihtsamalt, loogilisemalt\n\n\n\n\n\n\n\n\n\n\n
  • \n
  • Transcript

    • 1. Ruby’s Singleton Class
    • 2. You can define methods for a single instancea = Array.newb = Array.newdef a.hello puts "Hello Devclub!"enda.hello# => "Hello Devclub!"b.hello# => NoMethodError: undefined method hello for []:Array
    • 3. Or override a method on a single instanceclass Dog def speak puts "bark" endendmuki = Dog.newdef muki.speak puts "auh"endmuki.speak# => "auh"puff = Dog.newpuff.speak# => "bark"
    • 4. Enters the Singleton class class puff Dog class muki super supermuki Dog Singleton classmuki.singleton_methods# => ["speak"]puff.singleton_methods# => []
    • 5. class A def self.hello puts "Hello from class method!" endendA.hello# => "Hello from class method!"A.singleton_methods# => ["hello"]
    • 6. A.class# => ClassA.superclass# => Object class A Class super A Object method dispatch A A Object
    • 7. a_meta = class << A; self; end;puts a_meta# => #<Class:A>meta_instance = a_meta.new# => TypeError: cant create instance of virtualclass
    • 8. Everything in Ruby is an object1.class# => Fixnumnil.class# => NilClassNilClass.class# => ClassClass.class# => Classclass A puts self.classend# => Class
    • 9. Practical usesclass ApiClient attr_accessor :session def get_session # http request end def user session.user endend
    • 10. Practical usesclass ApiClientTest < Test::Unit::TestCase def setup @api_client = ApiClient.new def @api_client.get_session @api_client.session = Session.new(:user => "Mati") end end def test_user_mati @api_client.get_session assert(@api_client.user == Mati) end def test_user_nil assert(@api_client.user.nil?) endend
    • 11. Questions?
    • 12. ESTYLE comp LIF an AL y IT G DI We are looking for theCA N N E D Android Developers AP S P anton@cannedapps.com

    ×