SlideShare a Scribd company logo
1 of 36
Advanced Ruby Beyond the Basics
Blocks {   puts   'Hello'   } do puts   'Hello' end
Blocks def   call_block   puts   'Start of method'   # you can call the block using the yield keyword  yield   yield   puts   'End of method'   end   # invoke call_block   {puts   'In the block' }   >> Start of method In the block In the block End of method
Blocks def   call_block_with_params puts   'Start of method'   yield   'foo' ,   'bar' puts   'End of method'   end # invoke call_block_with_params {|a,b|   puts   "#{a} #{b}" } >> Start of method foo bar End of method
Exercise 1 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Proc objects p   =   Proc.new   {   puts   "Hello"   } p .call l   =   lambda   {   puts   "Hello"   } l .call
Proc vs lambda def   return_test l   =   lambda   {   return   } l .call puts   "Still here!" p   =   Proc.new   {   return   } p .call puts   "You won't see this." end return_test >> Still here!
Convert Blocks to Proc objects def   grab_block (&block) block .call end grab_block   {puts   "Hello" }
Higher Order Functions proc   =   lambda{puts   'Hello' } proc .call def   hello (proc) puts   "start of method" proc .call puts   "end of method" end # invoke hello   proc >> Hello >> start of method Hello end of method
Closures >> a = 1 @a = 2 original class   Holder def   call_block (pr) a   =   101 @a   =   102 pr .call end end class   Creator def   create_block a   =   1 @a   =   2 lambda   do puts   "a = #{a}" puts   "@a = #@a" puts   yield end end end block   =   Creator.new.create_block   {   "original"   } Holder .new.call_block   block
Metaprogramming ,[object Object],[object Object],[object Object],[object Object],[object Object]
Adding methods to a class class   Object def   greet puts   "Hello"   end end obj   =   Object.new obj .greet >> Hello obj2   =   Object.new obj2 .greet >> Hello
Adding methods to a class - example 3.hours .from_now class   Fixnum def   hours self   *   60   *   60 end def   from_now Time .now   +   self end end
Adding a method to an object obj   =   Object.new def   obj .greet puts   &quot;Hello&quot;   end obj .greet >> Hello obj2   =   Object.new obj2 .greet >> undefined method `greet' for #<Object:0x2bae594>
Singleton Class object Singleton class class module
Adding methods to singleton class s   =   &quot;Hello&quot; class   <<   s def   twice self   +   &quot; &quot;   +   self end end puts   s .twice >> Hello Hello def   s .twice self   +   &quot; &quot;   +   self end
Using modules module M def greet puts &quot;Hello&quot; end end obj = C.new class << obj include M end obj.greet obj = C.new obj.extend(M) obj.greet
Class method definitions class   String class   <<   self def   hello &quot;hello&quot; end end end class   <<   String def   hello &quot;hello&quot; end end def   String def   self .hello &quot;hello&quot; end end
Class methods and inheritance class C singleton class of C class D singleton class of D extends lookup path object
Exercise 2 ,[object Object],[object Object],[object Object]
Exercise 3 ,[object Object],[object Object],[object Object]
Method Aliasing class   C def   hi puts   &quot;hello&quot; end end class   C alias_method   :original_hi ,   :hi def   hi puts   &quot;greetings“ original_hi end end obj   =   C.new obj .hi >> greetings hello
Exercise 4 ,[object Object],[object Object],[object Object],[object Object]
Method_missing class   Echo def   method_missing   method_sym,   *args puts   &quot;#{method_sym}: #{args.inspect}&quot; end end Echo .new.yell   &quot;Hello&quot; ,   &quot;world!&quot; Echo .new.say   &quot;Good&quot; ,   &quot;bye!&quot; >> yell: [&quot;Hello&quot;, &quot;world!&quot;] say: [&quot;Good&quot;, &quot;bye!&quot;]
Exercise 5 ,[object Object],[object Object],[object Object]
Dynamically add methods >> &quot;defining method yell“ &quot;yell: [amp;quot;Helloamp;quot;, amp;quot;world!amp;quot;]“ &quot;yell: [amp;quot;goodamp;quot;, amp;quot;byeamp;quot;]&quot;
Dynamically add methods class   Echo def   method_missing   method_sym,   *args p   &quot;defining method #{method_sym}&quot; self .class.class_eval   <<-EOF def   #{method_sym.to_s} *args p   &quot;#{method_sym}: &quot;   +   args.inspect end EOF send (method_sym,   *args) end end Echo .new.yell   &quot;Hello&quot; ,   &quot;world!&quot; Echo .new.yell   &quot;good&quot; ,   &quot;bye&quot; >> &quot;defining method yell“ &quot;yell: [amp;quot;Helloamp;quot;, amp;quot;world!amp;quot;]“ &quot;yell: [amp;quot;goodamp;quot;, amp;quot;byeamp;quot;]&quot;
Dynamically add methods class   Echo def   method_missing   method_sym,   *args p   &quot;defining method #{method_sym}&quot; Echo .class_eval   <<-EOF def   #{method_sym.to_s} *args p   &quot;#{method_sym}: &quot;   +   args.inspect end EOF send (method_sym,   *args) end end Echo .new.yell   &quot;Hello&quot; ,   &quot;world!&quot; Echo .new.yell   &quot;good&quot; ,   &quot;bye&quot; >> &quot;defining method yell“ &quot;yell: [amp;quot;Helloamp;quot;, amp;quot;world!amp;quot;]“ &quot;yell: [amp;quot;goodamp;quot;, amp;quot;byeamp;quot;]&quot;
Dynamically add instance methods >> &quot;new_method: [amp;quot;blahamp;quot;]&quot;
Dynamically add instance methods >> &quot;new_method: [amp;quot;blahamp;quot;]&quot;
Exercise 6 ,[object Object],[object Object],[object Object]
Further Reading ,[object Object],[object Object],[object Object],[object Object]
The End
Extra Stuff ,[object Object],[object Object]
Continuations def   strange callcc   {|continuation|   return   continuation} print   &quot;Back in method. &quot; end print   &quot;Before method. &quot; continuation   =   strange() print   &quot;After method. &quot; continuation .call   if   continuation Before method. After method. Back in method. After method. RETURN THIS
Symbols ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

More Related Content

Similar to Advanced Ruby

Metaprogramming With Ruby
Metaprogramming With RubyMetaprogramming With Ruby
Metaprogramming With RubyFarooq Ali
 
How To Test Everything
How To Test EverythingHow To Test Everything
How To Test Everythingnoelrap
 
Perl Teach-In (part 2)
Perl Teach-In (part 2)Perl Teach-In (part 2)
Perl Teach-In (part 2)Dave Cross
 
Perl Teach-In (part 1)
Perl Teach-In (part 1)Perl Teach-In (part 1)
Perl Teach-In (part 1)Dave Cross
 
What's new in Rails 2?
What's new in Rails 2?What's new in Rails 2?
What's new in Rails 2?brynary
 
Javascript Primer
Javascript PrimerJavascript Primer
Javascript PrimerAdam Hepton
 
Php Loop
Php LoopPhp Loop
Php Looplotlot
 
Pro Java Fx – Developing Enterprise Applications
Pro Java Fx – Developing Enterprise ApplicationsPro Java Fx – Developing Enterprise Applications
Pro Java Fx – Developing Enterprise ApplicationsStephen Chin
 
Advanced Perl Techniques
Advanced Perl TechniquesAdvanced Perl Techniques
Advanced Perl TechniquesDave Cross
 
course slides -- powerpoint
course slides -- powerpointcourse slides -- powerpoint
course slides -- powerpointwebhostingguy
 
Ods Markup And Tagsets: A Tutorial
Ods Markup And Tagsets: A TutorialOds Markup And Tagsets: A Tutorial
Ods Markup And Tagsets: A Tutorialsimienc
 
Java Bytecode For Discriminating Developers - GeeCON 2011
Java Bytecode For Discriminating Developers - GeeCON 2011Java Bytecode For Discriminating Developers - GeeCON 2011
Java Bytecode For Discriminating Developers - GeeCON 2011Anton Arhipov
 
Lecture 3 - Comm Lab: Web @ ITP
Lecture 3 - Comm Lab: Web @ ITP Lecture 3 - Comm Lab: Web @ ITP
Lecture 3 - Comm Lab: Web @ ITP yucefmerhi
 
Refactoring Workshop (Rails Pacific 2014)
Refactoring Workshop (Rails Pacific 2014)Refactoring Workshop (Rails Pacific 2014)
Refactoring Workshop (Rails Pacific 2014)Bruce Li
 
Php Crash Course
Php Crash CoursePhp Crash Course
Php Crash Coursemussawir20
 
Impacta - Show Day de Rails
Impacta - Show Day de RailsImpacta - Show Day de Rails
Impacta - Show Day de RailsFabio Akita
 
Decorators in Python
Decorators in PythonDecorators in Python
Decorators in PythonBen James
 

Similar to Advanced Ruby (20)

Metaprogramming With Ruby
Metaprogramming With RubyMetaprogramming With Ruby
Metaprogramming With Ruby
 
Ruby on Rails
Ruby on RailsRuby on Rails
Ruby on Rails
 
How To Test Everything
How To Test EverythingHow To Test Everything
How To Test Everything
 
Perl Teach-In (part 2)
Perl Teach-In (part 2)Perl Teach-In (part 2)
Perl Teach-In (part 2)
 
Perl Teach-In (part 1)
Perl Teach-In (part 1)Perl Teach-In (part 1)
Perl Teach-In (part 1)
 
Php
PhpPhp
Php
 
What's new in Rails 2?
What's new in Rails 2?What's new in Rails 2?
What's new in Rails 2?
 
Javascript Primer
Javascript PrimerJavascript Primer
Javascript Primer
 
Php Loop
Php LoopPhp Loop
Php Loop
 
Pro Java Fx – Developing Enterprise Applications
Pro Java Fx – Developing Enterprise ApplicationsPro Java Fx – Developing Enterprise Applications
Pro Java Fx – Developing Enterprise Applications
 
Advanced Perl Techniques
Advanced Perl TechniquesAdvanced Perl Techniques
Advanced Perl Techniques
 
course slides -- powerpoint
course slides -- powerpointcourse slides -- powerpoint
course slides -- powerpoint
 
Ods Markup And Tagsets: A Tutorial
Ods Markup And Tagsets: A TutorialOds Markup And Tagsets: A Tutorial
Ods Markup And Tagsets: A Tutorial
 
Java Bytecode For Discriminating Developers - GeeCON 2011
Java Bytecode For Discriminating Developers - GeeCON 2011Java Bytecode For Discriminating Developers - GeeCON 2011
Java Bytecode For Discriminating Developers - GeeCON 2011
 
Lecture 3 - Comm Lab: Web @ ITP
Lecture 3 - Comm Lab: Web @ ITP Lecture 3 - Comm Lab: Web @ ITP
Lecture 3 - Comm Lab: Web @ ITP
 
Refactoring Workshop (Rails Pacific 2014)
Refactoring Workshop (Rails Pacific 2014)Refactoring Workshop (Rails Pacific 2014)
Refactoring Workshop (Rails Pacific 2014)
 
PHP 5.3
PHP 5.3PHP 5.3
PHP 5.3
 
Php Crash Course
Php Crash CoursePhp Crash Course
Php Crash Course
 
Impacta - Show Day de Rails
Impacta - Show Day de RailsImpacta - Show Day de Rails
Impacta - Show Day de Rails
 
Decorators in Python
Decorators in PythonDecorators in Python
Decorators in Python
 

Recently uploaded

Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 

Recently uploaded (20)

Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 

Advanced Ruby

  • 1. Advanced Ruby Beyond the Basics
  • 2. Blocks { puts 'Hello' } do puts 'Hello' end
  • 3. Blocks def call_block puts 'Start of method' # you can call the block using the yield keyword yield yield puts 'End of method' end # invoke call_block {puts 'In the block' } >> Start of method In the block In the block End of method
  • 4. Blocks def call_block_with_params puts 'Start of method' yield 'foo' , 'bar' puts 'End of method' end # invoke call_block_with_params {|a,b| puts &quot;#{a} #{b}&quot; } >> Start of method foo bar End of method
  • 5.
  • 6. Proc objects p = Proc.new { puts &quot;Hello&quot; } p .call l = lambda { puts &quot;Hello&quot; } l .call
  • 7. Proc vs lambda def return_test l = lambda { return } l .call puts &quot;Still here!&quot; p = Proc.new { return } p .call puts &quot;You won't see this.&quot; end return_test >> Still here!
  • 8. Convert Blocks to Proc objects def grab_block (&block) block .call end grab_block {puts &quot;Hello&quot; }
  • 9. Higher Order Functions proc = lambda{puts 'Hello' } proc .call def hello (proc) puts &quot;start of method&quot; proc .call puts &quot;end of method&quot; end # invoke hello proc >> Hello >> start of method Hello end of method
  • 10. Closures >> a = 1 @a = 2 original class Holder def call_block (pr) a = 101 @a = 102 pr .call end end class Creator def create_block a = 1 @a = 2 lambda do puts &quot;a = #{a}&quot; puts &quot;@a = #@a&quot; puts yield end end end block = Creator.new.create_block { &quot;original&quot; } Holder .new.call_block block
  • 11.
  • 12. Adding methods to a class class Object def greet puts &quot;Hello&quot; end end obj = Object.new obj .greet >> Hello obj2 = Object.new obj2 .greet >> Hello
  • 13. Adding methods to a class - example 3.hours .from_now class Fixnum def hours self * 60 * 60 end def from_now Time .now + self end end
  • 14. Adding a method to an object obj = Object.new def obj .greet puts &quot;Hello&quot; end obj .greet >> Hello obj2 = Object.new obj2 .greet >> undefined method `greet' for #<Object:0x2bae594>
  • 15. Singleton Class object Singleton class class module
  • 16. Adding methods to singleton class s = &quot;Hello&quot; class << s def twice self + &quot; &quot; + self end end puts s .twice >> Hello Hello def s .twice self + &quot; &quot; + self end
  • 17. Using modules module M def greet puts &quot;Hello&quot; end end obj = C.new class << obj include M end obj.greet obj = C.new obj.extend(M) obj.greet
  • 18. Class method definitions class String class << self def hello &quot;hello&quot; end end end class << String def hello &quot;hello&quot; end end def String def self .hello &quot;hello&quot; end end
  • 19. Class methods and inheritance class C singleton class of C class D singleton class of D extends lookup path object
  • 20.
  • 21.
  • 22. Method Aliasing class C def hi puts &quot;hello&quot; end end class C alias_method :original_hi , :hi def hi puts &quot;greetings“ original_hi end end obj = C.new obj .hi >> greetings hello
  • 23.
  • 24. Method_missing class Echo def method_missing method_sym, *args puts &quot;#{method_sym}: #{args.inspect}&quot; end end Echo .new.yell &quot;Hello&quot; , &quot;world!&quot; Echo .new.say &quot;Good&quot; , &quot;bye!&quot; >> yell: [&quot;Hello&quot;, &quot;world!&quot;] say: [&quot;Good&quot;, &quot;bye!&quot;]
  • 25.
  • 26. Dynamically add methods >> &quot;defining method yell“ &quot;yell: [amp;quot;Helloamp;quot;, amp;quot;world!amp;quot;]“ &quot;yell: [amp;quot;goodamp;quot;, amp;quot;byeamp;quot;]&quot;
  • 27. Dynamically add methods class Echo def method_missing method_sym, *args p &quot;defining method #{method_sym}&quot; self .class.class_eval <<-EOF def #{method_sym.to_s} *args p &quot;#{method_sym}: &quot; + args.inspect end EOF send (method_sym, *args) end end Echo .new.yell &quot;Hello&quot; , &quot;world!&quot; Echo .new.yell &quot;good&quot; , &quot;bye&quot; >> &quot;defining method yell“ &quot;yell: [amp;quot;Helloamp;quot;, amp;quot;world!amp;quot;]“ &quot;yell: [amp;quot;goodamp;quot;, amp;quot;byeamp;quot;]&quot;
  • 28. Dynamically add methods class Echo def method_missing method_sym, *args p &quot;defining method #{method_sym}&quot; Echo .class_eval <<-EOF def #{method_sym.to_s} *args p &quot;#{method_sym}: &quot; + args.inspect end EOF send (method_sym, *args) end end Echo .new.yell &quot;Hello&quot; , &quot;world!&quot; Echo .new.yell &quot;good&quot; , &quot;bye&quot; >> &quot;defining method yell“ &quot;yell: [amp;quot;Helloamp;quot;, amp;quot;world!amp;quot;]“ &quot;yell: [amp;quot;goodamp;quot;, amp;quot;byeamp;quot;]&quot;
  • 29. Dynamically add instance methods >> &quot;new_method: [amp;quot;blahamp;quot;]&quot;
  • 30. Dynamically add instance methods >> &quot;new_method: [amp;quot;blahamp;quot;]&quot;
  • 31.
  • 32.
  • 34.
  • 35. Continuations def strange callcc {|continuation| return continuation} print &quot;Back in method. &quot; end print &quot;Before method. &quot; continuation = strange() print &quot;After method. &quot; continuation .call if continuation Before method. After method. Back in method. After method. RETURN THIS
  • 36.

