Ruby’s Singleton Class
You can define methods for a single instancea = Array.newb = Array.newdef a.hello  puts "Hello Devclub!"enda.hello# => "Hel...
Or override a method on a single instanceclass Dog  def speak    puts "bark"  endendmuki = Dog.newdef muki.speak  puts "au...
Enters the Singleton class                            class                  puff                     Dog          class  ...
class A  def self.hello    puts "Hello from class method!"  endendA.hello# => "Hello from class method!"A.singleton_method...
A.class# => ClassA.superclass# => Object                      class               A                     Class             ...
a_meta = class << A; self; end;puts a_meta# => #<Class:A>meta_instance = a_meta.new# => TypeError: cant create instance of...
Everything in Ruby is an object1.class# => Fixnumnil.class# => NilClassNilClass.class# => ClassClass.class# => Classclass ...
Practical usesclass ApiClient  attr_accessor :session  def get_session    # http request  end  def user    session.user  e...
Practical usesclass ApiClientTest < Test::Unit::TestCase  def setup    @api_client = ApiClient.new    def @api_client.get_...
Questions?
ESTYLE comp                      LIF           an                   AL                 y                 IT          G    ...
Upcoming SlideShare
Loading in...5
×

Ruby singleton class

493

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
493
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
10
Comments
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
  • Ruby singleton class

    1. 1. Ruby’s Singleton Class
    2. 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. 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. 4. Enters the Singleton class class puff Dog class muki super supermuki Dog Singleton classmuki.singleton_methods# => ["speak"]puff.singleton_methods# => []
    5. 5. class A def self.hello puts "Hello from class method!" endendA.hello# => "Hello from class method!"A.singleton_methods# => ["hello"]
    6. 6. A.class# => ClassA.superclass# => Object class A Class super A Object method dispatch A A Object
    7. 7. a_meta = class << A; self; end;puts a_meta# => #<Class:A>meta_instance = a_meta.new# => TypeError: cant create instance of virtualclass
    8. 8. Everything in Ruby is an object1.class# => Fixnumnil.class# => NilClassNilClass.class# => ClassClass.class# => Classclass A puts self.classend# => Class
    9. 9. Practical usesclass ApiClient attr_accessor :session def get_session # http request end def user session.user endend
    10. 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. 11. Questions?
    12. 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
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×