網頁

顯示具有 Ruby 標籤的文章。 顯示所有文章
顯示具有 Ruby 標籤的文章。 顯示所有文章

2016年7月13日 星期三

Ruby method / block / yield / Proc / lambda 全面解釋

http://railsfun.tw/t/method-block-yield-proc-lambda/110

2012年4月6日 星期五

Working with Unicode in Ruby

ruby的irb中要顯示中文
輸入
irb> $KCODE = 'u'
就可以了

其它處理utf8字串的有
irb> require 'jcode'
irb> require 'rubygems'
irb> require 'unicode'

詳細參考 http://ideaharbor.org/notes/technical/working-with-unicode-in-ruby

2011年3月25日 星期五

Ruby19 encoding issues

ruby1.9 encoding issues

To upgrade without any errors you should add this line at the top of all your ruby scripts:

# encoding: utf-8

otherwise it’ll throw SyntaxErrors: “invalid multibtyte char (US-ASCII)”

Also you should use mysql2 gem (or new versions of pg and sqlite-ruby) in your application, otherwise you’ll see:

Encoding::CompatibilityError: incompatible character encoding: UTF-8 and ASCII-8BIT

from:http://def-end.com/post/3065938198/ruby1-9-encoding-issues

Ruby執行系統指令

如果是要直接呼叫系統指令,底下有幾種方式可以呼叫系統指令

`` # 就是在鍵盤上"ㄅ"左邊那個按鍵,通常跟"~"印在一起
%x() # 括號也可以用雙引號代替,像是%x""或者%x!!
exec # 透過exec來執行系統指令的話,會在執行結束後一併結束Ruby
system # 用法跟exec一樣,但是結束指令時並不會一併結束Ruby

來源:http://ithelp.ithome.com.tw/question/10000145

2009年11月19日 星期四

使用Ruby修改檔案內容

以下是使用Ruby修改檔案,主要目的是修改sympa(mailing list)中許多目錄(list)中的config檔案內容。
using ruby modify file content.
config的內容中找到"owner_include"區段將"profile normal"替換成"profile privileged"。

<--
Dir.chdir("/usr/local/sympa/expl")
Dir.glob("*-*").each do |x|
if File::directory?(x)
config_file = File.join(x, 'config')
if File.exist?(config_file)
puts config_file
flag = false
File.open(config_file, 'r+') do |f|
out = ""
f.each do |line|
if line =~ /owner_include/
flag = true
elsif line =~ /^$/
flag = false
elsif flag == true
if line =~ /profile normal/
line = "profile privileged\n"
end
end
out << line
end
#write to file
f.pos = 0 #from line 1
f.print out #write
f.truncate(f.pos) #truncate after current position
end
end
end
end
-->