Editor's Notes

  1. Anonymous chunk of code - Equivalent styles – {} and do...end
  2. - Can be associated with methods - Method invokes the block using ‘yield’
  3. Can have parameters Used in: iterators for wrapping actions (e.g. File.open)
  4. - A block converted to an object - Lambda – same as Proc.new, except: - Proc objects checks the number of parameters passed - scope of return in lambda is the block scope, not enclosing scope.
  5. Can convert a code block into a Proc object by using ‘&amp;’
  6. - functions can be assigned to variables - can be passed as parameters - can be returned from methods
  7. A stored block (stored in a variable) with a closed context Context is fixed to the context where the Proc object was created Associated with a block (and hence a Proc object) is all the context in which the block was defined: the value of self and the methods, variables, and constants in scope. Part of the magic of Ruby is that the block can still use all this original scope information even if the environment in which it was defined would otherwise have disappeared. In other languages, this facility is called a closure.
  8. Ruby has Open classes – that allow you to: Add a method to an existing class Add a method to an object of a class
  9. Simplified version of what rails does in ActiveSupport::CoreExtensions::Numeric::Time
  10. The method added is called a singleton method
  11. Every object in Ruby has its own singleton class aka eigenclass Singleton methods live in this singleton class Singleton class is: an object (instance of Class) anonymous – has no name REMEMBER – classes are objects too – (instances of class Class). the singleton class of a class is called a metaclass .
  12. The two approaches here are equivalent (ignoring a minor point regarding the scope of constants)
  13. Can use extend instead opening class to make methods in module available
  14. A common use of the class &lt;&lt; notation is for class method definitions. The above three ways of defining a class method are equivalent
  15. Normally singleton methods are only available to the object they belong to. An exception is class methods – they are available to subclasses
  16. alias_method :new_id, :existing_id
  17. Using class_eval . class_eval should be called on a class self.class is the same as Echo
  18. self.class is the same as Echo
  19. - allows entire execution context to be saved - could be used instead of threads - shared concurrency - have complete control instead of letting Thread scheduler decide.
  20. The following statements are handy in using (or not using) symbols: A Ruby symbol looks like a colon followed by characters. (:mysymbol) A Ruby symbol is a thing that has both a number (integer) and a string. The value of a Ruby symbol&apos;s string part is the name of the symbol, minus the leading colon. A Ruby symbol cannot be changed at runtime. Neither its string representation nor its integer representation can be changed at runtime. Ruby symbols are useful in preventing modification. Like most other things in Ruby, a symbol is an object. When designing a program, you can usually use a string instead of a symbol. Except when you must guarantee that the string isn&apos;t modified. Symbol objects do not have the rich set of instance methods that String objects do. After the first usage of :mysymbol all further useages of :mysymbol take no further memory -- they&apos;re all the same object. Ruby symbols save memory over large numbers of identical literal strings. Ruby symbols enhance runtime speed to at least some degree.