ruby学习笔记系列(1)

本文介绍了Ruby编程的基本概念,包括代码样例的表示方式、通过CVS安装最新版本的Ruby过程、使用irb进行交互式Ruby编程的方法,以及Ruby中的控制结构。


Notation Conventions
Throughout this book, we use the following typographic notations.
Literal code examples are shown using a typewriter-like font.
class SampleCode
def run
#...
end
end
Within the text, Fred#do_something is a reference to an instance method (in this case
do_something) of class Fred, Fred.new2 is a class method, and Fred::EOF is a class
constant. The decision to use a hash character to indicate instance methods was a tough
one: it isn’t valid Ruby syntax, but we thought that it was important to differentiate
between the instance and class methods of a particular class. When you see us write
File.read, you know we’re talking about the class method read. When instead we
write File#read, we’re referring to the instance method read.

2

install ruby(cvs install)

For those who just have to be on the very latest, hot-off-the-press
and untested cutting edge (as we were while writing this book), you
can get development versions straight from the developers’ working
repository.
The Ruby developers use CVS (Concurrent Version System, freely
available from https://www.cvshome.org) as their revision control
system. You can check files out as an anonymous user from their
archive by executing the following CVS commands:
% cvs z4
d
:pserver:anonymous@cvs.rubylang.
org:/src←֓
login
(Logging in to anonymous@cvs.rubylang.
org)
CVS password: ENTER
% cvs z4
d
:pserver:anonymous@cvs.rubylang.
org:/src←֓
checkout ruby
The complete source code tree, just as the developers last left it, will
now be copied to a ruby subdirectory on your machine.
This command will check out the head of the development tree. If you
want the Ruby 1.8 branch, add r
ruby_1_8 after the word checkout
in the second command.
If you use the CVSup mirroring utility (conveniently available from
http://www.cvsup.org), you can find Ruby supfiles on the rubylang
site at http://cvs.rubylang.
org/cvsup/.
Interactive Ruby
One way to run Ruby interactively is simply to type ruby at the shell prompt. Here
we typed in the single puts expression and an end-of-file character (which is Ctrl+D
on our system). This process works, but it’s painful if you make a typo, and you can’t
really see what’s going on as you type.
% ruby
puts "Hello, world!"
^D
Hello, world!
For most folks, irb—Interactive Ruby—is the tool of choice for executing Ruby interactively.
irb is a Ruby Shell, complete with command-line history, line-editing capabilities,
and job control. (In fact, it has its own chapter beginning on page 174.) You run
irb from the command line. Once it starts, just type in Ruby code. It will show you the
value of each expression as it evaluates it.
Prepared exclusively for Yeganefar

3
  Control Structures

if count > 10
puts "Try again"
elsif tries == 3
puts "You lose"
else
puts "Enter a number"
end


while weight < 100 and num_pallets <= 30
pallet = next_pallet()
weight += pallet.weight
num_pallets += 1
end
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值