楼主: jieforest

Ruby开发技巧

[复制链接]
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
31#
 楼主| 发表于 2013-11-10 10:26 | 只看该作者
Another use for this would be adapter classes for two disparate services (say database clients for example), mapping the various errors to the same categories, so your rescues still work after you switch out the client under the adapters.

This technique does however come with a drawback. The #extend call will invalidate Ruby’s global method cache, slowing your app for a moment each time it’s called. We will be getting more fine-grained method cache invalidation in Ruby 2.1, at which point there’s nothing to worry about. But even now this can be a very useful and powerful technique.

Modules

Modules serve a number of purposes in Ruby. Perhaps the most visible is as the name-spacing mechanism, and then there is their main intended use as ‘mixins’. Modules like Enumerable and Comparable are fantastic, easily allowing you to add complex behaviour to your classes by defining just a few methods and including the modules. Modules have some other uses too.

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
32#
 楼主| 发表于 2013-11-10 10:26 | 只看该作者
Sometimes you have a bunch of related methods that are much closer to functions, they take and input, return an output, and don’t do anything with self. Modules act as great way to group these methods together.

Here’s one from one of our apps (there are a couple more methods, but they’re a bit too specific to share).
  1. module Geo
  2.   module_function

  3.   RADIUS_OF_THE_EARTH = 6371

  4.   def distance((origin_lat, origin_long), (dest_lat, dest_long))
  5.     return unless origin_lat && origin_long && dest_lat && dest_long

  6.     sin_lats = Math.sin(rad(origin_lat)) * Math.sin(rad(dest_lat))
  7.     cos_lats = Math.cos(rad(origin_lat)) * Math.cos(rad(dest_lat))
  8.     cos_longs = Math.cos(rad(dest_long) - rad(origin_long))

  9.     x = sin_lats + (cos_lats * cos_longs)
  10.     x = [x, 1.0].min
  11.     x = [x, -1.0].max

  12.     Math.acos(x) * RADIUS_OF_THE_EARTH
  13.   end

  14.   def rad(degree)
  15.     degree.to_f / (180 / Math::PI)
  16.   end

  17.   def degree(rad)
  18.     rad.to_f * (180 / Math::PI)
  19.   end

  20.   def miles(km)
  21.     km / 1.609344
  22.   end

  23.   def km(miles)
  24.     miles * 1.609344
  25.   end
  26. end
复制代码

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
33#
 楼主| 发表于 2013-11-10 10:27 | 只看该作者
The module_function declaration at the start of the module is a method visibility modifier, like public, private, orprotected.

It makes the method available directly on the module like a class method, and as a private instance method so the method is available un-prefixed when the module is included.
  1. Geo.distance([51.47872, -0.610248], [51.5073346 , -0.1276831])   #=> 33.55959095208182

  2. include Geo
  3. distance([51.47872, -0.610248], [51.5073346 , -0.1276831])       #=> 33.55959095208182
  4. A similar effect can be achieved switching module_fuction to extend self.
  5. module Geo
  6.   extend self
  7.   ...
  8. end
复制代码

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
34#
 楼主| 发表于 2013-11-10 10:27 | 只看该作者
Here the instance methods of the module are copied as class methods. The difference is that you can use the other method visibility modifiers, making methods that are private on the module, or public on the instance (where they would have all been public on the module and private on the instance with module_fuction).

Modules also make for handy singleton objects with no need to mess about preventing more than one copy being instantiated or working out how to get ahold of the reference, it’s all built in to Ruby.
  1. require "net/http"
  2. module APIClient
  3.   @http = Net:HTTP.new("example.com", 80)
  4.   @user = "user"
  5.   @pass = "pass"

  6.   def self.get(path)
  7.     request = Net::HTTP::Get.new(path)
  8.     request.basic_auth(@user, @pass)
  9.     @http.request
  10.   end
  11. end

  12. response = APIClient.get("/api/examples")
复制代码

使用道具 举报

回复
论坛徽章:
0
35#
发表于 2013-11-12 10:35 | 只看该作者
都是英文,写的还是抄的。

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表