Presentation #19
Download
Report
Transcript Presentation #19
Ruby
Getting started
&
Ruby.New
Presented
by
Mario Tayah
April 14, 2006
1
installation
• Ruby comes with most mac os as well as
linux distributions.
• Binary vs. build from source
2
binary
• Works out of the box
• www.rpmfind.net (rpm based linux)
• Debian dpkg you use
– #apt-cache search ruby interpreter
– #apt-get install ruby…
• Windows http://rubyinstaller.rubyforge.org
3
From source
• Download the source:
–
–
–
–
http://www.ruby-lang.org
Stable release: tarball format
Stable snapshot (overnight)
Nightly development: under development
• Unpack : tar xzf snapshot.tar.gz
• Then build and install depends on system you
are using but usually it is something like:
• ./configure or make or make install…
4
Latest ruby version
• If you feel excited about ruby and want to put your hands
on the currently developed version, you can have access
to the repository of the development team.
• This is done through logging into
[email protected]
pass: enter
you can check out the whole development tree, or the
latest version, but definitely cannot check anything in.
for more info about the commands to use for check out
and for some additional options mirroring):
www.cvshome.org, www.cvsup.org
5
Running ruby
• 2 ways:
– Command lines
•
•
•
•
Def sum(n1,n2)
n1+n2
end
Sum(3,4)
– Programs, you can include this in a program
and simply ask the ruby interpreter just as you
do with any other scripting language.
6
doc
• Ruby standard library 9000 methods
• Use RDoc which is a system to extract
documentation from within a ruby file and
to an HTML document.
• Moreover, when you install ruby, you get
help and documentation about the
compiler as well as the libraries
7
Brief of OO in ruby
• Everything is an object, it has data and
methods that manipulate that data. Even
numbers are object.
• Ex:
– “hello”.lenght
– “Hello”.index(“e”)
– -1942.abs
– Class1.play(param)
8
Some def.
•
•
•
•
State: variables
Instances/receiver have identifiers (obj1)
Methods (functions, messages)
Methods are implemented using passing
of messages to the instance.
• Constructor classinstance = class.new()
9
syntax
• No need for “;”
• “#” defines comments that runs until the
end of the line
• Ignores empty spaces
• Invoke mrthod caller.method(params)
note : no “;”
• You can sometimes not use the () for
enclosing the params.
10
More syntax
• Local variables, method params, method
names, start with lower case or _
• Global var “$”
• Instance var start with an “@”
• Class variable start with an “@@”
• Class names, module names, constants
start with an uppercase
• The rest of the expression can be any
combination words, characters.
11
strings
• String are defined with “” or ‘’
– ‘’ ruby simply takes what is inside the ‘’ as a
string
– “” ruby takes into account escape characters
– “#{expression}” evaluates to the value of
expression.
– Sub, gsub…(substitute…)
12
Arrays/hashes
• Arrays and hashes both grow as needed,
they can hold any combination of types(all
objects).
• Arrays provide faster access, hashes more
flexible.
• Ex: a= [1,”hello”,2.1]
• Access a[index starting at 0]
• Out of bound indexing returns nil
13
Arrays/hashes
• Hashes are similar to arrays but indexed
with user defined keys.
– Hashin = {
‘one’ =>’oneval’,
‘two’ => ‘twoval’
}
Hashin[‘one’]
Key and value are arbitrary types, key should
be unique.
14
Control/structure
• If, while… they do not use() or{ they just
use the “end” keyword to indicate the
closing of a control statement/scope.
• Statement modifier:
If x>0
Puts “danger”
End
Can be replaced by
Puts “danger” if x>0
15
Regular expressions
• Regular expression are very useful, but
rarely included in languages other than
scripting languages.
• Regular expressions are expression like
those that you usually see in old dos.
– Ex: *.*, x *…
– Ex: line.sub(/perl *p/,’python’)
Substitutes every occ of perl zero or more
spaces then a p with python.
16
Blocks and iterator
• Def call_block
puts “start of method”
yield
yield
puts “end of method”
end
call_block {puts “in block”}
Will produce:
Start of method
In block
In block
End of method
17
Blocks and iterators
•
•
•
•
•
•
[‘cat’,’dog’].each{|name| print name, “ ”}
5.times{print “*”}//print a * 5 times
3.upto(6){|i| print i}//prints 3 to 6
.
.
.
18
I/O
• puts // writes to the output // add new line
• print // writes to the output //no new line
• The above two can be used to write to any
i/o object
• gets // new line of the standard input to the
program
19
questions
?
20