removing methods (motm 2010.01)

28
Removing Methods Ruby Method of the Month Kevin Munc => @muncman

Upload: kevin-munc

Post on 29-Jan-2018

890 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Removing Methods (MOTM 2010.01)

Removing MethodsRuby Method of the MonthKevin Munc => @muncman

Page 2: Removing Methods (MOTM 2010.01)

Adding a Method

class String def ruby_rocks? true #dat endend

Page 3: Removing Methods (MOTM 2010.01)

Fancy New Method

"rails".ruby_rocks?=> true

Page 4: Removing Methods (MOTM 2010.01)

Remove It!class String remove_method :ruby_rocks?end

Page 5: Removing Methods (MOTM 2010.01)

Remove It!class String remove_method :ruby_rocks?end"rails".ruby_rocks?NoMethodError: undefined method `ruby_rocks?' for "rails":String

Page 6: Removing Methods (MOTM 2010.01)

A Redefined Method

class Crb def to_s "Thanks Randall!" endend

Page 7: Removing Methods (MOTM 2010.01)

Redefinition in Action

columbusrb = Crb.new

Page 8: Removing Methods (MOTM 2010.01)

Redefinition in Action

columbusrb = Crb.new

columbusrb.to_s

Page 9: Removing Methods (MOTM 2010.01)

Redefinition in Action

columbusrb = Crb.new

columbusrb.to_s=> "Thanks Randall!"

Page 10: Removing Methods (MOTM 2010.01)

Redefinition Removalclass Crb remove_method :to_send

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

Page 11: Removing Methods (MOTM 2010.01)

Redefinition Removalclass Crb remove_method :to_send

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

columbusrb.to_s

Page 12: Removing Methods (MOTM 2010.01)

Redefinition Removalclass Crb remove_method :to_send

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

columbusrb.to_s=> "#<Crb:0x101651538>"

Page 13: Removing Methods (MOTM 2010.01)

Inherited Methodror = “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.

Page 14: Removing Methods (MOTM 2010.01)

Inherited Methodror = “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.

class String remove_method :to_send

Page 15: Removing Methods (MOTM 2010.01)

Inherited Methodror = “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.

class String remove_method :to_sendror.to_s=> "#<String:0x101682b88>"

Page 16: Removing Methods (MOTM 2010.01)

Removal Locationclass Object remove_method :to_send

Defined in Kernel.

Object mixes in Kernel.

Page 17: Removing Methods (MOTM 2010.01)

Removal Locationclass Object remove_method :to_send

Defined in Kernel.

Object mixes in Kernel.

NameError: method `to_s' not defined in Object

Page 18: Removing Methods (MOTM 2010.01)

Put It All Back

class Crb def to_s "Thanks Randall!" endend

Starting from a clean slate, so String still has to_s, etc.

Page 19: Removing Methods (MOTM 2010.01)

Undefine the Redefine Different than removal!

class Crb undef_method :to_send

Interesting that the default output of to_s is present in the output.

Page 20: Removing Methods (MOTM 2010.01)

Undefine the Redefine Different than removal!

class Crb undef_method :to_send

Interesting that the default output of to_s is present in the output.

columbusrb.to_s

Page 21: Removing Methods (MOTM 2010.01)

Undefine the Redefine Different than removal!

class Crb undef_method :to_send

Interesting that the default output of to_s is present in the output.

columbusrb.to_sNoMethodError: undefined method `to_s' for #<Crb:0x101651538>

Page 22: Removing Methods (MOTM 2010.01)

Definitely Undefinedclass String undef_method :to_send

Note that the undef also applied to subclasses of the type where the undef happened.

Page 23: Removing Methods (MOTM 2010.01)

Definitely Undefinedclass String undef_method :to_send

"rails".to_sNoMethodError: undefined method `to_s' for "rails":String

Note that the undef also applied to subclasses of the type where the undef happened.

Page 24: Removing Methods (MOTM 2010.01)

Defined Elsewhereclass Crb undef_method :object_idend

The undef succeeds because the method is available to class Crb.

Page 25: Removing Methods (MOTM 2010.01)

Defined Elsewhereclass Crb undef_method :object_idend

columbusrb = Crb.newcolumbusrb.object_idNoMethodError: undefined method `object_id' for #<Crb:0x10166d7d8>

The undef succeeds because the method is available to class Crb.

Page 26: Removing Methods (MOTM 2010.01)

Method Removalremove_method :symbol

Removed from the specific type only.

Ruby still searches up the hierarchy.

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

Both defined in the Module class.

Page 27: Removing Methods (MOTM 2010.01)

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).