Search


Meta

Creative


Support


Leesoft

Feed™

Recent Comments

Photos

Oct
27

转载关于Ruby的extend和include

Posted by » developerly
41 hits

Ruby extend and include

from URL: http://www.rsa.idv.tw/?p=151

Ruby modules有兩種方式mixed into a class,讓class可以簡易新增行為

  1. include
  2. 通常用來新增Instance of mehtod

     

     

    module  CMath
    
      def add(n)
    
        self + n
    
      end
    
    end
    
    Fixnum.class_eval do
    
      include CMath
    
    end
    
    puts  3.add 10
  3. extend
  4. 通常用來新增class method

     

     

    module ExtendMe
    
      def v_object_id
    
        “my object id is #{self.object_id}”
    
      end
    
    end
    
    class Person
    
      extend ExtendMe
    
    end
    
    puts Person.v_object_id
  5. extend through include pattern
  6. 提供統一介面處理

     

     

    module ExtendThroughIncludePattern
    
      def self.included(klass)
    
        klass.extend ClassMethods
    
      end
    
      def instance_method
    
        “this is an instance of #{self.class}”
    
      end
    
      module  ClassMethods
    
        def class_method
    
          “this is a method of the #{self} class”
    
        end
    
      end
    
    end
    
    class Person
    
        include ExtendThroughIncludePattern
    
    end
    
    puts Person.new.instance_method
    
    puts Person.class_method

self.included
Callback invoked whenever the receiver is included in another module or class. This should be used in preference to Module.append_features if your code wants to perform some action when a module is included in another.

Oct
21

Racl新鲜出炉:预告

Posted by » developerly
33 hits

今天真的很兴奋,经过这么长时间,终于把Rails程序中权限控制按照我自己的想法实现了。特此留个记号,明天将正式发布我的权限控制插件。欢迎大家交流!

Sep
4

使用Rails分页插件时慎用 include语句

Posted by » admin
60 hits

先说点题外话,最近这两个星期过的还是蛮烦的,主要是工作中的一些状况,让未来变得更加不确定。导致有段时间没有更新这个网站了。其实Leesoft的目标是做成独立的技术站点,所以不应该受到太多站长个人情绪的影响。在此稍微检讨一下。另外,最近业内也算发生了一件大事,就是微软将番茄彻底摘下来了。故以后我坚持合法使用软件的目标要更加坚定了。不过说道合法使用,并不一定要花很多钱。这台dell我已经全部linux化了,用了这么长的时间,其实还是挺好用的。我以后要更加努力的使用开源自由的软件来替代windows了。

说到开发,最近遇到了一个性能上的问题,这是我的代码:

conditions=[”1″]
build_query(conditions,”and username like ?”,@username)
build_query(conditions,”and realname like ?”,@realname)
@users = User.paginate  :page => params[:page],
:per_page => 10,:include=>’resumes’,:order=>’users.updated_at desc’,:conditions=>conditions

使用了rails的分页插件,其中users表中有几万条记录。结果页面一开始执行就死在那里了,非要重启MySQL才能解决。为了找到问题的根源我故意将condit拼错,从而让页面显示出SQL文信息, 发现执行的是如下的样式的SQL文:

select * from users left outer join resumes on resumes.user_id=users.id limit 0,10

在PHPMYSQLADMIN中执行这段SQL文速度也非常慢,然而去掉left outer join的内容

select * from users limit 0,10

事实上resumes表和users表一样,都很大,所以左连接的时候要扫描整个resumes表,导致系统资源耗尽了T&^%^$$#
因此虽然分页插件能够改善查询的效果,但是我们一定要好好了解include参数所连接的表,如果join的表本身也很大的话,服务器压力就会很大。

Aug
21

明天就要见到传说中的高人了

Posted by » admin
82 hits

明天就要见到传说中RoR的高人JP,不知道为什么心情有点激动

Aug
10

Ruby处理文本文档的两个方法

Posted by » admin
60 hits

这是一篇很简单的使用指南。

处理YAML文件:
YAML::load(FIle.open(filepath))

