SlideShare une entreprise Scribd logo
1  sur  36
Programming Production Code
 as if writing Natural Language




      Kazuya NUMATA
         @kaznum
Self-introduction
                        (Kazuya NUMATA)
  •
  •                          �


  •
  •
  •
      ���������������


  •
My favorite


•
    •
    •
Agile
Communication with team members
     including stakeholder
                &
        Iterative Testing
Behavior Driven Development
             BDD
Tools
to make BDD effective
•   RSpec (Ruby)

•   NSpec (.NET)

•   CppSpec (C++)

•   JBehave (Java)

•   instinct (Java)

•   PHPSpec (PHP)

•   and more....
Question
Question




   Have you ever experienced *Spec?


*Spec                                 ?
RSpec example:



describe "A new chess board" do

   before(:each) do
     @board = Chess::Board.new
   end

   it "should have 32 pieces" do
     @board.should have(32).pieces
   end

end
                               D. Chelimsky. The RSpec Book
                          Section 13.7 Generated Descriptions
RSpec example:



describe "A new chess board" do

   before(:each) do
     @board = Chess::Board.new
   end

   it "should have 32 pieces" do
     @board.should have(32).pieces
   end

end
                               D. Chelimsky. The RSpec Book
                          Section 13.7 Generated Descriptions
RSpec example:



$ rspec -fd board_spec.rb

(Output is...)

A new chess board
  should have 32 pieces

                       D. Chelimsky. The RSpec Book
                  Section 13.7 Generated Descriptions
It is just Natural Language!
�
The reality is that it’s hard for the Japanese.
Cucumber

(              )
Cucumber Example:


         :

         :


         "           "    "                 "
         "       "   "        Cucumber"
         "       "
             "           Cucumber"

                                                                   ( ∀ )o   sasata299's blog
                                     http://blog.livedoor.jp/sasata299/archives/51278697.html
/Spec




Before
 BDD


After    Spec    Spec
BDD
Before
 BDD


After
BDD
?
Uncle Bob

   Complex fulcrumPoint = Complex.FromRealNumber(23.0);

is generally better than

   Complex fulcrumPoint = new Complex(23.0);

Consider enforcing their use by making the corresponding constructors private.



                           (   )
by Masayoshi Son
Person
         name
                        true
                false
Ruby Example:   name   1

ex1


class Person
  attr_accessor :name
  def initialize(name = nil)
    @name = name
  end
end

