Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Ruby Basic(1)
Author: Jason
Content
Feature
String
Array
Hash
Symbol
Control Struct
Feature
Nearly 100% Object-Oriented
Dynamic
No need to compile
Object-oriented
1.class # => Fixnum
1.2.class # => Float
true.class # => TrueClass
Dynamic
a = "String abc"
a.class # => String
a = 10
a.class # => Fixnum
No need to compile
Enter irb in cmd/terminal
play with Ruby!!
String types
Single quote
Just to store character
Better in performance
Double quote
Store binary value, eg: n
Execute Rub...
String Example
v = "double"
s = 'I am a single quote String' # => "I am a
single quote String"
s = "I am a #{v} quote Stri...
Array(1)
Contain a list of item
The elements can be any type
a = [1, 'count', 3.14]
a[0] # => 1
a[-1] # => 3.14
Array(2)
Short cut to create String Array:
a = %w{jason sam ray}
a # => ["jason", "sam", "ray"]
Hash
Similar to associative array
Key & Value
score = {'jason' => 10, 'ray' => 9, 'sam' => 1}
score['jason'] # => 10
Symbol(1)
They are String
Use as identify
Unique in value (handled by Ruby)
Same object id
All methods and variables have ...
Symbol(2)
In Java, we define somethings like
int NORTH = 1
int EAST = 2
In Ruby you just use :north, and :east to act
as id...
Unique in value
n = :north
n2 = :north
if n == n2
puts "n is equal to n2"
end
• Results : n is equal to n2
Same object ID
n = :north
n2 = :north
n.object_id # => 292808
n2.object_id # => 292808
Symbol in Hash
Symbol usually acts as key in hash to save
memory.
Ruby 1.8:
score = {:jason => 10, :ray =>4, :sam => 1}
Ru...
Control Structure(1)
if jason == 'jason'
puts "I am Jason"
end
Control Structure(2)
if jason == 'jason'
puts "I am not Jason"
else
puts "I am Jason"
end
Control Structure(3)
if jason == 'jason'
puts "I am not Jason"
elsif jason == 'handsome'
puts "Jason is handsome"
else
put...
Control Structure(4)
while jason < 100 and jason > 50
jason = jason +1
end
and is equal to &&
or is equal to ||
Upcoming SlideShare
Loading in …5
×

Ruby basic

531 views

Published on

Introduce basic data type of ruby

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Ruby basic

  1. 1. Ruby Basic(1) Author: Jason
  2. 2. Content Feature String Array Hash Symbol Control Struct
  3. 3. Feature Nearly 100% Object-Oriented Dynamic No need to compile
  4. 4. Object-oriented 1.class # => Fixnum 1.2.class # => Float true.class # => TrueClass
  5. 5. Dynamic a = "String abc" a.class # => String a = 10 a.class # => Fixnum
  6. 6. No need to compile Enter irb in cmd/terminal play with Ruby!!
  7. 7. String types Single quote Just to store character Better in performance Double quote Store binary value, eg: n Execute Ruby code in #{}
  8. 8. String Example v = "double" s = 'I am a single quote String' # => "I am a single quote String" s = "I am a #{v} quote String" # => "I am a double quote String" #{} is to execute the ruby code
  9. 9. Array(1) Contain a list of item The elements can be any type a = [1, 'count', 3.14] a[0] # => 1 a[-1] # => 3.14
  10. 10. Array(2) Short cut to create String Array: a = %w{jason sam ray} a # => ["jason", "sam", "ray"]
  11. 11. Hash Similar to associative array Key & Value score = {'jason' => 10, 'ray' => 9, 'sam' => 1} score['jason'] # => 10
  12. 12. Symbol(1) They are String Use as identify Unique in value (handled by Ruby) Same object id All methods and variables have its own symbol
  13. 13. Symbol(2) In Java, we define somethings like int NORTH = 1 int EAST = 2 In Ruby you just use :north, and :east to act as identify, they should be unique in value
  14. 14. Unique in value n = :north n2 = :north if n == n2 puts "n is equal to n2" end • Results : n is equal to n2
  15. 15. Same object ID n = :north n2 = :north n.object_id # => 292808 n2.object_id # => 292808
  16. 16. Symbol in Hash Symbol usually acts as key in hash to save memory. Ruby 1.8: score = {:jason => 10, :ray =>4, :sam => 1} Ruby 1.9, you can also: score = {jason: 10, ray: 4 ,sam: 1} score[:jason] # => 10
  17. 17. Control Structure(1) if jason == 'jason' puts "I am Jason" end
  18. 18. Control Structure(2) if jason == 'jason' puts "I am not Jason" else puts "I am Jason" end
  19. 19. Control Structure(3) if jason == 'jason' puts "I am not Jason" elsif jason == 'handsome' puts "Jason is handsome" else puts "I am Jason" end
  20. 20. Control Structure(4) while jason < 100 and jason > 50 jason = jason +1 end and is equal to && or is equal to ||

×