Date post: | 29-May-2018 |
Category: |
Documents |
Upload: | chang-sau-sheong |
View: | 239 times |
Download: | 0 times |
of 41
8/8/2019 Ruby Course - Lesson 1 - Introduction to Ruby
1/41
Introduction toRuby
Lesson 1
Saturday, September 11, 2010
8/8/2019 Ruby Course - Lesson 1 - Introduction to Ruby
2/41
Philosophybehind Ruby
Saturday, September 11, 2010
8/8/2019 Ruby Course - Lesson 1 - Introduction to Ruby
3/41
Humanoriented
Saturday, September 11, 2010
8/8/2019 Ruby Course - Lesson 1 - Introduction to Ruby
4/41
Principle of least surprise
Saturday, September 11, 2010
8/8/2019 Ruby Course - Lesson 1 - Introduction to Ruby
5/41
Fun to program in
Saturday, September 11, 2010
8/8/2019 Ruby Course - Lesson 1 - Introduction to Ruby
6/41
Features of Ruby
Saturday, September 11, 2010
8/8/2019 Ruby Course - Lesson 1 - Introduction to Ruby
7/41
Everything is an object1. class # => Fixnum' a '. class # => String: z . class # => Symbol
class PersonendPerson . class # => Class
Person . new. class # => Person
Saturday, September 11, 2010
8/8/2019 Ruby Course - Lesson 1 - Introduction to Ruby
8/41
On the y
def double_it ( input )
input * 2enddouble_it ( 1) # => 2double_it (" a") # => "aa"
Saturday, September 11, 2010
8/8/2019 Ruby Course - Lesson 1 - Introduction to Ruby
9/41
Open for changeclass Array def first_half slice( 0, size / 2)
endenda = [ 1, 2, 3, 4, 5, 6]puts a. to_s #=> 123456
puts a. first_half . to_s #=> 123
Saturday, September 11, 2010
8/8/2019 Ruby Course - Lesson 1 - Introduction to Ruby
10/41
Program your programs
class Person attr_accessor :first_name , :last_name
attr_reader :person_id alias :last_name , :family_nameend
Saturday, September 11, 2010
8/8/2019 Ruby Course - Lesson 1 - Introduction to Ruby
11/41
Express yourself
me = people. find { |p| p. first_name ==' Sau Sheong ' }
family = people. select { |p| p. last_name == ' Chang ' }
Saturday, September 11, 2010
8/8/2019 Ruby Course - Lesson 1 - Introduction to Ruby
12/41
Perl
SmallTalkLispRuby
+
Saturday, September 11, 2010
8/8/2019 Ruby Course - Lesson 1 - Introduction to Ruby
13/41
Current versions of Ruby
Saturday, September 11, 2010
8/8/2019 Ruby Course - Lesson 1 - Introduction to Ruby
14/41
Matz Ruby InterpreterRuby 1.8.7
Saturday, September 11, 2010
8/8/2019 Ruby Course - Lesson 1 - Introduction to Ruby
15/41
Yet Another Ruby VM-> Ruby 1.9.1
Saturday, September 11, 2010
8/8/2019 Ruby Course - Lesson 1 - Introduction to Ruby
16/41
JRuby (1.4)Ruby 1.8 (1.9) on the JVM
Saturday, September 11, 2010
8/8/2019 Ruby Course - Lesson 1 - Introduction to Ruby
17/41
RubiniusRuby 1.8 in Ruby
(and a bit of C++)
Saturday, September 11, 2010
8/8/2019 Ruby Course - Lesson 1 - Introduction to Ruby
18/41
IronRuby (0.9.2)Ruby 1.8
Saturday, September 11, 2010
8/8/2019 Ruby Course - Lesson 1 - Introduction to Ruby
19/41
MacRuby (0.6)Ruby 1.9 in Objective-C
Saturday, September 11, 2010
8/8/2019 Ruby Course - Lesson 1 - Introduction to Ruby
20/41
Installing Ruby
Saturday, September 11, 2010
8/8/2019 Ruby Course - Lesson 1 - Introduction to Ruby
21/41
http://www.ruby-lang.org/en/downloads/
Windows - RubyInstaller
Mac - already pre-installed (1.8.7); oruse port
Linux (Debian, Ubuntu etc)% sudo apt-get install ruby1.9.1-full
Saturday, September 11, 2010
http://www.ruby-lang.org/en/downloads/http://www.ruby-lang.org/en/downloads/http://www.ruby-lang.org/en/downloads/8/8/2019 Ruby Course - Lesson 1 - Introduction to Ruby
22/41
Tools
Saturday, September 11, 2010
8/8/2019 Ruby Course - Lesson 1 - Introduction to Ruby
23/41
Rubygems
Saturday, September 11, 2010
8/8/2019 Ruby Course - Lesson 1 - Introduction to Ruby
24/41
Interactive Ruby(irb)
Saturday, September 11, 2010
8/8/2019 Ruby Course - Lesson 1 - Introduction to Ruby
25/41
Language
Saturday, September 11, 2010
8/8/2019 Ruby Course - Lesson 1 - Introduction to Ruby
26/41
Strings and Variables
' Hello, Sau Sheong! '
name = ' Sau Sheong ' # => "Sau Sheong"" Hello, #{ name}! " # => "Hello, Sau Sheong!"
Saturday, September 11, 2010
8/8/2019 Ruby Course - Lesson 1 - Introduction to Ruby
27/41
Methodsstr = " Hello, World! " # => "Hello, World!"str . size # => 13str . sub (" World ", " Ruby") # => "Hello, Ruby!"
def greet ( name=" Sau Sheong ")
" Greetings, #{ name}! "end
puts greet
puts greet (' everyone ')# >> Greetings, Sau Sheong!# >> Greetings, everyone!
Saturday, September 11, 2010
8/8/2019 Ruby Course - Lesson 1 - Introduction to Ruby
28/41
Numbers
1 + 2 # => 31 + 2.1 # => 3.1
2**10 # => 10249 / 2 # => 45 % 3 # => 2- 3. abs # => 31. class # => Fixnum( 2** 50). class # => Bignum1.3 . class # => Float
Saturday, September 11, 2010
8/8/2019 Ruby Course - Lesson 1 - Introduction to Ruby
29/41
ArraysArray . new # => [][ 1, 2, 3] # => [1, 2, 3][ 1] + [ 2] [1, 2, 3][ 1, 2, 3] - [ 2, 3] # => [1]%w(one two ) # => ["one", "two"][ 1, 2, 3]. pop # => 3[ 1, 2]. push ( 3) # => [1, 2, 3]
[ 1, 2, 3]. shift # => 1[ 2, 3]. unshift ( 1) # => [1, 2, 3][ 1, 3]. insert ( 1, 2) # => [1, 2, 3]
Saturday, September 11, 2010
8/8/2019 Ruby Course - Lesson 1 - Introduction to Ruby
30/41
Hashes
options = {: name => " Ruby"}
h = Hash . new # => {}h[' string '] = 1h[: symbol ] = 1h # => {:symbol=>1, "string"=>1}
h. keys # => [:symbol, "string"]h[: symbol ] # => 1
Saturday, September 11, 2010
8/8/2019 Ruby Course - Lesson 1 - Introduction to Ruby
31/41
Iterators
for n in 0.. 2puts n
end
( 0.. 2). each do | n|puts n
end
[ 0, 1, 2]. each {| n| puts n }
Saturday, September 11, 2010
8/8/2019 Ruby Course - Lesson 1 - Introduction to Ruby
32/41
Enumerable
ary = [ 1, 2, 3]
ary . map {| n| n * 2} # => [2, 4, 6]ary . select {| n| n % 2 == 0} # => [2]ary . inject ( 0) {| sum, n| sum + n} # => 6
Saturday, September 11, 2010
8/8/2019 Ruby Course - Lesson 1 - Introduction to Ruby
33/41
Control structuresif some_condition # ...elsif other_condition # ...else # ...end
while condition # ...end
puts " lol " if funny?
Saturday, September 11, 2010
8/8/2019 Ruby Course - Lesson 1 - Introduction to Ruby
34/41
Regular Expressionsstr = " Hello, world! "str . gsub (/[ aeiou ]/," *") # => "H*ll*, w*rld!"str =~ / w( or ) ld / # => 7
$1 # => "or"match = str . match (/ w( or ) ld /) # => #match [ 0] # => "world"match [ 1] # => "or"
Saturday, September 11, 2010
8/8/2019 Ruby Course - Lesson 1 - Introduction to Ruby
35/41
Error handlingbegin raise " Standard error "rescue => e
p eend# >> #
begin raise StandardError . new(" Oh, oh ")rescue RuntimeError
puts " runtime "rescue StandardErrorputs " standard "
end# >> standard
Saturday, September 11, 2010
8/8/2019 Ruby Course - Lesson 1 - Introduction to Ruby
36/41
Files
File . open (' test.txt ',' w') do | file |file . write (" line \n ")file . puts (" line ")
endFile . read (' test.txt ')
Saturday, September 11, 2010
8/8/2019 Ruby Course - Lesson 1 - Introduction to Ruby
37/41
Classesclass Calc attr_accessor : factor
def initialize ( factor =2) @factor = factor end
def multiply ( value ) @factor * value endend
calc = Calc . newcalc . multiply 5 # => 10calc . factor = 3calc . multiply 5 # => 15
Saturday, September 11, 2010
8/8/2019 Ruby Course - Lesson 1 - Introduction to Ruby
38/41
Inheritanceclass Foo def greet " Hi, #{ value }" end def value " foo "
endend
class Bar < Foo def value " bar " endend
Foo . new. greet # => "Hi, foo"Bar . new. greet # => "Hi, bar"
Saturday, September 11, 2010
8/8/2019 Ruby Course - Lesson 1 - Introduction to Ruby
39/41
Class methodsclass Foo def self.bar " BAR!" end
def bar " bar " endend
Foo . bar # => "BAR!"Foo . new. bar # => "bar"
Saturday, September 11, 2010
8/8/2019 Ruby Course - Lesson 1 - Introduction to Ruby
40/41
Mixinsmodule Even def even? self % 2 == 0
endend
class Fixnum include Evenend
1. even? # => false2. even? # => true
Saturday, September 11, 2010
8/8/2019 Ruby Course - Lesson 1 - Introduction to Ruby
41/41
Questions?