shimada = Person.new(“Shimada”)
if shimada.name.split(//)[0] == “S”
  puts “Match”
end
Ruby Example: name   1

ex2


class Person
  attr_accessor :name
  def initialize(name = nil)
    @name = name
  end

  def has_a_name_that_begins_with?(expected)
    @name.split(//)[0] == expected
  end
end

shimada = Person.new(“Shimada”)
if shimada.has_a_name_that_begins_with?(“S”)
  puts “Match”
end
Ruby Example: name    1

ex3

class Person
  attr_accessor :name
  def initialize(name = nil)
    @name = name
  end

  def has_a_name_that_begins_with?(expected)
    @name.split(//)[0] == expected
  end

  def method_missing(m, *args, &block)
    if m.to_s =~ /has_(a|the)_name_(that|which)_begins_with?/
      has_a_name_that_begins_with?(args)
    else
      super
    end
  end
end

shimada = Person.new(“Shimada”)
puts “Match” if shimada.has_a_name_that_begins_with?(“S”)
puts “Match” if shimada.has_the_name_that_begins_with?(“S”)
puts “Match” if shimada.has_a_name_which_begins_with?(“S”)
puts “Match” if shimada.has_the_name_which_begins_with?(“S”)
Ruby Example: name                           1
ex4      name                                                                                     String
                                                                   has_(a|the)_(.+)_(which|that)_begins_with?
Dynamic                    setter
             undef                         Singleton Method
class Person
  def initialize(args)
    args.each do |key, value|
      self.class.send(:attr_accessor, :"#{key}") unless instance_variables.include?(:"@#{key}")
      instance_variable_set(:"@#{key}", value)
    end
  end

  def self.undef_has_a_var_which_begins_with(var)
    method_name = "has_a_#{var}_which_begins_with?"
    send("undef_method", method_name) if method_defined? method_name
  end

  def self.define_has_a_var_which_begins_with(var)
    method_name = "has_a_#{var}_which_begins_with?"
    define_method(method_name) do |expectation|
      value = instance_variable_get(:"@#{var}")
      if value.is_a?(String)
        value.split(//)[0].downcase == expectation.downcase
      else
        self.class.undef_has_a_var_which_begins_with(var)
        method_missing(method_name.to_sym)
      end
    end
  end

  def method_missing(m, *args, &block)
    if m.to_s =~ /has_(a|the)_(.+)_(which|that)_begins_with?/ &&
         instance_variables.include?(:"@#{Regexp.last_match[2]}") &&
         instance_variable_get(:"@#{Regexp.last_match[2]}").is_a?(String)
      var_name = Regexp.last_match[2]
      self.class.define_has_a_var_which_begins_with(var_name)
      send("has_a_#{var_name}_which_begins_with?".to_sym, *args, &block)
    else
      super(m, *args, &block)
    end
  end
end

#### sample #####
shimada = Person.new(:name => "Shimada", :age => 18, :sex => :half, :fuga => "bababa")
p shimada.has_a_name_which_begins_with?("S") ? "Good" : "Suck"
p shimada.has_the_name_which_begins_with?("S") ? "Good" : "Suck"
shimada.name = 5
p shimada.has_the_name_which_begins_with?("5") ? "Good" : "Suck" #=> undefined method
if air_plane.is_created_by? :bowing
    if he.is_eligible_to_get? reward
    if selected_value.is_valid?


→
    can_..., is_,
is does was did
module Kernel
  def is(expected)
    expected
  end

 alias_method :does, :is
 alias_method :was, :is
 alias_method :were, :is
 alias_method :did, :is
 # alias_method :do, :is       × do
end

is air_plane.created_by?(bowing)
does [1,2,3].include?
is he.eligible_to_get?(bonus)
does [1,2,3].include?
is "h".included_in?("hello")
is she, nancy   # ?
Conclusion & Impression

•   Spec



•

•
      •                                                                          Receiver
                                                                            Array#sort


      •            Ruby Standard Library         if obj.instance_of?(exp)   be
                                           (Is                  Object#is   Is#method_missing
           self   return...)


      •
      •
      •
thank you :)

Contenu connexe

Tendances

CodeFest 2010. Иноземцев И. — Fantom. Cross-VM Language
CodeFest 2010. Иноземцев И. — Fantom. Cross-VM LanguageCodeFest 2010. Иноземцев И. — Fantom. Cross-VM Language
CodeFest 2010. Иноземцев И. — Fantom. Cross-VM Language
CodeFest
 
Rapid Development with Ruby/JRuby and Rails
Rapid Development with Ruby/JRuby and RailsRapid Development with Ruby/JRuby and Rails
Rapid Development with Ruby/JRuby and Rails
elliando dias
 
Slides chapter3part1 ruby-forjavaprogrammers
Slides chapter3part1 ruby-forjavaprogrammersSlides chapter3part1 ruby-forjavaprogrammers
Slides chapter3part1 ruby-forjavaprogrammers
Giovanni924
 
Ruby 入門 第一次就上手
Ruby 入門 第一次就上手Ruby 入門 第一次就上手
Ruby 入門 第一次就上手
Wen-Tien Chang
 
Perl.Hacks.On.Vim
Perl.Hacks.On.VimPerl.Hacks.On.Vim
Perl.Hacks.On.Vim
Lin Yo-An
 

Tendances (20)

Ruby - Uma Introdução
Ruby - Uma IntroduçãoRuby - Uma Introdução
Ruby - Uma Introdução
 
CodeFest 2010. Иноземцев И. — Fantom. Cross-VM Language
CodeFest 2010. Иноземцев И. — Fantom. Cross-VM LanguageCodeFest 2010. Иноземцев И. — Fantom. Cross-VM Language
CodeFest 2010. Иноземцев И. — Fantom. Cross-VM Language
 
Coffee Script
Coffee ScriptCoffee Script
Coffee Script
 
Exhibition of Atrocity
Exhibition of AtrocityExhibition of Atrocity
Exhibition of Atrocity
 
Exploring Ceylon with Gavin King - JUG BB Talk - Belrin 2014
Exploring Ceylon with Gavin King - JUG BB Talk - Belrin 2014Exploring Ceylon with Gavin King - JUG BB Talk - Belrin 2014
Exploring Ceylon with Gavin King - JUG BB Talk - Belrin 2014
 
Rapid Development with Ruby/JRuby and Rails
Rapid Development with Ruby/JRuby and RailsRapid Development with Ruby/JRuby and Rails
Rapid Development with Ruby/JRuby and Rails
 
Slides chapter3part1 ruby-forjavaprogrammers
Slides chapter3part1 ruby-forjavaprogrammersSlides chapter3part1 ruby-forjavaprogrammers
Slides chapter3part1 ruby-forjavaprogrammers
 
Ruby 入門 第一次就上手
Ruby 入門 第一次就上手Ruby 入門 第一次就上手
Ruby 入門 第一次就上手
 
Real world gobbledygook
Real world gobbledygookReal world gobbledygook
Real world gobbledygook
 
Coffeescript: No really, it's just Javascript
Coffeescript: No really, it's just JavascriptCoffeescript: No really, it's just Javascript
Coffeescript: No really, it's just Javascript
 
Ruby from zero to hero
Ruby from zero to heroRuby from zero to hero
Ruby from zero to hero
 
Object Oriented JavaScript
Object Oriented JavaScriptObject Oriented JavaScript
Object Oriented JavaScript
 
1 the ruby way
1   the ruby way1   the ruby way
1 the ruby way
 
Descobrindo a linguagem Perl
Descobrindo a linguagem PerlDescobrindo a linguagem Perl
Descobrindo a linguagem Perl
 
Everything is Permitted: Extending Built-ins
Everything is Permitted: Extending Built-insEverything is Permitted: Extending Built-ins
Everything is Permitted: Extending Built-ins
 
Introduction to Ruby Programming Language
Introduction to Ruby Programming LanguageIntroduction to Ruby Programming Language
Introduction to Ruby Programming Language
 
Hacker 101/102 - Introduction to Programming w/Processing
Hacker 101/102 - Introduction to Programming w/ProcessingHacker 101/102 - Introduction to Programming w/Processing
Hacker 101/102 - Introduction to Programming w/Processing
 
Beginning Object-Oriented JavaScript
Beginning Object-Oriented JavaScriptBeginning Object-Oriented JavaScript
Beginning Object-Oriented JavaScript
 
purely_functional_play_framework_application
purely_functional_play_framework_applicationpurely_functional_play_framework_application
purely_functional_play_framework_application
 
Perl.Hacks.On.Vim
Perl.Hacks.On.VimPerl.Hacks.On.Vim
Perl.Hacks.On.Vim
 

Similaire à あたかも自然言語を書くようにコーディングしてみる

Why Java Sucks and C# Rocks (Final)
Why Java Sucks and C# Rocks (Final)Why Java Sucks and C# Rocks (Final)
Why Java Sucks and C# Rocks (Final)
jeffz
 
Uses & Abuses of Mocks & Stubs
Uses & Abuses of Mocks & StubsUses & Abuses of Mocks & Stubs
Uses & Abuses of Mocks & Stubs
PatchSpace Ltd
 
Write Your Own JVM Compiler
Write Your Own JVM CompilerWrite Your Own JVM Compiler
Write Your Own JVM Compiler
Erin Dees
 
AST - the only true tool for building JavaScript
AST - the only true tool for building JavaScriptAST - the only true tool for building JavaScript
AST - the only true tool for building JavaScript
Ingvar Stepanyan
 

Similaire à あたかも自然言語を書くようにコーディングしてみる (20)

Language supports it
Language supports itLanguage supports it
Language supports it
 
Petitparser at the Deep into Smalltalk School 2011
Petitparser at the Deep into Smalltalk School 2011Petitparser at the Deep into Smalltalk School 2011
Petitparser at the Deep into Smalltalk School 2011
 
Threequals - Case Equality in Ruby
Threequals - Case Equality in RubyThreequals - Case Equality in Ruby
Threequals - Case Equality in Ruby
 
Why Java Sucks and C# Rocks (Final)
Why Java Sucks and C# Rocks (Final)Why Java Sucks and C# Rocks (Final)
Why Java Sucks and C# Rocks (Final)
 
Crystal presentation in NY
Crystal presentation in NYCrystal presentation in NY
Crystal presentation in NY
 
Hacking with ruby2ruby
Hacking with ruby2rubyHacking with ruby2ruby
Hacking with ruby2ruby
 
A limited guide to intermediate and advanced Ruby
A limited guide to intermediate and advanced RubyA limited guide to intermediate and advanced Ruby
A limited guide to intermediate and advanced Ruby
 
Ruby presentasjon på NTNU 22 april 2009
Ruby presentasjon på NTNU 22 april 2009Ruby presentasjon på NTNU 22 april 2009
Ruby presentasjon på NTNU 22 april 2009
 
Ruby presentasjon på NTNU 22 april 2009
Ruby presentasjon på NTNU 22 april 2009Ruby presentasjon på NTNU 22 april 2009
Ruby presentasjon på NTNU 22 april 2009
 
Ruby presentasjon på NTNU 22 april 2009
Ruby presentasjon på NTNU 22 april 2009Ruby presentasjon på NTNU 22 april 2009
Ruby presentasjon på NTNU 22 april 2009
 
Tork03 LT
Tork03 LT Tork03 LT
Tork03 LT
 
Making JavaScript Libraries More Approachable
Making JavaScript Libraries More ApproachableMaking JavaScript Libraries More Approachable
Making JavaScript Libraries More Approachable
 
jRuby: The best of both worlds
jRuby: The best of both worldsjRuby: The best of both worlds
jRuby: The best of both worlds
 
Uses & Abuses of Mocks & Stubs
Uses & Abuses of Mocks & StubsUses & Abuses of Mocks & Stubs
Uses & Abuses of Mocks & Stubs
 
Postobjektové programovanie v Ruby
Postobjektové programovanie v RubyPostobjektové programovanie v Ruby
Postobjektové programovanie v Ruby
 
Playfulness at Work
Playfulness at WorkPlayfulness at Work
Playfulness at Work
 
RSpec 3.0: Under the Covers
RSpec 3.0: Under the CoversRSpec 3.0: Under the Covers
RSpec 3.0: Under the Covers
 
Write Your Own JVM Compiler
Write Your Own JVM CompilerWrite Your Own JVM Compiler
Write Your Own JVM Compiler
 
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
 
AST - the only true tool for building JavaScript
AST - the only true tool for building JavaScriptAST - the only true tool for building JavaScript
AST - the only true tool for building JavaScript
 

Dernier

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 

Dernier (20)

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?
 
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
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
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...
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
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
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
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
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
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
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
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...
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 

あたかも自然言語を書くようにコーディングしてみる

  • 1. Programming Production Code as if writing Natural Language Kazuya NUMATA @kaznum
  • 2. Self-introduction (Kazuya NUMATA) • • � • • • ��������������� •
  • 3. My favorite • • •
  • 5. Communication with team members including stakeholder & Iterative Testing
  • 7. Tools to make BDD effective
  • 8. RSpec (Ruby) • NSpec (.NET) • CppSpec (C++) • JBehave (Java) • instinct (Java) • PHPSpec (PHP) • and more....
  • 10. Question Have you ever experienced *Spec? *Spec ?
  • 11. RSpec example: describe "A new chess board" do before(:each) do @board = Chess::Board.new end it "should have 32 pieces" do @board.should have(32).pieces end end D. Chelimsky. The RSpec Book Section 13.7 Generated Descriptions
  • 12. RSpec example: describe "A new chess board" do before(:each) do @board = Chess::Board.new end it "should have 32 pieces" do @board.should have(32).pieces end end D. Chelimsky. The RSpec Book Section 13.7 Generated Descriptions
  • 13. RSpec example: $ rspec -fd board_spec.rb (Output is...) A new chess board should have 32 pieces D. Chelimsky. The RSpec Book Section 13.7 Generated Descriptions
  • 14. It is just Natural Language!
  • 15.
  • 16. The reality is that it’s hard for the Japanese.
  • 17. Cucumber (
  • 18. Cucumber Example: : : " " " " " " " Cucumber" " " " Cucumber" ( ∀ )o sasata299's blog http://blog.livedoor.jp/sasata299/archives/51278697.html
  • 19.
  • 20. /Spec Before BDD After Spec Spec BDD
  • 22.
  • 23.
  • 24.
  • 25. Uncle Bob Complex fulcrumPoint = Complex.FromRealNumber(23.0); is generally better than Complex fulcrumPoint = new Complex(23.0); Consider enforcing their use by making the corresponding constructors private. ( )
  • 27. Person name true false
  • 28. Ruby Example: name 1 ex1 class Person attr_accessor :name def initialize(name = nil) @name = name end end shimada = Person.new(“Shimada”) if shimada.name.split(//)[0] == “S” puts “Match” end
  • 29. Ruby Example: name 1 ex2 class Person attr_accessor :name def initialize(name = nil) @name = name end def has_a_name_that_begins_with?(expected) @name.split(//)[0] == expected end end shimada = Person.new(“Shimada”) if shimada.has_a_name_that_begins_with?(“S”) puts “Match” end
  • 30. Ruby Example: name 1 ex3 class Person attr_accessor :name def initialize(name = nil) @name = name end def has_a_name_that_begins_with?(expected) @name.split(//)[0] == expected end def method_missing(m, *args, &block) if m.to_s =~ /has_(a|the)_name_(that|which)_begins_with?/ has_a_name_that_begins_with?(args) else super end end end shimada = Person.new(“Shimada”) puts “Match” if shimada.has_a_name_that_begins_with?(“S”) puts “Match” if shimada.has_the_name_that_begins_with?(“S”) puts “Match” if shimada.has_a_name_which_begins_with?(“S”) puts “Match” if shimada.has_the_name_which_begins_with?(“S”)
  • 31. Ruby Example: name 1 ex4 name String has_(a|the)_(.+)_(which|that)_begins_with? Dynamic setter undef Singleton Method class Person def initialize(args) args.each do |key, value| self.class.send(:attr_accessor, :"#{key}") unless instance_variables.include?(:"@#{key}") instance_variable_set(:"@#{key}", value) end end def self.undef_has_a_var_which_begins_with(var) method_name = "has_a_#{var}_which_begins_with?" send("undef_method", method_name) if method_defined? method_name end def self.define_has_a_var_which_begins_with(var) method_name = "has_a_#{var}_which_begins_with?" define_method(method_name) do |expectation| value = instance_variable_get(:"@#{var}") if value.is_a?(String) value.split(//)[0].downcase == expectation.downcase else self.class.undef_has_a_var_which_begins_with(var) method_missing(method_name.to_sym) end end end def method_missing(m, *args, &block) if m.to_s =~ /has_(a|the)_(.+)_(which|that)_begins_with?/ && instance_variables.include?(:"@#{Regexp.last_match[2]}") && instance_variable_get(:"@#{Regexp.last_match[2]}").is_a?(String) var_name = Regexp.last_match[2] self.class.define_has_a_var_which_begins_with(var_name) send("has_a_#{var_name}_which_begins_with?".to_sym, *args, &block) else super(m, *args, &block) end end end #### sample ##### shimada = Person.new(:name => "Shimada", :age => 18, :sex => :half, :fuga => "bababa") p shimada.has_a_name_which_begins_with?("S") ? "Good" : "Suck" p shimada.has_the_name_which_begins_with?("S") ? "Good" : "Suck" shimada.name = 5 p shimada.has_the_name_which_begins_with?("5") ? "Good" : "Suck" #=> undefined method
  • 32.
  • 33. if air_plane.is_created_by? :bowing if he.is_eligible_to_get? reward if selected_value.is_valid? → can_..., is_,
  • 34. is does was did module Kernel def is(expected) expected end alias_method :does, :is alias_method :was, :is alias_method :were, :is alias_method :did, :is # alias_method :do, :is × do end is air_plane.created_by?(bowing) does [1,2,3].include? is he.eligible_to_get?(bonus) does [1,2,3].include? is "h".included_in?("hello") is she, nancy # ?
  • 35. Conclusion & Impression • Spec • • • Receiver Array#sort • Ruby Standard Library if obj.instance_of?(exp) be (Is Object#is Is#method_missing self return...) • • •

Notes de l'éditeur

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n
  28. \n
  29. \n
  30. \n
  31. \n
  32. \n
  33. \n
  34. \n
  35. \n
  36. \n