发现了这个学习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