SlideShare ist ein Scribd-Unternehmen logo
1 von 17
Ruby Blocks Plus Test Driven Performance Monitorwith Stubs
Blocks A block is an anonymous function.  my_array.each do |value| 	do_something(value) end
Blocks def dos_veces     yield     yield end dos_veces { puts "Hola” } Hola Hola Yield executes the block This is a Block! {
Yield with Parameters def superpowers a = “x-ray vision” b = “flying” yield(a, b) end superpowers do |x,y| puts x,y  end x-ray vision flying Yield sends its parameters as arguments to the block yield(a,b) sends a and b to |x, y| x is set to a y is set to b
Block Syntax { |x| puts x} is the same as: do |x|  	puts x end
Checking for a Block def repeat(num)     if block_given?         num.times { puts yield }     else         puts "I can't repeat what you don't tell me"     end end
Explicit Block Parameter def repeat_lots(&my_block)   	puts "my_block is a #{my_block.class}"   	puts my_block.call   	puts yield          # this does the same thing    	puts my_block.call end
Performance Monitor Example	 How it works: Record Time Run Code Report Time Difference Hmmm… how do we test when time is always changing?
Test Doubles 	A Test Double replaces the "real" instance of an object used by the production code with something suitable for the currently running test, but with the same interface Stubs Hard-coded values Mocks Pre-programmed with expectations Fail-fast Test Doubles in general are often called Mock Objects, so be careful about terminology Fakes Can store values across calls, but don't really do what the live object would do E.g. in-memory database
How to Test Code with Dependencies Example from The Rspec Book by David Chelimsky
Isolating the Subject Example from The Rspec Book by David Chelimsky
Stub in RSpec my_instance.stub!(:msg).and_return(value) MyClass.stub!(:msg).and_return(value)
Using stub from irb >> require ‘spec’ >> require ‘spec/mocks’ >>Time.stub!(:now).and_return(10,20) >>Time.now ,[object Object],>>Time.now ,[object Object],>>Time.now ,[object Object],[object Object],[object Object]
Stub for Specific Values my_obj.stub!(:msg).with(1).and_return(2)
Stub in RSpec my_obj.stub!(:msg).and_return { ... }
Ruby Blocks
Ruby Blocks

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

Runtime Bytecode Transformation for Smalltalk
Runtime Bytecode Transformation for SmalltalkRuntime Bytecode Transformation for Smalltalk
Runtime Bytecode Transformation for Smalltalk
 
More Perl Basics
More Perl BasicsMore Perl Basics
More Perl Basics
 
Closures
ClosuresClosures
Closures
 
Function
FunctionFunction
Function
 
Test Driven Development With Python
Test Driven Development With PythonTest Driven Development With Python
Test Driven Development With Python
 
Let's talk ChatOps - Hubot with less CoffeeScript
Let's talk ChatOps - Hubot with less CoffeeScriptLet's talk ChatOps - Hubot with less CoffeeScript
Let's talk ChatOps - Hubot with less CoffeeScript
 
ITB2019 Real World Scenarios for Modern CFML - Nolan Erck
ITB2019 Real World Scenarios for Modern CFML - Nolan ErckITB2019 Real World Scenarios for Modern CFML - Nolan Erck
ITB2019 Real World Scenarios for Modern CFML - Nolan Erck
 
Advanced Ruby
Advanced RubyAdvanced Ruby
Advanced Ruby
 
What comments hide
What comments hideWhat comments hide
What comments hide
 
Angular testing
Angular testingAngular testing
Angular testing
 
Unit tests in node.js
Unit tests in node.jsUnit tests in node.js
Unit tests in node.js
 
unittest in 5 minutes
unittest in 5 minutesunittest in 5 minutes
unittest in 5 minutes
 
Spock framework
Spock frameworkSpock framework
Spock framework
 
Automated Testing in Django
Automated Testing in DjangoAutomated Testing in Django
Automated Testing in Django
 
Low Latency Logging with RabbitMQ (PHP London - 4th Sep 2014)
Low Latency Logging with RabbitMQ (PHP London - 4th Sep 2014)Low Latency Logging with RabbitMQ (PHP London - 4th Sep 2014)
Low Latency Logging with RabbitMQ (PHP London - 4th Sep 2014)
 
Python Programming Essentials - M37 - Brief Overview of Misc Concepts
Python Programming Essentials - M37 - Brief Overview of Misc ConceptsPython Programming Essentials - M37 - Brief Overview of Misc Concepts
Python Programming Essentials - M37 - Brief Overview of Misc Concepts
 
Low Latency Logging with RabbitMQ (Brno PHP, CZ - 20th Sep 2014)
Low Latency Logging with RabbitMQ (Brno PHP, CZ - 20th Sep 2014)Low Latency Logging with RabbitMQ (Brno PHP, CZ - 20th Sep 2014)
Low Latency Logging with RabbitMQ (Brno PHP, CZ - 20th Sep 2014)
 
Pyunit
PyunitPyunit
Pyunit
 
谈谈Javascript设计
谈谈Javascript设计谈谈Javascript设计
谈谈Javascript设计
 
New Stuff In Php 5.3
New Stuff In Php 5.3New Stuff In Php 5.3
New Stuff In Php 5.3
 

Ähnlich wie Ruby Blocks

Boosting Your Testing Productivity with Groovy
Boosting Your Testing Productivity with GroovyBoosting Your Testing Productivity with Groovy
Boosting Your Testing Productivity with Groovy
James Williams
 
JavaScript (without DOM)
JavaScript (without DOM)JavaScript (without DOM)
JavaScript (without DOM)
Piyush Katariya
 

Ähnlich wie Ruby Blocks (20)

Ruby Blocks
Ruby BlocksRuby Blocks
Ruby Blocks
 
Ruby Blocks
Ruby BlocksRuby Blocks
Ruby Blocks
 
Advanced Perl Techniques
Advanced Perl TechniquesAdvanced Perl Techniques
Advanced Perl Techniques
 
How To Test Everything
How To Test EverythingHow To Test Everything
How To Test Everything
 
Cool JVM Tools to Help You Test
Cool JVM Tools to Help You TestCool JVM Tools to Help You Test
Cool JVM Tools to Help You Test
 
How Testability Inspires AngularJS Design / Ran Mizrahi
How Testability Inspires AngularJS Design / Ran MizrahiHow Testability Inspires AngularJS Design / Ran Mizrahi
How Testability Inspires AngularJS Design / Ran Mizrahi
 
Cool Jvm Tools to Help you Test - Aylesbury Testers Version
Cool Jvm Tools to Help you Test - Aylesbury Testers VersionCool Jvm Tools to Help you Test - Aylesbury Testers Version
Cool Jvm Tools to Help you Test - Aylesbury Testers Version
 
Building unit tests correctly
Building unit tests correctlyBuilding unit tests correctly
Building unit tests correctly
 
Ruby for C# Developers
Ruby for C# DevelopersRuby for C# Developers
Ruby for C# Developers
 
Mockito with a hint of PowerMock
Mockito with a hint of PowerMockMockito with a hint of PowerMock
Mockito with a hint of PowerMock
 
12 ruby blocks
12 ruby blocks12 ruby blocks
12 ruby blocks
 
Boosting Your Testing Productivity with Groovy
Boosting Your Testing Productivity with GroovyBoosting Your Testing Productivity with Groovy
Boosting Your Testing Productivity with Groovy
 
Javaone2008 Bof 5101 Groovytesting
Javaone2008 Bof 5101 GroovytestingJavaone2008 Bof 5101 Groovytesting
Javaone2008 Bof 5101 Groovytesting
 
Real life-coffeescript
Real life-coffeescriptReal life-coffeescript
Real life-coffeescript
 
Spock: Test Well and Prosper
Spock: Test Well and ProsperSpock: Test Well and Prosper
Spock: Test Well and Prosper
 
JavaScript (without DOM)
JavaScript (without DOM)JavaScript (without DOM)
JavaScript (without DOM)
 
Introduction to Client-Side Javascript
Introduction to Client-Side JavascriptIntroduction to Client-Side Javascript
Introduction to Client-Side Javascript
 
Ajax and JavaScript Bootcamp
Ajax and JavaScript BootcampAjax and JavaScript Bootcamp
Ajax and JavaScript Bootcamp
 
Why you should be using the shiny new C# 6.0 features now!
Why you should be using the shiny new C# 6.0 features now!Why you should be using the shiny new C# 6.0 features now!
Why you should be using the shiny new C# 6.0 features now!
 
JavaScript: The Good Parts Or: How A C# Developer Learned To Stop Worrying An...
JavaScript: The Good Parts Or: How A C# Developer Learned To Stop Worrying An...JavaScript: The Good Parts Or: How A C# Developer Learned To Stop Worrying An...
JavaScript: The Good Parts Or: How A C# Developer Learned To Stop Worrying An...
 

Mehr von Sarah Allen

Mehr von Sarah Allen (20)

Internet security: a landscape of unintended consequences
Internet security: a landscape of unintended consequencesInternet security: a landscape of unintended consequences
Internet security: a landscape of unintended consequences
 
RTMP: how did we get to now? (Demuxed 2019)
RTMP: how did we get to now? (Demuxed 2019)RTMP: how did we get to now? (Demuxed 2019)
RTMP: how did we get to now? (Demuxed 2019)
 
Communication is a Technical Skill
Communication is a Technical SkillCommunication is a Technical Skill
Communication is a Technical Skill
 
Improving Federal Government Services
Improving Federal Government ServicesImproving Federal Government Services
Improving Federal Government Services
 
Transparency Wins
Transparency WinsTransparency Wins
Transparency Wins
 
A Short History of Computers
A Short History of ComputersA Short History of Computers
A Short History of Computers
 
Making Software Fun
Making Software FunMaking Software Fun
Making Software Fun
 
Power of Transparency
Power of TransparencyPower of Transparency
Power of Transparency
 
Designing for Fun
Designing for FunDesigning for Fun
Designing for Fun
 
Ruby in the US Government for Ruby World Conference
Ruby in the US Government for Ruby World ConferenceRuby in the US Government for Ruby World Conference
Ruby in the US Government for Ruby World Conference
 
Identities of Dead People
Identities of Dead PeopleIdentities of Dead People
Identities of Dead People
 
Let's pretend
Let's pretendLet's pretend
Let's pretend
 
3 Reasons Not to Use Ruby
3 Reasons Not to Use Ruby 3 Reasons Not to Use Ruby
3 Reasons Not to Use Ruby
 
Ruby Nation: Why no haz Ruby?
Ruby Nation: Why no haz Ruby?Ruby Nation: Why no haz Ruby?
Ruby Nation: Why no haz Ruby?
 
Why no ruby in gov?
Why no ruby in gov?Why no ruby in gov?
Why no ruby in gov?
 
People Patterns or What I learned from Toastmasters
People Patterns or What I learned from ToastmastersPeople Patterns or What I learned from Toastmasters
People Patterns or What I learned from Toastmasters
 
Blazing Cloud: Agile Product Development
Blazing Cloud: Agile Product DevelopmentBlazing Cloud: Agile Product Development
Blazing Cloud: Agile Product Development
 
Crowdsourced Transcription Landscape
Crowdsourced Transcription LandscapeCrowdsourced Transcription Landscape
Crowdsourced Transcription Landscape
 
Lessons Learned Future Thoughts
Lessons Learned Future ThoughtsLessons Learned Future Thoughts
Lessons Learned Future Thoughts
 
Mobile Web Video
Mobile Web VideoMobile Web Video
Mobile Web Video
 

Kürzlich hochgeladen

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Kürzlich hochgeladen (20)

Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 

Ruby Blocks

  • 1. Ruby Blocks Plus Test Driven Performance Monitorwith Stubs
  • 2. Blocks A block is an anonymous function. my_array.each do |value| do_something(value) end
  • 3. Blocks def dos_veces yield yield end dos_veces { puts "Hola” } Hola Hola Yield executes the block This is a Block! {
  • 4. Yield with Parameters def superpowers a = “x-ray vision” b = “flying” yield(a, b) end superpowers do |x,y| puts x,y end x-ray vision flying Yield sends its parameters as arguments to the block yield(a,b) sends a and b to |x, y| x is set to a y is set to b
  • 5. Block Syntax { |x| puts x} is the same as: do |x| puts x end
  • 6. Checking for a Block def repeat(num) if block_given? num.times { puts yield } else puts "I can't repeat what you don't tell me" end end
  • 7. Explicit Block Parameter def repeat_lots(&my_block) puts "my_block is a #{my_block.class}" puts my_block.call puts yield # this does the same thing puts my_block.call end
  • 8. Performance Monitor Example How it works: Record Time Run Code Report Time Difference Hmmm… how do we test when time is always changing?
  • 9. Test Doubles A Test Double replaces the "real" instance of an object used by the production code with something suitable for the currently running test, but with the same interface Stubs Hard-coded values Mocks Pre-programmed with expectations Fail-fast Test Doubles in general are often called Mock Objects, so be careful about terminology Fakes Can store values across calls, but don't really do what the live object would do E.g. in-memory database
  • 10. How to Test Code with Dependencies Example from The Rspec Book by David Chelimsky
  • 11. Isolating the Subject Example from The Rspec Book by David Chelimsky
  • 12. Stub in RSpec my_instance.stub!(:msg).and_return(value) MyClass.stub!(:msg).and_return(value)
  • 13.
  • 14. Stub for Specific Values my_obj.stub!(:msg).with(1).and_return(2)
  • 15. Stub in RSpec my_obj.stub!(:msg).and_return { ... }

Hinweis der Redaktion

  1. What is a block? It is the ability to take a block of code, wrap it up in an object and pass it to a method. Then you can run the block of code within the method any time you want…sometimes twice! The result is kind of like sending a method to a method, except that a block isn’t bound to an object like a method is – it is an object. So what? Why use blocks?elegant syntax for iteratorsBecause there are some things that only blocks can do, like being passed to a method and being returned by a method.
  2. two ways to declare a blockuse curly brackets for single lines of codeuse do end for multi lines of code