Download presentation
Presentation is loading. Please wait.
Published byVirginia Foster Modified over 6 years ago
1
Ruby Getting started & Ruby.New Presented by Mario Tayah
April 14, 2006
2
installation Ruby comes with most mac os as well as linux distributions. Binary vs. build from source
3
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
4
From source Download the source: Unpack : tar xzf snapshot.tar.gz
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…
5
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 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):
6
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.
7
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
8
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)
9
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()
10
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.
11
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.
12
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…)
13
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
14
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.
15
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
16
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.
17
Blocks and iterator Def call_block puts “start of method” yield
puts “end of method” end call_block {puts “in block”} Will produce: Start of method In block End of method
18
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 .
19
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
20
questions ?
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.