SlideShare a Scribd company logo
1 of 19
RUBY 2.0
  ugisozols.com
Refinments
module	
  StringPatch
	
  	
  refine	
  String	
  do
	
  	
  	
  	
  def	
  is_number?
	
  	
  	
  	
  	
  	
  !!match(/^d+$/)
	
  	
  	
  	
  end
	
  	
  end
end
	
  
"1".is_number?	
  #	
  =>	
  undefined	
  method	
  `is_number?'	
  ...
	
  
using	
  StringPatch
	
  
"1".is_number?	
  #	
  =>	
  true
"a".is_number?	
  #	
  =>	
  false
Refinments
$	
  ruby	
  string_patch.rb	
  

	
  	
  string_patch.rb:2:	
  warning:	
  Refinements	
  are	
  experimental,
	
  	
  and	
  the	
  behavior	
  may	
  change	
  in	
  future	
  versions	
  of	
  Ruby!
Module#prepend
module	
  A
	
  	
  def	
  foo;	
  puts	
  "A";	
  end
end
	
  
class	
  B
	
  	
  include	
  A
	
  
	
  	
  def	
  foo;	
  puts	
  "B";	
  end
end
	
  
klass	
  =	
  B.new
klass.foo	
  	
  	
  #	
  =>	
  "B"
B.ancestors	
  #	
  =>	
  [B,	
  A,	
  Object,	
  Kernel,	
  BasicObject]
Module#prepend
module	
  A
	
  	
  def	
  foo;	
  puts	
  "A";	
  end
end
	
  
class	
  B
	
  	
  prepend	
  A	
  #	
  <-­‐	
  prepend	
  instead	
  of	
  include
	
  
	
  	
  def	
  foo;	
  puts	
  "B";	
  end
end
	
  
klass	
  =	
  B.new
klass.foo	
  	
  	
  #	
  =>	
  "A"
B.ancestors	
  #	
  =>	
  [A,	
  B,	
  Object,	
  Kernel,	
  BasicObject]
Module#prepend
require	
  "active_support/core_ext/module/aliasing"
	
  
class	
  A
	
  	
  def	
  foo;	
  puts	
  "foo";	
  end
end
	
  
class	
  A
	
  	
  def	
  foo_with_bar
	
  	
  	
  	
  foo_without_bar
	
  	
  	
  	
  puts	
  "bar"
	
  	
  end
	
  
	
  	
  alias_method_chain	
  :foo,	
  :bar
end
	
  
A.new.foo	
  	
  	
  #	
  =>	
  "foo	
  bar"
A.ancestors	
  #	
  =>	
  [A,	
  Object,	
  Kernel,	
  BasicObject]
Module#prepend
class	
  A
	
  	
  def	
  foo;	
  puts	
  "foo";	
  end
end
	
  
module	
  Extension
	
  	
  def	
  foo
	
  	
  	
  	
  super
	
  	
  	
  	
  puts	
  "bar"
	
  	
  end
end
	
  
class	
  A
	
  	
  prepend	
  Extension
end
	
  
A.new.foo	
  	
  	
  #	
  =>	
  "foo	
  bar"
A.ancestors	
  #	
  =>	
  [Extension,	
  A,	
  Object,	
  Kernel,	
  BasicObject]
Keyword Arguments
def	
  link_to(name,	
  url_options	
  =	
  {},	
  html_options	
  =	
  {})
	
  	
  puts	
  "name	
  =	
  #{name.inspect}"
	
  	
  puts	
  "url_options	
  =	
  #{url_options.inspect}"
	
  	
  puts	
  "html_options	
  =	
  #{html_options.inspect}"
end
	
  
link_to("Ruby	
  1.9",	
  {},	
  {:class	
  =>	
  "styled-­‐link"})
#	
  name	
  =	
  "Ruby	
  1.9"
#	
  url_options	
  =	
  {}
#	
  html_options	
  =	
  {:class=>"styled-­‐link"}
Keyword Arguments
def	
  link_to(name,	
  url_options:	
  {},	
  html_options:	
  {})
	
  	
  puts	
  "name	
  =	
  #{name.inspect}"
	
  	
  puts	
  "url_options	
  =	
  #{url_options.inspect}"
	
  	
  puts	
  "html_options	
  =	
  #{html_options.inspect}"
end
	
  
link_to("Ruby	
  2.0",
	
  	
  	
  	
  	
  	
  	
  	
  :html_options	
  =>	
  {:class	
  =>	
  "styled-­‐link"})
#	
  name	
  =	
  "Ruby	
  2.0"
#	
  url_options	
  =	
  {}
#	
  html_options	
  =	
  {:class=>"styled-­‐link"}
Keyword Arguments
def	
  temperature(celsius,	
  fahrenheit:	
  to_fahrenheit(celsius))
	
  	
  puts	
  "#{celsius}°C"
	
  	
  puts	
  "#{fahrenheit}°F"
end
	
  
def	
  to_fahrenheit(celsius)
	
  	
  celsius	
  *	
  9	
  /	
  5	
  +	
  32
end
	
  
temperature(36)
#	
  36°C
#	
  96°F
Enumerable#lazy
words	
  =	
  File.open('/usr/share/dict/words')
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  .map(&:chomp)
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  .reject	
  {	
  |word|	
  word.length	
  <	
  4	
  }
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  .take(10)
	
  
words	
  #	
  =>	
  ["aalii",	
  "Aani",	
  "aardvark",	
  ...]

#	
  time	
  ruby	
  lazy.rb	
  ~	
  0.22s
Enumerable#lazy
words	
  =	
  File.open('/usr/share/dict/words')
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  .lazy	
  #	
  <-­‐-­‐	
  added	
  lazy	
  here
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  .map(&:chomp)
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  .reject	
  {	
  |word|	
  word.length	
  <	
  4	
  }
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  .take(10)
	
  
words	
  	
  	
  	
  	
  	
  #	
  =>	
  #<Enumerator::Lazy:	
  ...]
words.to_a	
  #	
  =>	
  ["aalii",	
  "Aani",	
  "aardvark",	
  ...]
	
  
#	
  time	
  ruby	
  lazy.rb	
  ~	
  0.06s
Enumerable#lazy
require	
  "fruity"
	
  
range	
  =	
  1..100
	
  
compare	
  do
	
  	
  without_lazy	
  {	
  range.	
  	
  	
  	
  	
  map	
  {	
  |el|	
  el	
  *	
  2	
  }	
  }
	
  	
  with_lazy	
  	
  	
  	
  {	
  range.lazy.map	
  {	
  |el|	
  el	
  *	
  2	
  }.to_a	
  }
end
	
  
#	
  Running	
  each	
  test	
  256	
  times.
#	
  without_lazy	
  is	
  faster	
  than	
  with_lazy	
  by	
  4x	
  ±	
  0.1
%i and %I symbol literals

%i(foo	
  bar	
  baz)	
  #	
  =>	
  [:foo,	
  :bar,	
  :baz]
	
  
%I(foo	
  b#{2+2}r	
  baz)	
  #	
  =>	
  [:foo,	
  :b4r,	
  :baz]
Default source encoding is
          UTF-8
#	
  Ruby	
  1.9
#	
  encoding:	
  utf-­‐8
#	
  without	
  ^	
  next	
  line	
  will	
  throw	
  "invalid	
  multibyte	
  char	
  (US-­‐
ASCII)"
name	
  =	
  "Uģis	
  Ozols"


#	
  Ruby	
  2.0
name	
  =	
  "Uģis	
  Ozols"
#to_h
Ruby	
  =	
  Struct.new(:version)
ruby	
  =	
  Ruby.new("2.0")
	
  
ruby.to_h	
  #	
  =>	
  {	
  :version	
  =>	
  "2.0"	
  }



def	
  config(options)
	
  	
  if	
  options.respond_to?(:to_h)
	
  	
  	
  	
  #	
  consume	
  hash
	
  	
  else
	
  	
  	
  	
  raise	
  TypeErorr,	
  "Can't	
  convert	
  #{options.inspect}	
  into	
  
Hash"
	
  	
  end
end
#respond_to? returns
false for protected methods
 class	
  A
 	
  	
  protected
 	
  
 	
  	
  def	
  foo
 	
  	
  	
  	
  "A"
 	
  	
  end
 end
 	
  
 klass	
  =	
  A.new
 klass.respond_to?(:foo)	
  #=>	
  false
 klass.respond_to?(:foo,	
  true)	
  #=>	
  true
#require optimizations

Rails	
  4.0.0.beta1	
  application
	
  
rails	
  g	
  scaffold	
  user	
  name
	
  
time	
  rake	
  test                  Ruby	
  1.9.3-­‐p392
~	
  12	
  seconds
	
  
time	
  rake	
  test                  Ruby	
  2.0.0-­‐p0
~	
  5	
  seconds
Thank you for listening!




        ugisozols.com

More Related Content

What's hot

Perl6 Regexen: Reduce the line noise in your code.
Perl6 Regexen: Reduce the line noise in your code.Perl6 Regexen: Reduce the line noise in your code.
Perl6 Regexen: Reduce the line noise in your code.Workhorse Computing
 
COSCUP2012: How to write a bash script like the python?
COSCUP2012: How to write a bash script like the python?COSCUP2012: How to write a bash script like the python?
COSCUP2012: How to write a bash script like the python?Lloyd Huang
 
Static Optimization of PHP bytecode (PHPSC 2017)
Static Optimization of PHP bytecode (PHPSC 2017)Static Optimization of PHP bytecode (PHPSC 2017)
Static Optimization of PHP bytecode (PHPSC 2017)Nikita Popov
 
Neatly Hashing a Tree: FP tree-fold in Perl5 & Perl6
Neatly Hashing a Tree: FP tree-fold in Perl5 & Perl6Neatly Hashing a Tree: FP tree-fold in Perl5 & Perl6
Neatly Hashing a Tree: FP tree-fold in Perl5 & Perl6Workhorse Computing
 
PHP Basics and Demo HackU
PHP Basics and Demo HackUPHP Basics and Demo HackU
PHP Basics and Demo HackUAnshu Prateek
 
Typed Properties and more: What's coming in PHP 7.4?
Typed Properties and more: What's coming in PHP 7.4?Typed Properties and more: What's coming in PHP 7.4?
Typed Properties and more: What's coming in PHP 7.4?Nikita Popov
 
Generated Power: PHP 5.5 Generators
Generated Power: PHP 5.5 GeneratorsGenerated Power: PHP 5.5 Generators
Generated Power: PHP 5.5 GeneratorsMark Baker
 
BSDM with BASH: Command Interpolation
BSDM with BASH: Command InterpolationBSDM with BASH: Command Interpolation
BSDM with BASH: Command InterpolationWorkhorse Computing
 
Doctrine MongoDB ODM (PDXPHP)
Doctrine MongoDB ODM (PDXPHP)Doctrine MongoDB ODM (PDXPHP)
Doctrine MongoDB ODM (PDXPHP)Kris Wallsmith
 
Refactor like a boss
Refactor like a bossRefactor like a boss
Refactor like a bossgsterndale
 
Advanced symfony Techniques
Advanced symfony TechniquesAdvanced symfony Techniques
Advanced symfony TechniquesKris Wallsmith
 
BASH Variables Part 1: Basic Interpolation
BASH Variables Part 1: Basic InterpolationBASH Variables Part 1: Basic Interpolation
BASH Variables Part 1: Basic InterpolationWorkhorse Computing
 
A Functional Guide to Cat Herding with PHP Generators
A Functional Guide to Cat Herding with PHP GeneratorsA Functional Guide to Cat Herding with PHP Generators
A Functional Guide to Cat Herding with PHP GeneratorsMark Baker
 

What's hot (20)

Perl6 grammars
Perl6 grammarsPerl6 grammars
Perl6 grammars
 
Perl6 Regexen: Reduce the line noise in your code.
Perl6 Regexen: Reduce the line noise in your code.Perl6 Regexen: Reduce the line noise in your code.
Perl6 Regexen: Reduce the line noise in your code.
 
COSCUP2012: How to write a bash script like the python?
COSCUP2012: How to write a bash script like the python?COSCUP2012: How to write a bash script like the python?
COSCUP2012: How to write a bash script like the python?
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
 
Static Optimization of PHP bytecode (PHPSC 2017)
Static Optimization of PHP bytecode (PHPSC 2017)Static Optimization of PHP bytecode (PHPSC 2017)
Static Optimization of PHP bytecode (PHPSC 2017)
 
Introduction in php
Introduction in phpIntroduction in php
Introduction in php
 
Neatly Hashing a Tree: FP tree-fold in Perl5 & Perl6
Neatly Hashing a Tree: FP tree-fold in Perl5 & Perl6Neatly Hashing a Tree: FP tree-fold in Perl5 & Perl6
Neatly Hashing a Tree: FP tree-fold in Perl5 & Perl6
 
PHP Basics and Demo HackU
PHP Basics and Demo HackUPHP Basics and Demo HackU
PHP Basics and Demo HackU
 
Typed Properties and more: What's coming in PHP 7.4?
Typed Properties and more: What's coming in PHP 7.4?Typed Properties and more: What's coming in PHP 7.4?
Typed Properties and more: What's coming in PHP 7.4?
 
Scalar data types
Scalar data typesScalar data types
Scalar data types
 
Intro to Perl and Bioperl
Intro to Perl and BioperlIntro to Perl and Bioperl
Intro to Perl and Bioperl
 
Generated Power: PHP 5.5 Generators
Generated Power: PHP 5.5 GeneratorsGenerated Power: PHP 5.5 Generators
Generated Power: PHP 5.5 Generators
 
Subroutines
SubroutinesSubroutines
Subroutines
 
BSDM with BASH: Command Interpolation
BSDM with BASH: Command InterpolationBSDM with BASH: Command Interpolation
BSDM with BASH: Command Interpolation
 
Doctrine MongoDB ODM (PDXPHP)
Doctrine MongoDB ODM (PDXPHP)Doctrine MongoDB ODM (PDXPHP)
Doctrine MongoDB ODM (PDXPHP)
 
Refactor like a boss
Refactor like a bossRefactor like a boss
Refactor like a boss
 
Advanced symfony Techniques
Advanced symfony TechniquesAdvanced symfony Techniques
Advanced symfony Techniques
 
BASH Variables Part 1: Basic Interpolation
BASH Variables Part 1: Basic InterpolationBASH Variables Part 1: Basic Interpolation
BASH Variables Part 1: Basic Interpolation
 
A Functional Guide to Cat Herding with PHP Generators
A Functional Guide to Cat Herding with PHP GeneratorsA Functional Guide to Cat Herding with PHP Generators
A Functional Guide to Cat Herding with PHP Generators
 
Perl 6 by example
Perl 6 by examplePerl 6 by example
Perl 6 by example
 

Similar to Ruby 2.0

Attributes Unwrapped: Lessons under the surface of active record
Attributes Unwrapped: Lessons under the surface of active recordAttributes Unwrapped: Lessons under the surface of active record
Attributes Unwrapped: Lessons under the surface of active record.toster
 
Ruby 程式語言入門導覽
Ruby 程式語言入門導覽Ruby 程式語言入門導覽
Ruby 程式語言入門導覽Wen-Tien Chang
 
Ruby 入門 第一次就上手
Ruby 入門 第一次就上手Ruby 入門 第一次就上手
Ruby 入門 第一次就上手Wen-Tien Chang
 
Code for Startup MVP (Ruby on Rails) Session 2
Code for Startup MVP (Ruby on Rails) Session 2Code for Startup MVP (Ruby on Rails) Session 2
Code for Startup MVP (Ruby on Rails) Session 2Henry S
 
Perl 5.10
Perl 5.10Perl 5.10
Perl 5.10acme
 
Ruby on Rails 中級者を目指して - 大場寧子
Ruby on Rails 中級者を目指して - 大場寧子Ruby on Rails 中級者を目指して - 大場寧子
Ruby on Rails 中級者を目指して - 大場寧子Yasuko Ohba
 
Metaprogramovanie #1
Metaprogramovanie #1Metaprogramovanie #1
Metaprogramovanie #1Jano Suchal
 
A tour on ruby and friends
A tour on ruby and friendsA tour on ruby and friends
A tour on ruby and friends旻琦 潘
 
Ruby from zero to hero
Ruby from zero to heroRuby from zero to hero
Ruby from zero to heroDiego Lemos
 
Programming in perl style
Programming in perl styleProgramming in perl style
Programming in perl styleBo Hua Yang
 
Slides chapter3part1 ruby-forjavaprogrammers
Slides chapter3part1 ruby-forjavaprogrammersSlides chapter3part1 ruby-forjavaprogrammers
Slides chapter3part1 ruby-forjavaprogrammersGiovanni924
 
Ruby - Uma Introdução
Ruby - Uma IntroduçãoRuby - Uma Introdução
Ruby - Uma IntroduçãoÍgor Bonadio
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perlworr1244
 
Ruby_Coding_Convention
Ruby_Coding_ConventionRuby_Coding_Convention
Ruby_Coding_ConventionJesse Cai
 

Similar to Ruby 2.0 (20)

An introduction to Ruby
An introduction to RubyAn introduction to Ruby
An introduction to Ruby
 
Attributes Unwrapped: Lessons under the surface of active record
Attributes Unwrapped: Lessons under the surface of active recordAttributes Unwrapped: Lessons under the surface of active record
Attributes Unwrapped: Lessons under the surface of active record
 
Designing Ruby APIs
Designing Ruby APIsDesigning Ruby APIs
Designing Ruby APIs
 
Ruby 程式語言入門導覽
Ruby 程式語言入門導覽Ruby 程式語言入門導覽
Ruby 程式語言入門導覽
 
Ruby 入門 第一次就上手
Ruby 入門 第一次就上手Ruby 入門 第一次就上手
Ruby 入門 第一次就上手
 
Ruby
RubyRuby
Ruby
 
Code for Startup MVP (Ruby on Rails) Session 2
Code for Startup MVP (Ruby on Rails) Session 2Code for Startup MVP (Ruby on Rails) Session 2
Code for Startup MVP (Ruby on Rails) Session 2
 
Elegant APIs
Elegant APIsElegant APIs
Elegant APIs
 
Rails by example
Rails by exampleRails by example
Rails by example
 
Ruby on Rails
Ruby on RailsRuby on Rails
Ruby on Rails
 
Perl 5.10
Perl 5.10Perl 5.10
Perl 5.10
 
Ruby on Rails 中級者を目指して - 大場寧子
Ruby on Rails 中級者を目指して - 大場寧子Ruby on Rails 中級者を目指して - 大場寧子
Ruby on Rails 中級者を目指して - 大場寧子
 
Metaprogramovanie #1
Metaprogramovanie #1Metaprogramovanie #1
Metaprogramovanie #1
 
A tour on ruby and friends
A tour on ruby and friendsA tour on ruby and friends
A tour on ruby and friends
 
Ruby from zero to hero
Ruby from zero to heroRuby from zero to hero
Ruby from zero to hero
 
Programming in perl style
Programming in perl styleProgramming in perl style
Programming in perl style
 
Slides chapter3part1 ruby-forjavaprogrammers
Slides chapter3part1 ruby-forjavaprogrammersSlides chapter3part1 ruby-forjavaprogrammers
Slides chapter3part1 ruby-forjavaprogrammers
 
Ruby - Uma Introdução
Ruby - Uma IntroduçãoRuby - Uma Introdução
Ruby - Uma Introdução
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
 
Ruby_Coding_Convention
Ruby_Coding_ConventionRuby_Coding_Convention
Ruby_Coding_Convention
 

Recently uploaded

04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 

Recently uploaded (20)

04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 

Ruby 2.0

  • 1. RUBY 2.0 ugisozols.com
  • 2. Refinments module  StringPatch    refine  String  do        def  is_number?            !!match(/^d+$/)        end    end end   "1".is_number?  #  =>  undefined  method  `is_number?'  ...   using  StringPatch   "1".is_number?  #  =>  true "a".is_number?  #  =>  false
  • 3. Refinments $  ruby  string_patch.rb      string_patch.rb:2:  warning:  Refinements  are  experimental,    and  the  behavior  may  change  in  future  versions  of  Ruby!
  • 4. Module#prepend module  A    def  foo;  puts  "A";  end end   class  B    include  A      def  foo;  puts  "B";  end end   klass  =  B.new klass.foo      #  =>  "B" B.ancestors  #  =>  [B,  A,  Object,  Kernel,  BasicObject]
  • 5. Module#prepend module  A    def  foo;  puts  "A";  end end   class  B    prepend  A  #  <-­‐  prepend  instead  of  include      def  foo;  puts  "B";  end end   klass  =  B.new klass.foo      #  =>  "A" B.ancestors  #  =>  [A,  B,  Object,  Kernel,  BasicObject]
  • 6. Module#prepend require  "active_support/core_ext/module/aliasing"   class  A    def  foo;  puts  "foo";  end end   class  A    def  foo_with_bar        foo_without_bar        puts  "bar"    end      alias_method_chain  :foo,  :bar end   A.new.foo      #  =>  "foo  bar" A.ancestors  #  =>  [A,  Object,  Kernel,  BasicObject]
  • 7. Module#prepend class  A    def  foo;  puts  "foo";  end end   module  Extension    def  foo        super        puts  "bar"    end end   class  A    prepend  Extension end   A.new.foo      #  =>  "foo  bar" A.ancestors  #  =>  [Extension,  A,  Object,  Kernel,  BasicObject]
  • 8. Keyword Arguments def  link_to(name,  url_options  =  {},  html_options  =  {})    puts  "name  =  #{name.inspect}"    puts  "url_options  =  #{url_options.inspect}"    puts  "html_options  =  #{html_options.inspect}" end   link_to("Ruby  1.9",  {},  {:class  =>  "styled-­‐link"}) #  name  =  "Ruby  1.9" #  url_options  =  {} #  html_options  =  {:class=>"styled-­‐link"}
  • 9. Keyword Arguments def  link_to(name,  url_options:  {},  html_options:  {})    puts  "name  =  #{name.inspect}"    puts  "url_options  =  #{url_options.inspect}"    puts  "html_options  =  #{html_options.inspect}" end   link_to("Ruby  2.0",                :html_options  =>  {:class  =>  "styled-­‐link"}) #  name  =  "Ruby  2.0" #  url_options  =  {} #  html_options  =  {:class=>"styled-­‐link"}
  • 10. Keyword Arguments def  temperature(celsius,  fahrenheit:  to_fahrenheit(celsius))    puts  "#{celsius}°C"    puts  "#{fahrenheit}°F" end   def  to_fahrenheit(celsius)    celsius  *  9  /  5  +  32 end   temperature(36) #  36°C #  96°F
  • 11. Enumerable#lazy words  =  File.open('/usr/share/dict/words')                        .map(&:chomp)                        .reject  {  |word|  word.length  <  4  }                        .take(10)   words  #  =>  ["aalii",  "Aani",  "aardvark",  ...] #  time  ruby  lazy.rb  ~  0.22s
  • 12. Enumerable#lazy words  =  File.open('/usr/share/dict/words')                        .lazy  #  <-­‐-­‐  added  lazy  here                        .map(&:chomp)                        .reject  {  |word|  word.length  <  4  }                        .take(10)   words            #  =>  #<Enumerator::Lazy:  ...] words.to_a  #  =>  ["aalii",  "Aani",  "aardvark",  ...]   #  time  ruby  lazy.rb  ~  0.06s
  • 13. Enumerable#lazy require  "fruity"   range  =  1..100   compare  do    without_lazy  {  range.          map  {  |el|  el  *  2  }  }    with_lazy        {  range.lazy.map  {  |el|  el  *  2  }.to_a  } end   #  Running  each  test  256  times. #  without_lazy  is  faster  than  with_lazy  by  4x  ±  0.1
  • 14. %i and %I symbol literals %i(foo  bar  baz)  #  =>  [:foo,  :bar,  :baz]   %I(foo  b#{2+2}r  baz)  #  =>  [:foo,  :b4r,  :baz]
  • 15. Default source encoding is UTF-8 #  Ruby  1.9 #  encoding:  utf-­‐8 #  without  ^  next  line  will  throw  "invalid  multibyte  char  (US-­‐ ASCII)" name  =  "Uģis  Ozols" #  Ruby  2.0 name  =  "Uģis  Ozols"
  • 16. #to_h Ruby  =  Struct.new(:version) ruby  =  Ruby.new("2.0")   ruby.to_h  #  =>  {  :version  =>  "2.0"  } def  config(options)    if  options.respond_to?(:to_h)        #  consume  hash    else        raise  TypeErorr,  "Can't  convert  #{options.inspect}  into   Hash"    end end
  • 17. #respond_to? returns false for protected methods class  A    protected      def  foo        "A"    end end   klass  =  A.new klass.respond_to?(:foo)  #=>  false klass.respond_to?(:foo,  true)  #=>  true
  • 18. #require optimizations Rails  4.0.0.beta1  application   rails  g  scaffold  user  name   time  rake  test Ruby  1.9.3-­‐p392 ~  12  seconds   time  rake  test Ruby  2.0.0-­‐p0 ~  5  seconds
  • 19. Thank you for listening! ugisozols.com