Upcoming SlideShare
×

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.
Standard text messaging rates apply

# Ruby singleton class

458
views

Published on

Published in: Technology, Education

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total Views
458
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
10
0
Likes
0
Embeds 0
No embeds

No notes for slide
• \n
• 2 array instantsi, meetod &amp;#x201C;hello&amp;#x201D; defineeritakse ainult instants &amp;#x201C;a&amp;#x201D;-le\n
• Siin on j&amp;#xE4;llegi n&amp;#xE4;ha kuidas &amp;#x201C;muki&amp;#x201D; instantsil defineeritakse &amp;#xFC;le meetod &amp;#x201C;speak&amp;#x201D;\n\nStaatilistest keeltest tulenev l&amp;#xE4;henemine reeglina ette, et on olemas klassid milledel defineeritakse meetodid mida selle klassi objektid kasutada saavad.\n\nKuhu antud juhul &amp;#x201C;muki.speak&amp;#x201D; meetod defineeritakse?\n\nKui see definitsioon tehtaks &amp;#x201C;muki&amp;#x201D; klassile milleks on &amp;#x201C;Dog&amp;#x201D; siis peaks &amp;#x201C;speak&amp;#x201D; meetodi defineerimine laienema ka instantsile &amp;#x201C;puff&amp;#x201D;\n\n\n
• muki instantsile luuakse Singleton klass sellel hetkel kui Ruby interpreter n&amp;#xE4;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&amp;#xE4;htamatu p&amp;#xE4;rinevuse jadas\n2. Singleton klassi ei saa instantiate&amp;#x2019;ida\n\n\n\n\n\n
• Singleton klassi omadused:\n\n1. N&amp;#xE4;htamatu p&amp;#xE4;rinevuse jadas\n2. Singleton klassi ei saa instantiate&amp;#x2019;ida\n\nTODO: show inheritance chain\n\n\n\n
• 1. Ruby&amp;#x2019;s on k&amp;#xF5;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&amp;#xF5;ikidele objektidele k&amp;#xE4;ttesaadav.\n\nSeega tulenevalt sellest kuidas Ruby method dispatch t&amp;#xF6;&amp;#xF6;tab luuakse objekti A p&amp;#xE4;rinevusejadasse &amp;#x2018;A kus defineeritakse tema klassimeetodid.\n
• K&amp;#xF5;ige lihtsam n&amp;#xE4;ide instantsi spetsiifilisest loogikast on Testides meetodite mock&amp;#x2019;imine\n\nLisaks:\nSingleton class on &amp;#xFC;heks talaks mis teeb Ruby&amp;#x2019;st high-level metaprogrammeerimise keele.\nMida see t&amp;#xE4;hendab on see, et see v&amp;#xF5;imaldab koodi kirjutada \nabstraktselt, domeenispetsiifiliselt, lihtsamalt, loogilisemalt\n\n\n\n\n\n
• K&amp;#xF5;ige lihtsam n&amp;#xE4;ide instantsi spetsiifilisest loogikast on Testides meetodite mock&amp;#x2019;imine\n\nLisaks:\nSingleton class on &amp;#xFC;heks talaks mis teeb Ruby&amp;#x2019;st high-level metaprogrammeerimise keele.\nMida see t&amp;#xE4;hendab on see, et see v&amp;#xF5;imaldab koodi kirjutada \nabstraktselt, domeenispetsiifiliselt, lihtsamalt, loogilisemalt\n\n\n\n\n\n\n\n\n\n\n
• K&amp;#xF5;ige lihtsam n&amp;#xE4;ide instantsi spetsiifilisest loogikast on Testides meetodite mock&amp;#x2019;imine\n\nLisaks:\nSingleton class on &amp;#xFC;heks talaks mis teeb Ruby&amp;#x2019;st high-level metaprogrammeerimise keele.\nMida see t&amp;#xE4;hendab on see, et see v&amp;#xF5;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 deﬁne 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