SlideShare una empresa de Scribd logo
1 de 28
Removing Methods
 Ruby Method of the Month
  Kevin Munc => @muncman
Adding a Method

class String
  def ruby_rocks?
    true #dat
  end
end
Fancy New Method


"rails".ruby_rocks?
      => true
Remove It!
class String
  remove_method :ruby_rocks?
end
Remove It!
class String
  remove_method :ruby_rocks?
end
"rails".ruby_rocks?
NoMethodError: undefined method
`ruby_rocks?' for "rails":String
A Redefined Method

class Crb
  def to_s
    "Thanks Randall!"
  end
end
Redefinition in
        Action
columbusrb = Crb.new
Redefinition in
        Action
columbusrb = Crb.new

columbusrb.to_s
Redefinition in
        Action
columbusrb = Crb.new

columbusrb.to_s
=> "Thanks Randall!"
Redefinition Removal
class Crb
  remove_method :to_s
end
                        Note that we didn’t have
                        to get a new instance for
                        the removal to take
                        effect.
Redefinition Removal
class Crb
  remove_method :to_s
end

columbusrb.to_s
                        Note that we didn’t have
                        to get a new instance for
                        the removal to take
                        effect.
Redefinition Removal
class Crb
  remove_method :to_s
end

columbusrb.to_s
                          Note that we didn’t have
                          to get a new instance for
                          the removal to take
                          effect.

=> "#<Crb:0x101651538>"
Inherited Method
ror = “Ruby on Rails”
ror.to_s
=> "Ruby on Rails"


                        We get the to_s from Object.

                        Note that the removal takes
                        effect for instances already
                        declared.
Inherited Method
ror = “Ruby on Rails”
ror.to_s
=> "Ruby on Rails"
class String
  remove_method :to_s   We get the to_s from Object.


end                     Note that the removal takes
                        effect for instances already
                        declared.
Inherited Method
ror = “Ruby on Rails”
ror.to_s
=> "Ruby on Rails"
class String
  remove_method :to_s    We get the to_s from Object.


end                      Note that the removal takes
                         effect for instances already
                         declared.


ror.to_s
=> "#<String:0x101682b88>"
Removal Location
class Object
  remove_method :to_s
end


                        Defined in Kernel.

                        Object mixes in Kernel.
Removal Location
class Object
  remove_method :to_s
end

NameError: method `to_s' not
defined in Object       Defined in Kernel.

                        Object mixes in Kernel.
Put It All Back
           Starting from a clean slate, so String still has to_s, etc.




class Crb
  def to_s
    "Thanks Randall!"
  end
end
Undefine the
        Redefine       Different than removal!




class Crb
  undef_method :to_s
end
                       Interesting that the
                       default output of to_s
                       is present in the output.
Undefine the
        Redefine       Different than removal!




class Crb
  undef_method :to_s
end
                       Interesting that the
                       default output of to_s
                       is present in the output.

columbusrb.to_s
Undefine the
         Redefine          Different than removal!




class Crb
  undef_method :to_s
end
                           Interesting that the
                           default output of to_s
                           is present in the output.

columbusrb.to_s
NoMethodError: undefined method
`to_s' for #<Crb:0x101651538>
Definitely Undefined
class String
  undef_method :to_s
end
                       Note that the undef also applied
                       to subclasses of the type where
                       the undef happened.
Definitely Undefined
class String
  undef_method :to_s
end
                         Note that the undef also applied
                         to subclasses of the type where
                         the undef happened.

"rails".to_s
NoMethodError: undefined method
`to_s' for "rails":String
Defined Elsewhere
class Crb
  undef_method :object_id
end


                            The undef succeeds
                            because the method is
                            available to class Crb.
Defined Elsewhere
class Crb
  undef_method :object_id
end

columbusrb = Crb.new        The undef succeeds
                            because the method is

columbusrb.object_id        available to class Crb.



