O slideshow foi denunciado.
Utilizamos seu perfil e dados de atividades no LinkedIn para personalizar e exibir anúncios mais relevantes. Altere suas preferências de anúncios quando desejar.
Ruby: Beyond the Basics          by Michael Koby      http://www.codecasts.tv
Hi, my name is... Michael Koby
@mkoby
http://www.codecasts.tv
http://www.mkoby.com
What
What•   Open Classes
What•   Open Classes•   Dynamic Methods
Metaprogramming Ruby       by Paolo PerrottaO’Reilly: http://obyk.us/39267Amazon: http://obyk.us/1efe7
Open Classes
What is an “Open Class?”
You’ve Heard   of this byAnother Name
Monkeypatching
class String  #shortened for brevity  def sub(a, b)    #replaces string ‘a’ with string ‘b’  end  def gsub(a, b)    #simil...
class String  def to_alphanumeric    gsub /[^ws]/, ‘’  endend
1.9.3 :005 > “my string”.methods.grep /alphanumeric/
=> [:to_alphanumeric]
class Employee  def salary    “Some Number”  endend
class Employee  def salary    “Some Number”  endendclass Employee  def name    “Some Name”  endend
e = Employee.new
e = Employee.newe.salary
e = Employee.newe.salary   # => “Some Number”
e = Employee.newe.salary   # => “Some Number”e.name
e = Employee.newe.salary   # => “Some Number”e.name     # => “Some Name”
class Employee  def salary    “Some Number”  endendclass Employee  def name    “Some Name”  endend
Some Real Life Samples
Dynamic Methods
How You Usually Call Methods
someObject.methodName
Dynamically Calling Methods
x = 2
x = 2x.send(:+, 2)
x = 2x.send(:+, 2)   => 4
class APIRequestWrapper  attr_accessor :client  def initialize    @client = Savon::Client.new do |wsdl|      #wsdl.options...
Some Real Life Samples
In Closing...
Open Classes
class Employee  def salary    “Some Number”  endendclass Employee  def name    “Some Name”  endend
Dynamic Methods
x = 2x.send(:+, 2)   => 4
Metaprogramming Ruby       by Paolo PerrottaO’Reilly: http://obyk.us/39267Amazon: http://obyk.us/1efe7
Thank You!
Ruby: Beyond the Basics
Ruby: Beyond the Basics
Ruby: Beyond the Basics
Ruby: Beyond the Basics
Ruby: Beyond the Basics
Ruby: Beyond the Basics
Próximos SlideShares
Carregando em…5
×

Ruby: Beyond the Basics

643 visualizações

Publicada em

You've learned to program in Ruby, but now you want to start learning about how to move beyond the language basics and make use of some of the dynamic parts of the Ruby language. We'll cover that a bit here.

Publicada em: Tecnologia, Educação
  • Seja o primeiro a comentar

  • Seja a primeira pessoa a gostar disto

Ruby: Beyond the Basics

  1. 1. Ruby: Beyond the Basics by Michael Koby http://www.codecasts.tv
  2. 2. Hi, my name is... Michael Koby
  3. 3. @mkoby
  4. 4. http://www.codecasts.tv
  5. 5. http://www.mkoby.com
  6. 6. What
  7. 7. What• Open Classes
  8. 8. What• Open Classes• Dynamic Methods
  9. 9. Metaprogramming Ruby by Paolo PerrottaO’Reilly: http://obyk.us/39267Amazon: http://obyk.us/1efe7
  10. 10. Open Classes
  11. 11. What is an “Open Class?”
  12. 12. You’ve Heard of this byAnother Name
  13. 13. Monkeypatching
  14. 14. class String #shortened for brevity def sub(a, b) #replaces string ‘a’ with string ‘b’ end def gsub(a, b) #similar to sub but with the /g from RegEx end #shortened for brevityend
  15. 15. class String def to_alphanumeric gsub /[^ws]/, ‘’ endend
  16. 16. 1.9.3 :005 > “my string”.methods.grep /alphanumeric/
  17. 17. => [:to_alphanumeric]
  18. 18. class Employee def salary “Some Number” endend
  19. 19. class Employee def salary “Some Number” endendclass Employee def name “Some Name” endend
  20. 20. e = Employee.new
  21. 21. e = Employee.newe.salary
  22. 22. e = Employee.newe.salary # => “Some Number”
  23. 23. e = Employee.newe.salary # => “Some Number”e.name
  24. 24. e = Employee.newe.salary # => “Some Number”e.name # => “Some Name”
  25. 25. class Employee def salary “Some Number” endendclass Employee def name “Some Name” endend
  26. 26. Some Real Life Samples
  27. 27. Dynamic Methods
  28. 28. How You Usually Call Methods
  29. 29. someObject.methodName
  30. 30. Dynamically Calling Methods
  31. 31. x = 2
  32. 32. x = 2x.send(:+, 2)
  33. 33. x = 2x.send(:+, 2) => 4
  34. 34. class APIRequestWrapper attr_accessor :client def initialize @client = Savon::Client.new do |wsdl| #wsdl.options go here end end ## Makes API request, request_name is string ## parameters is a Ruby hash def make_api_request(request_name, paramenters) @client.request(request_name.to_sym) do |soap| soap.body = parameters end endend
  35. 35. Some Real Life Samples
  36. 36. In Closing...
  37. 37. Open Classes
  38. 38. class Employee def salary “Some Number” endendclass Employee def name “Some Name” endend
  39. 39. Dynamic Methods
  40. 40. x = 2x.send(:+, 2) => 4
  41. 41. Metaprogramming Ruby by Paolo PerrottaO’Reilly: http://obyk.us/39267Amazon: http://obyk.us/1efe7
  42. 42. Thank You!

×