Tuesday, June 30, 2009
Installing Ruby on Rails on Ubuntu
Friday, June 26, 2009

安裝完成後就可以來.... 寫formatter程式XD,說寫,其實是剪剪貼貼啦XD 對程式的部分一知半解的。
Ref:
Convert Ruby code to HTML code for blog posting
本篇前傳請看Here。
直接進入主題,首先,要先安裝一個叫syntax的東西(原諒我稱呼他為"東西" 因為我才讀ruby第二天Q口Q)
安裝的方法是透過command line下:
>gem install syntax
執行畫面如下:
安裝完成後就可以來.... 寫formatter程式XD,說寫,其實是剪剪貼貼啦XD 對程式的部分一知半解的。
程式內容如下:
################
## ruby2html.rb
################
require "rubygems"
# "rubygems" 少了就被抱怨:
#`require': no such file to load -- syntax/convertors/html (LoadError)
require "syntax/convertors/html"
require "Win32API"
require "Win32/clipboard"
include Win32
# 上面這行少會被被抱怨:
#undefined method `data' for Windows::Clipboard:Module (NoMethodError)
in_data = Clipboard.data
convertor = Syntax::Convertors::HTML.for_syntax "ruby"
code_html = convertor.convert(in_data)
Clipboard.set_data(code_html)
以上算是try and error來的XD,我也不知道為什麼別人的程式可以跑,我就要修修改改的。
我把檔名存成"ruby2html.rb"丟在/rails_app/下面。
這支程式是這樣用的: 把要format的code選起來按複製,接著就run剛剛存好的程式:
>ruby ruby2html.rb
跑完後command line不會show什麼訊息,這時候只要開個notepad起來按貼上,就可以看到處理好的html code,就完成啦~
最後,還是要配合CSS,CSS的部份我也自己做了些修改,因為之前已經先有一段跟C#有關的CSS,怕兩種code的CSS會互相影響,以下是我的CSS file:
/* ------------------------- For format ruby codes --------------------------*/
.ruby, .ruby pre {
background-color: #f1f1f3;
color: #112;
padding: 5px;
font-family:"bitstream vera sans mono",monaco,"lucida console","courier new",courier,serif;
font-size: 0.9em;
overflow: auto;
margin: 4px 0px;
width: 95%;
}
/* Syntax highlighting */
.ruby .normal {}
.ruby .comment { color: #005; font-style: italic; }
.ruby .keyword { color: #A00; font-weight: bold; }
.ruby .method { color: #077; }
.ruby .class { color: #074; }
.ruby .module { color: #050; }
.ruby .punct { color: #447; font-weight: bold; }
.ruby .symbol { color: #099; }
.ruby .string { color: #944; background: #FFE; }
.ruby .char { color: #F07; }
.ruby .ident { color: #004; }
.ruby .constant { color: #07F; }
.ruby .regex { color: #B66; background: #FEF; }
.ruby .number { color: #F99; }
.ruby .attribute { color: #5bb; }
.ruby .global { color: #7FB; }
.ruby .expr { color: #227; }
.ruby .escape { color: #277; }
/* -----------------------------------------------------------------------*/
嗯,有了CSS跟html code配合起來,就可以把ruby code漂漂亮亮的顯示出來囉~
另外如果覺得每次都用複製code去做很麻煩的話,也可以format整個.rb檔,請自行參閱Ref 4號,個人因為習慣copy片段,所以這個是比較適合自己的方法。
- Howto format ruby code for blogs (這篇應該算始祖吧..)
- Formatting Ruby and HTML code for blog posting
(這篇參考始祖可以跑... 我參考他就不行跑Q口Q) - syntax ruby code to html in gnome (這篇是成功的關鍵XD)
- Syntax Highlighting (一樣的東西,不過他讀整個.rb檔)
3:50:00 PM | Filed Under ruby | 0 Comments
ruby code format test
天哪!! 我成功了!! 原本是想說開始學ruby了,也應該像C# code一樣去找個formatter來用,這樣以後要貼code比較好看,結果找來找去,好像ruby本身就提供把code轉成html tags的function!! Ruby真的是programmer的好朋友!!!
in_data = Clipboard.data
convertor = Syntax::Convertors::HTML.for_syntax "ruby"
code_html = convertor.convert(in_data)
Clipboard.set_data(code_html)
過程還是有點卡XD 晚點整理好再來分享~
3:38:00 PM | Filed Under ruby, temp | 0 Comments
Wednesday, June 24, 2009
Crystal Report資料存取模式 與 TableLogOnInfo
存取模式詳細說明請見reference。下面只節錄簡易說明:
Crystal Report提拱兩種資料存取模式:
Crystal Report提拱兩種資料存取模式:
- 提取模式
由報表引擎直接向資料庫提取資料。包括連線還有SQL都由Crystal Report處理。 - 推入模式
由開發人員自行撰寫連接資料庫與撈資料部分,再將資料結果(通常是DataSet)推入報表引擎,再顯示於報表。
當使用提取模式時,就需要另外處理報表連接資料庫時所需要的帳號密碼問題。因為安全考量,Crystal Report並不會記憶連接資料庫的帳號密碼。TableLonOnInfo就是提供來處理這個問題的。
1: protected void ConfigCrystalReport(){
2: TableLogOnInfo logOnInfo = new TableLogOnInfo();
3:
4: //load report
5: ReportDocument reportDoc = new ReportDocument();
6: reportDoc.Load(Server.MapPath("rpt_name.rpt"));
7:
8: //set up connection info for each table in report
9: //if the report connect to not only one table in db
10: foreach (CrystalDecisions.CrystalReports.Engine.Table table
11: in reportDoc.Database.Tables)
12: {
13: logOnInfo = table.LogOnInfo;
14:
15: //抓取config檔裡 <AppSettings>區段內設定好的資料
16: //除了下面列出的UserID, Password, 還可設定ServerName, DatabaseName
17: logOnInfo.ConnectionInfo.UserID =
18: ConfigurationManager.AppSettings["_UserId"];
19: logOnInfo.ConnectionInfo.Password =
20: ConfigurationManager.AppSettings["_Password"];
21:
22: // apply settings
23: table.ApplyLogOnInfo(logOnInfo);
24: }
25: }
Ref:
4:53:00 PM | Filed Under crystal report | 0 Comments
Tuesday, June 16, 2009
Use validator and client script at the same time
ㄟ~ 故事是這樣的。
我在Gridview資料繫結時才去幫他加上onclick的屬性:
script在這:
就這樣。好像沒看到什麼時候解開XD? 因為我原本就有在code裡根據其他數據處理判斷是否enable。所以不必另外處理,每次page_load都會重新設定。搞定。
測試的話,以上面例子來說,動作是在save command裡,可以加個
來觀察disable狀態,也可以趁機多按幾次按鈕看是否真的無作用。
寫web最怕的就是連線不穩定,好死不死我寫的某塊功能就是專給海外的人處理資料用的。最近使用者跟我反應存檔後跑出兩筆一模一樣的資料.... 根據我推論,應該是因為網路剛好不穩,使用者按了submit以為沒反應又按了一次... 網路不穩歸不穩,兩次request還是都處理了。
就是這樣,所以要想辦法防止這次的request完成前再送一次。
聽起來超簡單,就按一下submit的時候把按鈕disable,複雜一點的,按一下之後先跟使用者做個確認,確定了再disable。
嗯,沒錯。就這樣,reference都不完全符合我要處理的狀況,不過頗有參考價值。
還是自己搞定了Q口Q
首先,原本寫在LinkButton上的onClientClick就不能直接寫了,因為需要多一層判斷,所以要在程式裡動態幫他把attribute加上去。
1: <!--原本在html裡的client confirm不能用-->
2: <asp:LinkButton runat="server" ID="lbSave" CommandName="save" Text="Save"
3: CommandArgument='<%#DataBinder.Eval(Container,"RowIndex")%>'
4: OnClientClick="return confirm('Are you sure?');">
5: </asp:LinkButton>
6:
7: <!--拿掉後: -->
8: <asp:LinkButton runat="server" ID="lbSave" CommandName="save" Text="Save"
9: CommandArgument='<%#DataBinder.Eval(Container,"RowIndex")%>'>
10: </asp:LinkButton>
我在Gridview資料繫結時才去幫他加上onclick的屬性:
1: //加上onclick attritube, 叫用lockBtn的script
2: ((LinkButton)GridView2.Rows[i].FindControl("lbSave"))
3: .Attributes.Add("onclick", "return lockBtn(this);");
script在這:
1: <script language="javascript" type="text/javascript">
2: //lockBtn: 接受觸發者當參數
3: function lockBtn(btn){
4: //先判斷是否已disable,因為linkBtn就算disable了(會變灰色)
5: //但是click一樣會觸發script
6: if(btn.disabled==true){
7: return false;
8: }
9: else{
10: //取得使用者確認
11: var sure = confirm('Are you sure');
12: if(sure==true){
13: //直接disable掉
14: btn.disabled=true;
15: return true;
16: }
17: else{
18: return false;
19: }
20: }
21: }
22: </script>
就這樣。好像沒看到什麼時候解開XD? 因為我原本就有在code裡根據其他數據處理判斷是否enable。所以不必另外處理,每次page_load都會重新設定。搞定。
測試的話,以上面例子來說,動作是在save command裡,可以加個
1: System.Threading.Thread.Sleep(5000);
來觀察disable狀態,也可以趁機多按幾次按鈕看是否真的無作用。
Ref:
5:14:00 PM | Filed Under c# 3.0 | 0 Comments
Friday, June 12, 2009
待研究主題: SQL語法之 IN vs. EXISTS
兩篇討論from Ask Tom:
11:41:00 AM | Filed Under SQL, temp | 0 Comments
Thursday, June 11, 2009
SharePoint: Recycle Bin
這兩天稍為研究了一下WSS3.0資源回收桶的運作模式。
以系統預設的狀況來看,可以用下圖來說明:
- 使用者從各子網站將任何檔案刪除後,檔案會進入該網站下的資源回收桶。
此時使用者還可自行從資源回收桶裡回復該檔案。
資料庫裡與該檔案相關連的資料 [DeleteTransactionId]欄位會被註記為非"0X"的十六進位值。同時在網站集合層級的[使用者資源回收桶項目]下可以看見這個檔案。 - 使用者從自己網站下的資源回收桶刪除後,即無法再看見該檔案。
但並非永久刪除,此時在網站集合層級的[已從使用者資源回收桶項目刪除]的連結下還可看見該檔案,使用者可要求網站管理員幫助回復檔案。 - 若檔案在[已從使用者資源回收桶項目刪除]被刪除,就真的從資料庫裡把該筆檔案刪除了。
其他細節請看下面參考連結。
Ref:
- Configure quotas with a second-stage Recycle Bin (by UI)
- Stsadm properties (by command mode)
Recycle-bin-cleanup-enabled
Recycle-bin-retention-period
Second-stage-recycle-bin-quota - 設定資源回收桶排程(by command mode)
Job-recycle-bin-cleanup
9:47:00 AM | Filed Under SharePoint | 0 Comments
Wednesday, June 10, 2009
SharePoint: About Deleted Item
重要文件暫記:
2:27:00 PM | Filed Under SharePoint, temp | 0 Comments
c# 3.0 new features Example Demo
1: /////////////////////////////////////////////////////
2: //
3: // 總結所有的 New Features(language extensions)
4: //
5: /////////////////////////////////////////////////////
6:
7: using System;
8: using System.Collections.Generic;
9: using System.Linq;
10: using System.Text;
11: using System.Diagnostics;
12:
13: static class newFeaturesDemo
14: {
15: class ProcessData {
16: public Int32 Id { set; get; }
17: public Int64 Memory { set; get; }
18: public String Name { set; get; }
19: }
20:
21: public static void DisplayProcesses(Func<Process, Boolean> match) {
22:
23: // 1. implicitly typed local variables
24: var processes = new List<ProcessData>();
25:
26: foreach (var process in Process.GetProcesses()) {
27: if (match(process)) {
28:
29: // 2. object initialized
30: processes.Add(new ProcessData
31: {
32: Id = process.Id,
33: Name = process.ProcessName,
34: Memory = process.WorkingSet64
35: });
36: }
37: }
38:
39: // 3. extension methods
40: Console.WriteLine("Total memeory: {0} MB",
41: processes.TotalMemory()/1024/1024);
42:
43: // 4. lambda expression
44: var top2Memory = processes
45: .OrderByDescending(process => process.Memory)
46: .Take(2)
47: .Sum(process => process.Memory) / 1024 / 1024;
48:
49: Console.WriteLine(
50: "Memory consumed by the two most hungry processes: {0} MB",
51: top2Memory);
52:
53: // 5. anonymous type
54: var results = new
55: {
56: TotalMemory = processes.TotalMemory() / 1024 / 1024,
57: Top2Memory = top2Memory,
58: Processes = processes
59: };
60:
61: ObjectDumper.Write(results, 1);
62: ObjectDumper.Write(processes);
63:
64: }
65:
66: static Int64 TotalMemory(this IEnumerable<ProcessData> processes) {
67: Int64 result = 0;
68: foreach (var process in processes) {
69: result += process.Memory;
70: }
71:
72: return result;
73: }
74:
75: static void Main()
76: {
77: DisplayProcesses(
78: process => process.WorkingSet64 >= 20*1024*1024);
79: }
80: }
10:21:00 AM | Filed Under .net, c# 3.0 | 0 Comments
Thursday, June 04, 2009
c# 3.0 new features (part II)
前一篇已經很長了,不想再拉長他的篇幅,所以分篇寫。而且這個趴吐也是相當模糊的不熟悉XD
New Feature #3: Lambda(λ) expressions
講這個之前呢,必須先知道有個delegates(委派)這東西。至於什麼是委派呢... 我也不是很清楚
= =,相當慚愧(orz) 所以才在上一篇貼了個文件XD。大概看得懂啦,不過不知道該怎麼寫。而且也還沒搞懂為什麼要這麼做(他存在的目的?)。
原本的delegates因為有了anonymous methods(匿名方法)而更簡潔。
現在要介紹的lambada expressions就是再進一步精簡他。
下面這個範例來自MSDN,裡面就包含了以具名、匿名,或這裡的重點,以lambda expression來 instantiate(實體化?) delegates:
1: // Declare delegate -- defines required signature:
2: delegate double MathAction(double num);
3:
4: class DelegateTest
5: {
6: // Regular method that matches signature:
7: static double Method4Delegates(double input)
8: {
9: return input * 2;
10: }
11:
12: static void Main()
13: {
14: // Instantiate delegate with named method(具名方法):
15: MathAction ma = Method4Delegates;
16:
17: // Invoke delegate ma:
18: double multByTwo = ma(4.5);
19: Console.WriteLine(multByTwo);
20:
21: // Instantiate delegate with anonymous method(匿名方法):
22: MathAction ma2 = delegate(double input)
23: {
24: return input * input;
25: };
26:
27: double square = ma2(5);
28: Console.WriteLine(square);
29:
30: // Instantiate delegate with lambda expression
31: MathAction ma3 = s => s * s * s;
32: double cube = ma3(4.375);
33:
34: Console.WriteLine(cube);
35: }
36: }
37:
New Feature #4: Extension methods
這個新功能也是個好東西,也一樣很難描述,所以一樣貼貼重點。
This new language feature makes it possible to treat existing types as if they were extended with additional methods.
In order to transform our method into an extension method, all we have to do is add the this keyword to the first parameter. The this keyword instructs the compiler to treat the method as an extension method.
小小看一下他的作用:
這裡有一小行code, 大概先把一堆processes做了些篩選,然後計算篩選出來的processes所佔用的memory, 最後做一個單位的換算。
BytesToMegaBytes(TotalMemory(FilterOutSomeProcesses(processes)));
嗯,對阿,然後呢? 然後如果把剛剛那些method變成extension method的話,就可以長成這樣:
processes
.FilterOutSomeProcesses()
.TotalMemory()
.BytesToMegaBytes();
上面那段code是為了方便閱讀才分成4行的,不是什麼特殊寫法XD。而且,看起來好像FilterOutSomeProcesses(), TotalMemory(), BytesToMegaBytes()就是processes的static method一樣,用個"."(dot)就可以叫用這些功能,他們其實是extension methods~ 酷吧~
但是!!! 以上面的code來說,如果processes其實也有一個instance method叫做TotalMemory呢? 在C#裡(VB.NET不太一樣), 如果extension method跟instance method都符合叫用條件的話,會優先呼叫該instance本身的method。
下面這個example就可以看出來:
1: using System;
2:
3: class Class1 {}
4: class Class2 {
5: public void Method1(string s) { Console.WriteLine("Class2.Method1");}
6: }
7: class Class3 {
8: public void Method1(object o) { Console.WriteLine("Class3.Method1");}
9: }
10: class Class4 {
11: public void Method1(int i) { Console.WriteLine("Class4.Method1");}
12: }
13: static class Extensions {
14: static public void Method1(this object o, int i)
15: {
16: Console.WriteLine("Extensions.Method1");
17: }
18:
19: static void Main()
20: {
21: new Class1().Method1(12);
22: new Class2().Method1(12);
23: new Class3().Method1(12);
24: new Class4().Method1(12);
25: }
26: }
27:
28: // Answer:
29: // Extensions.Method1
30: // Extensions.Method1
31: // Class3.Method1
32: // Class4.Method1
New Features #1, #2 請看c# 3.0 new features
9:49:00 AM | Filed Under .net, c# 3.0 | 0 Comments
Delegates
阿... 還不懂,收個文件。
Both delegates and interfaces enable a class designer to separate type declarations and implementation. A given interface can be inherited and implemented by any class or struct. A delegate can be created for a method on any class, as long as the method fits the method signature for the delegate.
A delegate can be instantiated by associating it either with a named or anonymous method.
9:33:00 AM | Filed Under .net | 0 Comments
Wednesday, June 03, 2009
c# 3.0 new features
前幾個禮拜一直想好好寫一下auto-implemented properties這個主題,拖了一陣子,忽然覺得他沒什麼= =,就不想寫了。發現了個更讓人感興趣的東西,所以就一起寫在這。
New Feature #1: auto-implemented properties
懶的描述,請看連結XD
New Feature #2: Object and collection initializers
這一塊是我感興趣的部分。直接看程式:
1: ProcessData data = new ProcessData();
2: data.Id = 123;
3: data.Name = "MyProcess";
4: data.Memory = 123456;
上面這段程式是很常見的片段: 宣告ProcessDate的object後,初始化一些值。
用C#3.0可以寫成這樣:
var data = new ProcessData{ Id=123, Name="MyProcess", Memory=123456};
嗯, 是認真的。
如果是有constructor的class:
1: var exception = new Exception("message");
2: exception.Source = "LINQ in Action";
3: throw exception;
可以變成這樣:
throw new Exception("message") { Source = "LINQ in Action" };
相當over!!! 以上介紹的是所謂的"Object Initializer", 接下來看看"collection initializer", 他其實並不陌生:
var digits = new List<int> {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
這看起來超眼熟的,他原本應該是這樣:
1: List<int> digits = new List<int>();
2: digits.Add(0);
3: digits.Add(1);
4: digits.Add(2);
5: //....
6: digits.Add(9);
再來一個稍微麻煩一點的:
1: ProcessData tmp;
2: var processes = new List<ProcessData>();
3: //先來第一組資料
4: tmp = new ProcessData();
5: tmp.Id = 123;
6: tmp.Name = "devenv";
7: processes.Add(tmp);
8: //再來第二組資料
9: tmp = new ProcessData();
10: tmp.Id = 456;
11: tmp.Name = "firefox";
12: processes.Add(tmp);
把ojbect initializer跟collection initializer一起用:
1: var processes = new List<ProcessData> {
2: new ProcessData {Id=123, Name="devenv"},
3: new ProcessData {Id=456, Name="firefox"}
4: }
相當簡潔明瞭。以上,要這樣做,必須有一個前提XD(這麼晚才講)
This new syntax allows us to initialize different types of collections, provided they implement System.Collections.IEnumerable and provide suitable Add methods.
(take a break~) 好像還有,讀完後補。
3:27:00 PM | Filed Under c# 3.0 | 0 Comments
PowerBuilder problems
今天整個早上都在跟pb奮戰。遇到幾個詭異的問題= =
1. function忽然壞掉
自己寫的某個function不知道發生了什麼事情,忽然就罷工了。就是程式跑到那,就直接跳過 他。就像有一支funcion會回傳boolean,然後放在if區段裡,程式跑到那,根本不會進去那支 function就跳到else去了... 相當傻眼。搞了半天,可以嘗試做一下Regenerate,搞定。
2. 資料庫欄位長度問題
這個狀況有點複雜,就是兩間公司有自己的資料庫,可是跑的是同樣的系統,同樣的程式。但是資料庫的結構又有那麼點不同,這次遇到的是欄位長度不同。所以某個data window的該欄位在P公司(欄位長度較短)跑起來是正常的,可是在O公司資料卻被截斷了。明明就連到O公司的資料庫了,程式還是顯示P公司資料庫的長度。原本以為長度被寫死了,後來知道原來可以在那個data window按右鍵,選"Edit Source"。可以發現原來column的型別與長度被定義在這裡面,就從這兒給他改掉,顯示上是沒問題,不過目前還不確定如果要操作這筆資料會不會有問題。
這樣,搞一個早上,還有其他零零碎碎的。
2:15:00 PM | Filed Under PowerBuilder | 0 Comments
Tags
Archivo
-
▼
2009
(61)
-
▼
June
(13)
- Installing Ruby on Rails on Ubuntu
- Convert Ruby code to HTML code for blog posting
- ruby code format test
- Crystal Report資料存取模式 與 TableLogOnInfo
- Use validator and client script at the same time
- 待研究主題: SQL語法之 IN vs. EXISTS
- SharePoint: Recycle Bin
- SharePoint: About Deleted Item
- c# 3.0 new features Example Demo
- c# 3.0 new features (part II)
- Delegates
- c# 3.0 new features
- PowerBuilder problems
-
▼
June
(13)