程序员…
2 Jun
days_in_month这个函数返回给定的年月的月的天数,对于写阴历什么的很有帮助,下面是英文的介绍。Descriptiondays_in_month(month, year=nil)Return the number of days in the given month. If a year is given, February will return the correct number of days for leap years. Otherwise, this method will always report February as having 28 days.<code> 1. >> Time.days_in_month(4) => 30 2. >> Time.days_in_month(4,2006) => 30 3. >> Time.days_in_month(2,2006) => 28 4. >> Time.days_in_month(2) => 28 5. >> Time.days_in_month(2,2008) => 29</code>
2 Jun
在使用SSH的时候,需要另外安装SSH的库,但是我按照官方上的方法无论是远程安装还是本地安装都出现错误,如下:D:\>gem install net-ssh –include-dependenciesERROR: While executing gem … (Zlib::BufError) buffer error查了下资料,貌似问题是出在Windows版本上的RUBY有点问题,可以按照下面的进行修复:1.修改ruby的安装目录下(c:\ruby\lib\ruby\site_ruby\1.8\rubygems\package.rb)的package.rb文件,在 618 行,把1.2.1修改为 1.2.3。2) removed the linessh 22/tcp #SSHfrom file %systemroot%\system32\drivers\etc\services
2 Jun
有时候你可能想备份下原来的博客,或者对写的文章做些备份之类的,用RUBY很间就可以实现,下面是我的这个应用中所用的,针对用户推荐的文章,给出文章的RSS地址,然后写些自己的感受或者评论,就可以把提交的那个RSS文章取过来的。下面是代码:<code> @topic.body = ‘引言:’ + @topic.body + ‘<br/>’ feed = RSS::Parser.parse(open(params[:uri]).read, false) @topic.body += ‘<br />*来源:*’ + feed.channel.title @topic.body += ‘<br />*地址:*’ + feed.channel.link feed.items.each do |item| @topic.body += ‘<br />*标题:*’ + item.title @topic.body += ‘<br />*内容摘要:*<br />’ + item.description end </code>对了,你还需要在前面加上对库的引用,如下:require ‘rss/2.0′require ‘open-uri’另外,刚刚看到Javaeye上一个老兄写了一个差不多的,我就不多写了,大家可以参考下,地址如下:http://www.javaeye.com/blog/57538
31 May
老外(Matthew Bass)写了一篇文章*Automating File Uploads with SSH and Ruby.*放在InfoQ上,其说明了如何使用Net-SSH 和 Net-SFTP 库实现了使用SSH备份文件到远程主机上的Ruby实现,如果你有类似方面的需求,可以好好看看这篇文章,地址在:http://www.infoq.com/articles/ruby-file-upload-ssh-intro
31 May
ActiveMessaging是个感觉很不错的一个插件,通过它你可以发送、接收MO,总得来说很有诱惑力,还没有看完,下面这篇文章对这个东西做了详细的介绍,感兴趣的可以自己去看看,我就不翻译了,等我自己试用后,可以再些些心得什么的。*introduction*Rails has already planted its flag firmly in the ground of database driven web applications. *ActiveMessaging*is a Rails plugin framework that extends the borders of Rails territory by adding simplified messaging integration. With ActiveMessaging and Rails now you can loosely integrate with systems as disparate as mainframes sending MQ messages or J2EE webapps, offload processing long-running tasks, or create applications with event or message-driven architectures. As others have put it, ActiveMessaging is trying to do for messaging what ActiveRecord does for databases. This article will introduce you to ActiveMessaging and related technologies, and get you started using it in your Rails applications now.http://www.infoq.com/articles/intro-active-messaging-rails
30 May
很多时候,我们需要处理用户输入,为了适合WEB2的特性,做为开发者我们也会设计很对个途径让用户参与进去,但是“盖茨”大叔说得很对:*凡是用户的输入都是有害的!*一点不假,有时候用户可能是无意的或者一些不怀好意的用户会输入一些我们并不想让他们输入的东西,以防止破坏页面结构或者造成其他的危害(比如SQL注射和XSS攻击),所以我们需要对用户的输入认真的检验和过滤。在页面上显示的时候,我们可以选择+h()+或者+sanitize+或者+strip_tags+等等rails提供的方法进行过滤,但是这些危险的字符还适度会存在数据库,或许有一天就会引爆了呢,所以呢,最好的办法是在存入数据库的时候就做一些过滤或转换,一下一点点代码段只是一个小例子,真实世界中需要过滤和清除的危险字符是很多的,大家注意主机收集和完善,也希望大家一起来实现这个功能,做一个通用的 helper方法。<code> def stripped_html(html) # this is a copynpaste of the routine in article.rb # so the one in article.rb can change w/o breaking this. self.html.gsub(/<[^>]*>/,”).to_url end</code>对于HTML标签的过滤和清除,你有什么好的方法么?欢迎分享!
28 May
*导读:*infoq上的这篇REST构架分析的文章还是不错,从HTTP协议开始介绍了REST的构架理念,值得看看,再花点时间好好理解下REST。一种思维方式影响了软件行业的发展。REST软件架构是当今世界上最成功的互联网的超媒体分布式系统。它让人们真正理解我们的网络协议HTTP本来面貌。它正在成为网络服务的主流技术,同时也正在改变互联网的网络软件开发的全新思维方式。AJAX技术和Rails框架把REST软件架构思想真正地在实际中很好表现出来。今天微软也已经应用REST并且提出把我们现有的网络变成为一个语义网,这种网络将会使得搜索更加智能化。详细内容,请查看:http://www.infoq.com/cn/articles/rest-architecure
27 May
给定一个时间点,希望得到其他时间点的解决方案如下,Ruby 的所有时间对象都可像数字一样用在值域中。Date和DateTime对象按天递增,而Time对象按秒递增:*例子*require ‘date’(Date.new(1976, 7, 2)..Date.new(1976, 7, 4)).each { |x| puts x }1976-07-021976-07-031976-07-04span = DateTime.new(1776, 7, 2, 1, 30, 15)..DateTime.new(1776, 7, 4, 7, 0,0)span.each { |x| puts x } 1776-07-02T01:30:15Z 1776-07-03T01:30:15Z 1776-07-04T01:30:15Z(Time.at(100)..Time.at(102)).each { |x| puts x } Wed Dec 31 19:01:40 EST 1969 Wed Dec 31 19:01:41 EST 1969 Wed Dec 31 19:01:42 EST 1969Ruby 的Date类定义了*step*和*upto*两种方便的由数字使用的迭代器:the_first = Date.new(2004, 1, 1)the_fifth = Date.new(2004, 1, 5)the_first.upto(the_fifth) { |x| puts x } 2004-01-01 2004-01-02 2004-01-03 2004-01-04 2004-01-05*讨论*Ruby的日期对象在内部被存储为数字,并且一定范围的这种对象可被视为一定范围的数字。对于Date和DateTime对象而言,内部表示是儒略历日期:每次在一定范围的这种对象上累加一天。对于Time对象而言,内部表示是自Unix 纪元以来的秒数:每次在一定范围的Time对象上累加一秒。*Time*没有定义step和upto方法,但添加它们也很简单:class Timebq. def step(other_time, increment) raise ArgumentError, "step can’t be 0" if increment == 0 increasing = self < other_time if (increasing && increment < 0) || (!increasing && increment > 0) yield self return end d = self begin yield d d += increment end while (increasing ? d <= other_time : d >= other_time) enddef upto(other_time) step(other_time, 1) { |x| yield x } endendthe_first = Time.local(2004, 1, 1)the_second = Time.local(2004, 1, 2)the_first.step(the_second, 60 * 60 * 6) { |x| puts x } Thu Jan 01 00:00:00 EST 2004 Thu Jan 01 06:00:00 EST 2004 Thu Jan 01 12:00:00 EST 2004 Thu Jan 01 18:00:00 EST 2004 Fri Jan 02 00:00:00 EST 2004the_first.upto(the_first) { |x| puts x } Thu Jan 01 00:00:00 EST 2004
25 May
*Textile*是一种语言标记格式,目前在ROR领域用的相当多,本着简洁好用的原则,这套规则就像PHP领域内的BBCODE一样流行,但是一直一来,都是靠手工去写这些标识符号,很不方便,也曾萌发了写一套可视化的东西来,可惜一直没有时间和精力,今天看到slate blog的blog上有关于这个的东西,感觉就是自己想实现的那个,看了下视频(http://www.youtube.com/watch?v=wQYedmbsJf4)和DEMO(http://slateinfo.blogs.wvu.edu/plugins/textile_editor_helper/demo),果然是的,试用了下,很方便,效果很好,再各种浏览器下都表现不错,推荐给大家。可以先看下它的功能:安装也挺方便的:+script/plugin install http://svn.webtest.wvu.edu/repos/rails/plugins/textile_editor_helper/+使用:bq. # 1. run rake textile_editor_helper:install 2. for the textarea(s) that you want to add the TEH toolbar to replace the text area tag(s) with: <%= textile_editor ‘object’, ‘field’ -%> just like writing a text area tag, same options 3. at the end of your form put in the following code: <%= textile_editor_initialize -%> Important! 4. save your view and check it out*注意*:To use Textile, you must install the RedCloth gem first.
24 May
需要安装这个的很简单,下面就是我在自己机子上安装的时候的过程演示,比起Apache等的配置,这个东西好多了。D:\>gem install mongrelSelect which gem to install for your platform (i386-mswin32) 1. mongrel 1.0.1 (mswin32) 2. mongrel 1.0.1 (ruby) 3. mongrel 1.0 (mswin32) 4. mongrel 1.0 (ruby) 5. Skip this gem 6. Cancel installation> 1Install required dependency gem_plugin? [Yn] yInstall required dependency cgi_multipart_eof_fix? [Yn] ySuccessfully installed mongrel-1.0.1-mswin32Successfully installed gem_plugin-0.2.2Successfully installed cgi_multipart_eof_fix-2.1Installing ri documentation for mongrel-1.0.1-mswin32…Installing ri documentation for gem_plugin-0.2.2…Installing ri documentation for cgi_multipart_eof_fix-2.1…Installing RDoc documentation for mongrel-1.0.1-mswin32…Installing RDoc documentation for gem_plugin-0.2.2…Installing RDoc documentation for cgi_multipart_eof_fix-2.1…D:\>
Comments::最新评论