Archive for ‘Ruby’ Category

26
Dec

hack truncate in ruby 1.8.7 compability issue

add follows code into rails app environment.rb and restart the service
# http://rails.lighthouseapp.com/projects/8994/tickets/867-undefined-method-length-for-enumerable

class String
def chars
ActiveSupport::Multibyte::Chars.new(self)
end
alias_method :mb_chars, :chars
end
##
module ActionView
module Helpers
module TextHelper
def truncate(text, length = 30, truncate_string = “…”)
[...]

25
Dec

process base64 encode in ruby

在Ruby世界中,语言本身就支持标准的Base64编码实现,而且还有不止一种实现,一个是类库base64中提供的实现(使用时需要require),一个是Ruby基础数据类型Array提供的,可直接使用.具体使用方法如下:
使用Array的实现
str = “imabase64encode”
base64_str = [str].pack(”m”)
original = base64_str.unpack(”m”)
使用Base64的实现
require “base64″
enc = Base64.encode64(’Send reinforcements’)
plain = Base64.decode64(enc)
请注意方法pack(’m’)、unpack(’m’)以及encode64和decode64的用法
建议使用Array方式的实现,在Ruby1.9中将会去除Base64这个类库

15
Dec

使用net/http发送请求

下面是使用Ruby通过Http请求读取del.icio.us的一个示例.
require ‘net/https’
require “rexml/document”
username = “” # your del.icio.us username
password = “” # your del.icio.us password
resp = href = “”;
begin
http = Net::HTTP.new(”api.del.icio.us”, 443)
http.use_ssl = true
http.start do |http|
req = Net::HTTP::Get.new(”/v1/tags/get”, {”User-Agent” =>
“raecoo.com”})
req.basic_auth(username, password)
[...]

09
Nov

Merb 1.0 Released

Merb - a much heralded, highly flexible Ruby-based Web application framework - has reached version 1.0 after two years of development. Congratulations to Merb’s creator, Ezra Zygmuntowicz, and to the large group of associated developers (such as Yehuda Katz and Matt Aimonetti) who’ve kept adding features and pushed Merb forward to be a significant alternative [...]

04
Nov

Use Fastri to increase the speed of Ruby ri

gem install fastri
fastri-server -b
the first line install the gem,second line make ruby doc indexs.
Usage: qri keyword

04
Nov

Thinking Sphinx on Windows

In one of my Ruby on Rails projects, I needed a full-text search engine to integrate. And after some research, I decided that Thinking Sphinx is the way to go. It seemed simple, fast and well documented. Unfortunately, I had some issues when tried to get it running on Windows.
The first step is installing [...]

31
Oct

ruby email validation regex expression

this Regex Expression can handle ‘-’ character in Ruby email validation
^\w+((-\w+)|(\-)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$
Ruby Regex Expression test tool : http://www.rubular.com/
BTW:only test in ruby.
感谢ashchan 提供了可以支持Gmail 标签的正则表达式,经测试效果也不错,如下:
/^([^@\s!#\$%\^&\(\)]+)@((?:[-a-z0-9A-Z]+\.)+[a-zA-Z]{2,})$/

10
Sep

Shoes, a Tiny Toolkit Of Ruby

Shoes is a very informal graphics and windowing toolkit. It’s for making regular old apps that run on Windows, Mac OS X and Linux. It’s a blend of my favorite things from the Web, some Ruby style, and a sprinkling of cross-platform widgets. (More in the README, or at the newshoooes.net!)
click here , you can find [...]

    About

    Tag Cloud