http://tryruby.hobix.com/ 提供了一個命令列,除了可以聯絡Ruby內建的方法,還提供了以下有趣的有用的5個命令:help 15分鐘左右的嚮導可以讓你學習到ruby最基本的東西 help 2 第二章,同樣可以用help 3進入第三章,當你沒有連續的很長時間去看完它時,可以直接跳到某一章,共8章 clear 清屏,當你發現瀏覽器開始變慢時用它,它會清楚命令曆史 back 返迴向導中的上一級頁面 time
each最簡單的迭代器,它所做的就是連續訪問收集的所有元素。 [1,3,5,7,9].each { |i| puts i } 輸出結果:13579獨特的作用就是用來實現Ruby語言的for迴圈。因為在Ruby內建的迴圈原語只有while和until。for只是一個文法塊。例如:for song in songlist song.playend Ruby會用each迭代器轉換為:songlist.each do |song|
1.Block: Ruby中的塊就是由多行程式碼群組成的一個代碼塊,通常可以把它認為是一個匿名方法,常用來迭代一個數組或範圍(如each, times方法);文法格式如下: { //code } OR do //code end 塊變數:當建立一個塊時,在兩個豎線之間(如: | i |)被稱作塊變數,作用和一個正常方法的參數一樣;如:5.each{| x |
Using Ruby On Rails on Bluehost:This is intended to be a brief introduction to developing ruby on rails applications on a bluehost account. At the bottom of this article you will find a number of resources to help you learn more about ruby on rails
今天,下面這段程式讓我糾結了很久,Ruby中private的概念真的很奇怪。。。 class Test private def test_print puts 'test' endendclass Test2 < Test def test_print2 # self.test_print #=> 這裡加上self就不能調用,private method `test_print' called for # (NoMethodError) test_print
# Part oneaFile = File.new("C:\\1.txt","w") aFile.puts "the 1" aFile.puts "the 2"aFile.close # Part twoaFile = File.new("C:\\2.txt","w") puts "the 1" puts "the 2"aFile.close閑的無聊,看了一下Ruby如何讀寫檔案,準備寫段代碼來產生測試資料。上面有兩段代碼,看出有什麼區別嗎?嗯
1、中文在Scite編輯器中顯示亂碼的問題解決方案,使用記事本開啟rb指令碼,然後另存新檔,選擇編碼方式為utf-8,然後儲存。在用Scite開啟該指令碼即可。如何讓Scite控制台也正常輸出中文?方法如下:開啟scite->options,在裡面找到"open global options file",把裡面的內容全選複製(本來是可以在這裡改的,但是由於是全域配置,不建議在這裡直接修改),再開啟“open user options
1驗證輸入的內容是否為空白,不可為空 在modelsvalidates_presence_of :title,:description,:image_url2驗證輸入的內容為數字型validates_numericality_of:price3驗證輸入的資料price不小於 0.01protecteddef validateerrors.add(:price,"should be at least 0.01") if