發現了這個學習ruby的好地方,每天就盡量抽點時間學習一下吧。
http://www.rubylearning.com/forum/
今天學習第一課,比較簡單.
1.1
Introduction
簡介ruby
1.2 Installation
1)Downloading Ruby and an Editor
:
下載地址:
Ruby Installer for Windows
安裝包中包括一個ruby的編輯器SciTE.
2)Ruby Programming Environment:
假設我們裝在C:/ruby目錄下,系統將創建一些新的文件夾。
c:/ruby/bin:ruby子帶的命令以及可執行文件
c:/ruby/lib/ruby/1.8:ruby的一些標準庫
c:/ruby/lib/ruby/1.8/i386-mswin32:與平臺相關聯的庫
c:/ruby/lib/ruby/site_ruby: 存放工具庫或第三方庫
c:/ruby/lib/ruby/gems: Ruby-Gems 包目錄.
c:/ruby/src:ruby代碼目錄.
c:/ruby/samples/RubySrc-1.8.6/sample: ruby例子代碼目錄.
1.3
First Ruby program
1) 可以使用ruby自帶的SciTE.作為編輯器(程序->ruby->SciTE). 對于SciTE,使用F8打開輸出,F5運行ruby程序
2) ruby的文件一般以.rb后綴
3) 經典的hellowor!(在SciTE中新建文件hello.rb,然后輸入)
puts 'Hello ' + "world " + 'ruby!'
4) 總結:
a. 不需要main方法或者函數
b. 字符串可以用‘’ 、“”
c. ruby是腳本語言,直接運行,不需要編譯
d. ruby的命名規范建議文件名為小寫,如Foo class命名foo.rb
1.4
Features of Ruby
1) 大小寫敏感
2) 單行注釋可以用#,多行注釋可以用=begin開始,=end結束
3) 一行中的多行代碼必須用;分割
4)使用\轉義 或者 連接多行
1.5 Numbers in Ruby
在ruby中,帶.的數稱為浮點數,不帶.的數稱為整數
1) 整數和整數的操作結果為整數
2) ruby使用Fixnum(默認)和
Bignum處理超大數
3) 繼承結構:
Object
|
Numric
| |
Integer Float
|
Bignum Fixnum
1.6
String fundamentals
在ruby中,ruby作為bytes的序列存儲,不像java的String不可修改
1) 字符串可以使用“ ”或者' ',但' '更有效
2) 使用\轉義
3) 可以使用` ` 執行Console 命令, 如 puts `dir`等
1.7 Variables and Assignment
在ruby中,當解釋器判斷到賦值符號=,就認為存在一個變量.
1) 局部變量由_或者小寫字母開頭,由字母、數字、下劃線組成
2) << 可以向一個字符串中添加字符串,如 a = 'hello '; a<<'world.
1.8 總結:(原網站上的總結,很容易理解)
* We are discussing about Ruby 1.8.6 on a Windows platform.
* Ruby is an interpreted language
* In Ruby, there's always more than one way to solve a given problem.
* The code examples would be run in the SciTE editor and ensure that you have done the relevant settings as mentioned on the page "First Ruby program".
* All Ruby source files have the .rb file extension.
* In Ruby, program execution proceeds in general from top to bottom.
* Features: Free format, Case sensitive, Two type of comments, Statement delimiters are not required, Around 38 Keywords
* You may be used to thinking that a false value may be represented as a zero, a null string, a null character, or various other things. But in Ruby, all of these are true; in fact, everything is true except the reserved words false and nil.
* We shall be referring to the documentation here - http://www.ruby-doc.org/core/
* puts (s in puts stands for string; puts really means put string) simply writes onto the screen whatever comes after it, but then it also automatically goes to the next line.
* Parentheses are usually optional with a method call. These calls are all valid:
foobar
foobar()
foobar(a, b, c)
foobar a, b, c
* In Ruby, numbers without decimal points are called integers, and numbers with decimal points are usually called floating-point numbers or, more simply, floats (you must place at least one digit before the decimal point).
* Some very common Ruby operators:+ addition; - subtraction; * multiplication; / division
* The increment and decrement operators (++ and --) are not available in Ruby, neither in "pre" nor "post" forms.
* Anything inside brackets is calculated first (or, more technically, given higher precedence).
* Observe how the modulus operator (%) works in Ruby.
* When you do arithmetic with integers, you'll get integer answers.
* String literals are sequences of characters between single or double quotation marks.
* In Ruby, strings are mutable. They can expand as needed, without using much time and memory.
* String concatenation is joining of two strings, using the + operator.
* Escape sequence is the \ character. Examples: \", \\, \n
* '' is an empty string.
* If you get a compilation error like -
#<TypeError: can't convert Fixnum into String>
it means that you can't really add a number to a string, or multiply a string by another string.
* Constants begin with capital letters. Example PI, Length
* A variable springs into existence as soon as the interpreter sees an assignment to that variable. It is a good practice to assign nil to a variable initially.
* x, y = y, x will interchange the values of x and y.
* Local variables must start with either a lowercase letter or the underscore character (_), and they must consist entirely of letters, numbers, and underscores.Examples: india, _usa, some_var
* .to_i, .to_f, .to_s are used to convert to an integer, float, string respectively.
* The operator << is used to append to a string