NoMethodError: undefined method
`object_id' for #<Crb:0x10166d7d8>
Method Removal
remove_method :symbol
 Removed from the specific type only.

 Ruby still searches up the hierarchy.

undef_method :symbol
 Prevents calls to the method regardless
 of the type’s hierarchy.

Both defined in the Module class.
Finer Points
  (pun intended)
You can call remove_method only
on a method actually defined
within the class itself.

You can call undef_method on a
method accessible from the
class (inherited or mixed in).
Questions?
                Photo Credits:
    http://www.flickr.com/photos/tomscott/4225623/
 http://www.flickr.com/photos/slayer23/2215384832/
http://www.flickr.com/photos/andreweason/268627274/
   http://www.flickr.com/photos/tomscott/28188883/
  http://www.flickr.com/photos/jonmikel/259535350/

Más contenido relacionado

Destacado

Ruby's String Slicing (MOTM 2009.07)
Ruby's String Slicing (MOTM 2009.07)Ruby's String Slicing (MOTM 2009.07)
Ruby's String Slicing (MOTM 2009.07)Kevin Munc
 
Therapy throuw photography
Therapy throuw photographyTherapy throuw photography
Therapy throuw photographyalinefrisch
 
Google Automotive Report by 7thFloor
Google Automotive Report by 7thFloorGoogle Automotive Report by 7thFloor
Google Automotive Report by 7thFloorAndrea Genovese
 
Collaborate to Innovate: a new venture capital model
Collaborate to Innovate: a new venture capital modelCollaborate to Innovate: a new venture capital model
Collaborate to Innovate: a new venture capital modelAndrea Genovese
 
Body Talks....Le Voyage
Body Talks....Le Voyage  Body Talks....Le Voyage
Body Talks....Le Voyage alinefrisch
 
Editoria Viaggi Meridiani2
Editoria Viaggi Meridiani2Editoria Viaggi Meridiani2
Editoria Viaggi Meridiani2Andrea Genovese
 
7thFloor Business Magazine n. 12
7thFloor Business Magazine n. 127thFloor Business Magazine n. 12
7thFloor Business Magazine n. 12Andrea Genovese
 
Augusto Coppola Business Plan-innovation-lab
Augusto Coppola Business Plan-innovation-labAugusto Coppola Business Plan-innovation-lab
Augusto Coppola Business Plan-innovation-labAndrea Genovese
 
Tiny Frogs
Tiny FrogsTiny Frogs
Tiny FrogsSINGHA
 
Obama Social Media Marketing & Web Communication Strategy
Obama Social Media Marketing & Web Communication StrategyObama Social Media Marketing & Web Communication Strategy
Obama Social Media Marketing & Web Communication StrategyAndrea Genovese
 
Tiny Frogs
Tiny FrogsTiny Frogs
Tiny FrogsSINGHA
 

Destacado (13)

Ruby's String Slicing (MOTM 2009.07)
Ruby's String Slicing (MOTM 2009.07)Ruby's String Slicing (MOTM 2009.07)
Ruby's String Slicing (MOTM 2009.07)
 
Therapy throuw photography
Therapy throuw photographyTherapy throuw photography
Therapy throuw photography
 
Google Automotive Report by 7thFloor
Google Automotive Report by 7thFloorGoogle Automotive Report by 7thFloor
Google Automotive Report by 7thFloor
 
Presenta 7thfloor
Presenta 7thfloorPresenta 7thfloor
Presenta 7thfloor
 
Collaborate to Innovate: a new venture capital model
Collaborate to Innovate: a new venture capital modelCollaborate to Innovate: a new venture capital model
Collaborate to Innovate: a new venture capital model
 
Body Talks....Le Voyage
Body Talks....Le Voyage  Body Talks....Le Voyage
Body Talks....Le Voyage
 
Editoria Viaggi Meridiani2
Editoria Viaggi Meridiani2Editoria Viaggi Meridiani2
Editoria Viaggi Meridiani2
 
Fcl Retail 07 7th Floor
Fcl Retail 07 7th FloorFcl Retail 07 7th Floor
Fcl Retail 07 7th Floor
 
7thFloor Business Magazine n. 12
7thFloor Business Magazine n. 127thFloor Business Magazine n. 12
7thFloor Business Magazine n. 12
 
Augusto Coppola Business Plan-innovation-lab
Augusto Coppola Business Plan-innovation-labAugusto Coppola Business Plan-innovation-lab
Augusto Coppola Business Plan-innovation-lab
 
Tiny Frogs
Tiny FrogsTiny Frogs
Tiny Frogs
 
Obama Social Media Marketing & Web Communication Strategy
Obama Social Media Marketing & Web Communication StrategyObama Social Media Marketing & Web Communication Strategy
Obama Social Media Marketing & Web Communication Strategy
 
Tiny Frogs
Tiny FrogsTiny Frogs
Tiny Frogs
 

Similar a Removing Methods (MOTM 2010.01)

The Dark Art of Rails Plugins (2008)
The Dark Art of Rails Plugins (2008)The Dark Art of Rails Plugins (2008)
The Dark Art of Rails Plugins (2008)lazyatom
 
Introduction to Ruby’s Reflection API
Introduction to Ruby’s Reflection APIIntroduction to Ruby’s Reflection API
Introduction to Ruby’s Reflection APINiranjan Sarade
 
Metaprogramming in Ruby
Metaprogramming in RubyMetaprogramming in Ruby
Metaprogramming in RubyConFoo
 
Metaprogramming
MetaprogrammingMetaprogramming
Metaprogrammingjoshbuddy
 
Ruby object model at the Ruby drink-up of Sophia, January 2013
Ruby object model at the Ruby drink-up of Sophia, January 2013Ruby object model at the Ruby drink-up of Sophia, January 2013
Ruby object model at the Ruby drink-up of Sophia, January 2013rivierarb
 
Magic in ruby
Magic in rubyMagic in ruby
Magic in rubyDavid Lin
 
Metaprogramming Primer (Part 1)
Metaprogramming Primer (Part 1)Metaprogramming Primer (Part 1)
Metaprogramming Primer (Part 1)Christopher Haupt
 
Rails for zombies_2_cheatsheets
Rails for zombies_2_cheatsheetsRails for zombies_2_cheatsheets
Rails for zombies_2_cheatsheetsedith890
 
Introduction to Ruby MagLev
Introduction to Ruby MagLevIntroduction to Ruby MagLev
Introduction to Ruby MagLevrengelbrecht
 
Forbidden Fruit: A Taste of Ruby's ParseTree
Forbidden Fruit: A Taste of Ruby's ParseTreeForbidden Fruit: A Taste of Ruby's ParseTree
Forbidden Fruit: A Taste of Ruby's ParseTreeerr
 
Ruby: Beyond the Basics
Ruby: Beyond the BasicsRuby: Beyond the Basics
Ruby: Beyond the BasicsMichael Koby
 
Ruby object model - Understanding of object play role for ruby
Ruby object model - Understanding of object play role for rubyRuby object model - Understanding of object play role for ruby
Ruby object model - Understanding of object play role for rubyTushar Pal
 
Metaprogramming code-that-writes-code
Metaprogramming code-that-writes-codeMetaprogramming code-that-writes-code
Metaprogramming code-that-writes-codeorga shih
 
Ruby for C# Developers
Ruby for C# DevelopersRuby for C# Developers
Ruby for C# DevelopersCory Foy
 

Similar a Removing Methods (MOTM 2010.01) (20)

The Dark Art of Rails Plugins (2008)
The Dark Art of Rails Plugins (2008)The Dark Art of Rails Plugins (2008)
The Dark Art of Rails Plugins (2008)
 
How to write Ruby extensions with Crystal
How to write Ruby extensions with CrystalHow to write Ruby extensions with Crystal
How to write Ruby extensions with Crystal
 
Introduction to Ruby’s Reflection API
Introduction to Ruby’s Reflection APIIntroduction to Ruby’s Reflection API
Introduction to Ruby’s Reflection API
 
block
blockblock
block
 
Ruby objects
Ruby objectsRuby objects
Ruby objects
 
Metaprogramming in Ruby
Metaprogramming in RubyMetaprogramming in Ruby
Metaprogramming in Ruby
 
Metaprogramming
MetaprogrammingMetaprogramming
Metaprogramming
 
Ruby object model at the Ruby drink-up of Sophia, January 2013
Ruby object model at the Ruby drink-up of Sophia, January 2013Ruby object model at the Ruby drink-up of Sophia, January 2013
Ruby object model at the Ruby drink-up of Sophia, January 2013
 
Magic in ruby
Magic in rubyMagic in ruby
Magic in ruby
 
Metaprogramming Primer (Part 1)
Metaprogramming Primer (Part 1)Metaprogramming Primer (Part 1)
Metaprogramming Primer (Part 1)
 
Rails for zombies_2_cheatsheets
Rails for zombies_2_cheatsheetsRails for zombies_2_cheatsheets
Rails for zombies_2_cheatsheets
 
Ruby introduction
Ruby introductionRuby introduction
Ruby introduction
 
Python advance
Python advancePython advance
Python advance
 
Introduction to Ruby MagLev
Introduction to Ruby MagLevIntroduction to Ruby MagLev
Introduction to Ruby MagLev
 
Why I choosed Ruby
Why I choosed RubyWhy I choosed Ruby
Why I choosed Ruby
 
Forbidden Fruit: A Taste of Ruby's ParseTree
Forbidden Fruit: A Taste of Ruby's ParseTreeForbidden Fruit: A Taste of Ruby's ParseTree
Forbidden Fruit: A Taste of Ruby's ParseTree
 
Ruby: Beyond the Basics
Ruby: Beyond the BasicsRuby: Beyond the Basics
Ruby: Beyond the Basics
 
Ruby object model - Understanding of object play role for ruby
Ruby object model - Understanding of object play role for rubyRuby object model - Understanding of object play role for ruby
Ruby object model - Understanding of object play role for ruby
 
Metaprogramming code-that-writes-code
Metaprogramming code-that-writes-codeMetaprogramming code-that-writes-code
Metaprogramming code-that-writes-code
 
Ruby for C# Developers
Ruby for C# DevelopersRuby for C# Developers
Ruby for C# Developers
 

Más de Kevin Munc

Ci for i-os-codemash-01.2013
Ci for i-os-codemash-01.2013Ci for i-os-codemash-01.2013
Ci for i-os-codemash-01.2013Kevin Munc
 
NaN, Zero, & Infinities
NaN, Zero, & InfinitiesNaN, Zero, & Infinities
NaN, Zero, & InfinitiesKevin Munc
 
Basic Scheduling with setTimeout & setInterval
Basic Scheduling with setTimeout & setIntervalBasic Scheduling with setTimeout & setInterval
Basic Scheduling with setTimeout & setIntervalKevin Munc
 
Apples & Oranges? Adventures in Equality Comparison & one of the ‘Bad Parts’
Apples & Oranges? Adventures in Equality Comparison & one of the ‘Bad Parts’Apples & Oranges? Adventures in Equality Comparison & one of the ‘Bad Parts’
Apples & Oranges? Adventures in Equality Comparison & one of the ‘Bad Parts’Kevin Munc
 
Number Conversions (MOTM 2010.12)
Number Conversions (MOTM 2010.12)Number Conversions (MOTM 2010.12)
Number Conversions (MOTM 2010.12)Kevin Munc
 
Percent Literals (MOTM 2010.09)
Percent Literals (MOTM 2010.09)Percent Literals (MOTM 2010.09)
Percent Literals (MOTM 2010.09)Kevin Munc
 
cycle (MOTM 2010.07)
cycle (MOTM 2010.07)cycle (MOTM 2010.07)
cycle (MOTM 2010.07)Kevin Munc
 
empty?, nil?, blank?, & present? (MOTM 2010.05)
empty?, nil?, blank?, & present? (MOTM 2010.05)empty?, nil?, blank?, & present? (MOTM 2010.05)
empty?, nil?, blank?, & present? (MOTM 2010.05)Kevin Munc
 
Take & Drop (MOTM 2010.04)
Take & Drop (MOTM 2010.04)Take & Drop (MOTM 2010.04)
Take & Drop (MOTM 2010.04)Kevin Munc
 
Helpers (MOTM 2010.03)
Helpers (MOTM 2010.03)Helpers (MOTM 2010.03)
Helpers (MOTM 2010.03)Kevin Munc
 
Grouping (MOTM 2010.02)
Grouping (MOTM 2010.02)Grouping (MOTM 2010.02)
Grouping (MOTM 2010.02)Kevin Munc
 
gsub (MOTM 2009.09)
gsub (MOTM 2009.09)gsub (MOTM 2009.09)
gsub (MOTM 2009.09)Kevin Munc
 
The Methods Method and Its Friends (MOTM 2009.08)
The Methods Method and Its Friends (MOTM 2009.08)The Methods Method and Its Friends (MOTM 2009.08)
The Methods Method and Its Friends (MOTM 2009.08)Kevin Munc
 

Más de Kevin Munc (14)

Ci for i-os-codemash-01.2013
Ci for i-os-codemash-01.2013Ci for i-os-codemash-01.2013
Ci for i-os-codemash-01.2013
 
NaN, Zero, & Infinities
NaN, Zero, & InfinitiesNaN, Zero, & Infinities
NaN, Zero, & Infinities
 
Basic Scheduling with setTimeout & setInterval
Basic Scheduling with setTimeout & setIntervalBasic Scheduling with setTimeout & setInterval
Basic Scheduling with setTimeout & setInterval
 
Shellwords
ShellwordsShellwords
Shellwords
 
Apples & Oranges? Adventures in Equality Comparison & one of the ‘Bad Parts’
Apples & Oranges? Adventures in Equality Comparison & one of the ‘Bad Parts’Apples & Oranges? Adventures in Equality Comparison & one of the ‘Bad Parts’
Apples & Oranges? Adventures in Equality Comparison & one of the ‘Bad Parts’
 
Number Conversions (MOTM 2010.12)
Number Conversions (MOTM 2010.12)Number Conversions (MOTM 2010.12)
Number Conversions (MOTM 2010.12)
 
Percent Literals (MOTM 2010.09)
Percent Literals (MOTM 2010.09)Percent Literals (MOTM 2010.09)
Percent Literals (MOTM 2010.09)
 
cycle (MOTM 2010.07)
cycle (MOTM 2010.07)cycle (MOTM 2010.07)
cycle (MOTM 2010.07)
 
empty?, nil?, blank?, & present? (MOTM 2010.05)
empty?, nil?, blank?, & present? (MOTM 2010.05)empty?, nil?, blank?, & present? (MOTM 2010.05)
empty?, nil?, blank?, & present? (MOTM 2010.05)
 
Take & Drop (MOTM 2010.04)
Take & Drop (MOTM 2010.04)Take & Drop (MOTM 2010.04)
Take & Drop (MOTM 2010.04)
 
Helpers (MOTM 2010.03)
Helpers (MOTM 2010.03)Helpers (MOTM 2010.03)
Helpers (MOTM 2010.03)
 
Grouping (MOTM 2010.02)
Grouping (MOTM 2010.02)Grouping (MOTM 2010.02)
Grouping (MOTM 2010.02)
 
gsub (MOTM 2009.09)
gsub (MOTM 2009.09)gsub (MOTM 2009.09)
gsub (MOTM 2009.09)
 
The Methods Method and Its Friends (MOTM 2009.08)
The Methods Method and Its Friends (MOTM 2009.08)The Methods Method and Its Friends (MOTM 2009.08)
The Methods Method and Its Friends (MOTM 2009.08)
 

Último

08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
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...Martijn de Jong
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
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
 
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
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
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 MountPuma Security, LLC
 
🐬 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
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
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
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
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
 
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 Servicegiselly40
 
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 textsMaria Levchenko
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
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
 

Último (20)

08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
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...
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
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...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
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
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
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
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
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
 
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
 
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
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
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...
 

Removing Methods (MOTM 2010.01)

  • 1. Removing Methods Ruby Method of the Month Kevin Munc => @muncman
  • 2. Adding a Method class String def ruby_rocks? true #dat end end
  • 4. Remove It! class String remove_method :ruby_rocks? end
  • 5. Remove It! class String remove_method :ruby_rocks? end "rails".ruby_rocks? NoMethodError: undefined method `ruby_rocks?' for "rails":String
  • 6. A Redefined Method class Crb def to_s "Thanks Randall!" end end
  • 7. Redefinition in Action columbusrb = Crb.new
  • 8. Redefinition in Action columbusrb = Crb.new columbusrb.to_s
  • 9. Redefinition in Action columbusrb = Crb.new columbusrb.to_s => "Thanks Randall!"
  • 10. Redefinition Removal class Crb remove_method :to_s end Note that we didn’t have to get a new instance for the removal to take effect.
  • 11. Redefinition Removal class Crb remove_method :to_s end columbusrb.to_s Note that we didn’t have to get a new instance for the removal to take effect.
  • 12. Redefinition Removal class Crb remove_method :to_s end columbusrb.to_s Note that we didn’t have to get a new instance for the removal to take effect. => "#<Crb:0x101651538>"
  • 13. Inherited Method ror = “Ruby on Rails” ror.to_s => "Ruby on Rails" We get the to_s from Object. Note that the removal takes effect for instances already declared.
  • 14. Inherited Method ror = “Ruby on Rails” ror.to_s => "Ruby on Rails" class String remove_method :to_s We get the to_s from Object. end Note that the removal takes effect for instances already declared.
  • 15. Inherited Method ror = “Ruby on Rails” ror.to_s => "Ruby on Rails" class String remove_method :to_s We get the to_s from Object. end Note that the removal takes effect for instances already declared. ror.to_s => "#<String:0x101682b88>"
  • 16. Removal Location class Object remove_method :to_s end Defined in Kernel. Object mixes in Kernel.
  • 17. Removal Location class Object remove_method :to_s end NameError: method `to_s' not defined in Object Defined in Kernel. Object mixes in Kernel.
  • 18. Put It All Back Starting from a clean slate, so String still has to_s, etc. class Crb def to_s "Thanks Randall!" end end
  • 19. Undefine the Redefine Different than removal! class Crb undef_method :to_s end Interesting that the default output of to_s is present in the output.
  • 20. Undefine the Redefine Different than removal! class Crb undef_method :to_s end Interesting that the default output of to_s is present in the output. columbusrb.to_s
  • 21. Undefine the Redefine Different than removal! class Crb undef_method :to_s end Interesting that the default output of to_s is present in the output. columbusrb.to_s NoMethodError: undefined method `to_s' for #<Crb:0x101651538>
  • 22. Definitely Undefined class String undef_method :to_s end Note that the undef also applied to subclasses of the type where the undef happened.
  • 23. Definitely Undefined class String undef_method :to_s end Note that the undef also applied to subclasses of the type where the undef happened. "rails".to_s NoMethodError: undefined method `to_s' for "rails":String
  • 24. Defined Elsewhere class Crb undef_method :object_id end The undef succeeds because the method is available to class Crb.
  • 25. Defined Elsewhere class Crb undef_method :object_id end columbusrb = Crb.new The undef succeeds because the method is columbusrb.object_id available to class Crb. NoMethodError: undefined method `object_id' for #<Crb:0x10166d7d8>
  • 26. Method Removal remove_method :symbol Removed from the specific type only. Ruby still searches up the hierarchy. undef_method :symbol Prevents calls to the method regardless of the type’s hierarchy. Both defined in the Module class.
  • 27. Finer Points (pun intended) You can call remove_method only on a method actually defined within the class itself. You can call undef_method on a method accessible from the class (inherited or mixed in).
  • 28. Questions? Photo Credits: http://www.flickr.com/photos/tomscott/4225623/ http://www.flickr.com/photos/slayer23/2215384832/ http://www.flickr.com/photos/andreweason/268627274/ http://www.flickr.com/photos/tomscott/28188883/ http://www.flickr.com/photos/jonmikel/259535350/

Notas del editor