Refactoring: The Ruby        Way      Shouichi KAMIYA
Goal•   Writing beautiful code in the ruby way
Beautiful• Readable: easy to understand• Efficient: easy to write• Flexible: easy to extend
The Ruby Way• Modular• Method chaining• English like
class Server < ActiveRecord::Base  def reload_nginx(flag = :reload)   if flag == :reload     ssh("user", address, "sudo /etc...
Problems• Duplications• if ... else ...• The method named “reload” does “restart”
class Server < ActiveRecord::Base  def reload_nginx   exec("/etc/init.d/nginx”, “restart")  end def restart_nginx  exec(“/...
Still don’t feel right• If it is a C, it might OK but, doesn’t look  very nice when it comes to ruby.
What we wantserver = Server.find(id)server.nginx.restartserver.nginx.reload
Modularmodule Daemons class Nginx end class Syslog endend
How to write• User first: think of the users who use the  API you designed.
Ruby Idiomsmodule ActiveRecord                      class User < AR::Base class Base                       has_many :artic...
class Base  class << self    def has_path(path)     self.path = path    end  endendclass Nginx < Base  has_path “/etc/init...
class Base  def exec(*args)   ssh(“root”, address, “#{self.path} #{args}”)  end def reload  exec(:reload) end def restart ...
Finallyclass Server def nginx  @nginx ||= Daemons::Nginx.new(self) endendServer.find(id).nginx.restart
Thanks• All source codes are available at https://  gist.github.com/749800
Upcoming SlideShare
Loading in …5
×

Refactoring.the.ruby.way

1,142 views

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,142
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
14
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Refactoring.the.ruby.way

    1. 1. Refactoring: The Ruby Way Shouichi KAMIYA
    2. 2. Goal• Writing beautiful code in the ruby way
    3. 3. Beautiful• Readable: easy to understand• Efficient: easy to write• Flexible: easy to extend
    4. 4. The Ruby Way• Modular• Method chaining• English like
    5. 5. class Server < ActiveRecord::Base def reload_nginx(flag = :reload) if flag == :reload ssh("user", address, "sudo /etc/init.d/nginx reload") elsif flag == :restart ssh("user", address, "sudo /etc/init.d/nginx restart") end end def reload_syslog(flag = :reload) # almost same as above endend
    6. 6. Problems• Duplications• if ... else ...• The method named “reload” does “restart”
    7. 7. class Server < ActiveRecord::Base def reload_nginx exec("/etc/init.d/nginx”, “restart") end def restart_nginx exec(“/etc/init.d/nginx”, “restart”) end def exec(path, arg) ssh("user", address, "sudo #{path} #{arg}") endend
    8. 8. Still don’t feel right• If it is a C, it might OK but, doesn’t look very nice when it comes to ruby.
    9. 9. What we wantserver = Server.find(id)server.nginx.restartserver.nginx.reload
    10. 10. Modularmodule Daemons class Nginx end class Syslog endend
    11. 11. How to write• User first: think of the users who use the API you designed.
    12. 12. Ruby Idiomsmodule ActiveRecord class User < AR::Base class Base has_many :articles end endend
    13. 13. class Base class << self def has_path(path) self.path = path end endendclass Nginx < Base has_path “/etc/init.d/nginx”endclass Syslog < Base has_path “/etc/init.d/syslog-ng”end
    14. 14. class Base def exec(*args) ssh(“root”, address, “#{self.path} #{args}”) end def reload exec(:reload) end def restart exec(:restart) endend
    15. 15. Finallyclass Server def nginx @nginx ||= Daemons::Nginx.new(self) endendServer.find(id).nginx.restart
    16. 16. Thanks• All source codes are available at https:// gist.github.com/749800

    ×