处理CSV文件:
CSV::open(filepath,’r') do |row|
puts row
end

Aug
9

转载关于Ruby中的随机数(没有翻译)

Posted by » admin
86 hits

Problem

You want to generate pseudorandom numbers, select items from a data structure at random, or repeatedly generate the same “random” numbers for testing purposes.

跳转后阅读转载关于Ruby中的随机数(没有翻译)的完整内容

Aug
1

CGI::Session::CookieStore::CookieOverflow

Posted by » developerly
180 hits

这又是一个滥用session导致的后果。

与ASP不一样的在于,Rails把Session对象保留在客户端而不是服务器端。所以我如下的代码中:

session[:process_resume]=Resume.find(params[:id])

当Resume.find的结果是一个很大的对象的时候,就会出现如上很诡异的错误。同样的道理,session中不能保存不能被dump的ruby对象。当然有时候为了减少数据库的查询,我们希望能够把大对象保存在session里面,解决方法也很多,可以简历一个内存中的table,以session_id作为key值,来保存任意ruby对象。不过代价就是对服务器的内存要求高了。甚至于可以使用memcache来做这样的工作。

PS,rails的session机制也说明了不能在session里面保留任何敏感的数据信息,因为理论上说客户端的任何数据都是可以读取的,而且cookie是不加密的。

Jul
10

RoR中的中文截取和去掉HTML标签

Posted by » developerly
105 hits

最近在做一个网站内容管理系统,需要截取部分内容做摘要。其实就两个目标,去掉HTML的标签并截取前10个字符串的内容。

去掉HTML标签:

Ruby代码, 代码高亮@代码发芽网

1 def plain_text(text,replacement=” “)
2 text.gsub(/<[^>]*>/){|html| replacement}
3 end

截取UTF-8编码的中文字符(来自于http://wtb.javaeye.com/)

Ruby代码, 代码高亮@代码发芽网

01 def trc_utf8(text, length = 10, t_string = “…”)
02 l=0
03 char_array=text.unpack(“U*”)
04 char_array.each_with_index do |c,i|
05 l = l+ (c<127 ? 0.5 : 1)
06 if l>=length
07 return char_array[0..i].pack(“U*”)+(i<char_array.length-1 ? t_string : “”)
08 end
09 end
10 return text
11 end

Jul
2

TekGeek:Another way of Mixin

Posted by » developerly
88 hits

mixin是ruby里面非常超强的一个方法。虽然在设计哲学上,mixin破坏了类继承的层次,不过却为编程带来了很多便利和新的思路。典型的mixin是对类进行重新定义

1 class Foo
2     ...
3 end
4 class Foo
5     include YourModule
6 end
7 class Foo
8     extend YourModule #为类添加静态方法
9 end

这里介绍另外一种Mixin的方法,适合在程序中批量动态使用,尤其适合用来编写RoR的插件(国内很少有文章介绍怎么编写RoR插件)。关于RoR的插件编写,我将在后续的文章中作一个介绍

Ruby代码, 代码高亮@代码发芽网

1 Foo.send(:include,YourModule)
2 Foo.class.send(:include,YourModule)
3 Foo.send(:extend,YourModule)

其中第2行和第3行的执行是等价的

Jul
1

新的不一定是最好的

Posted by » developerly
75 hits

一直都没有下载firefox3.0,主要是担心这个新的firefox下面,一票插件都不能使用了。最近在RedHat上面搭建RoR的生产环境,再次印证了,新的不一定是最好的。

这次搭建,主要使用 mod_rails来搭建,本来一切都还好,到最后死活不运行。只好停掉apache,用最原始的ruby script/server来看看。结果居然报出及其诡异的提示

*** glibc detected *** double free or corruption

在网上查了一圈,好像是因为最新的ruby(1.8.6-p230)的一个bug(囧http://www.ruby-forum.com/topic/157248)。重新从源代码安装了ruby1.8.6-p114解决了这个问题。script/server可以运行了,重启启动apache也居然能够正确运行了。