Programming Ruby Object

#Object
#string t copyed from string s
s="Ruby"
t=s
p t #=>"Ruby"
s="Rails"
p t #=>"Ruby"
p s #=>"Rails"


t[-1]=""
print s #=>Rails
puts "\n"
print t #=>Rub

o="test"
o.class
o.class.superclass
o.class.superclass.superclass
if o.class==String
puts "\n"
p o
end

if o.instance_of? String
p o
end


x=1
if x.instance_of? Fixnum
puts Fixnum
end #=>true
if x.instance_of? Numeric
puts Numeric
end #=>false
if x.is_a? Fixnum
puts Fixnum
end #=>true
if x.is_a? Numeric
puts Numeric
end #=>true
if x.is_a? Integer
puts Integer
end #=>true
if x.is_a? Comparable
puts Comparable
end #=>true
if x.is_a? Object
puts Object
end #=>true
if Numeric===x
puts Numeric
end #=>true

if x.respond_to?:"<<"
puts "<< true"
end #=>true


#The equal? method
a="Ruby"
b=c="Ruby"
if a.equal?(b)
puts "True"
end #=>false a b c are not exactly the same
if b.equal?(c)
puts "True"
end #=>true b and c are exactly the same

if a.object_id==b.object_id
puts "true"
end #=>false works like a.equal?(b)

if (1..10)===5
puts "include 5"
end #true

if /\d+/ === "123"
puts "include 123"
end #true

if String==="s"
puts "String"
end #true

if String==="s"
puts "String"
end #true

if :s ==="s"
puts ":s"
else
puts "not"
end #false

lr=1<=>5
puts lr #-1
lr=5<=>5
puts lr #0
lr=9<=>5
puts lr #1
lr="1"<=>5
puts lr #nil

if 1.between?(0,10)
puts "1.between(0,10)"
end #true

Arithmetic operator type coercions
coerce Fixnum to Float
p 1.1.coerce(1) #[1.0,1.1]

Use Rational numbers
One third as a Rational number
require "rational"
r=Rational(1,3)
p r #(1/3)
Rational(2,1),Rational(1,3) Fixnum to Rational
p r.coerce(2) #[(2/1), (1/3)]

#Freezing Objects
s="ice" #strings are mutable objects
s.freeze #Make this string immutable
s.frozen? #true:it has been frozen
s.upcase! #typeError:can't modify frozen string
s[0]="ni" #TypeError:can't modify frozen string

#Tainted and Untrusted Obects
s="untrusted" #Objects are normally untainted
s.tainted #Mark this untrusted object as tainted
s.tainted? #true:it is tainted
s.upcase.tainted? #true:derived objects are tainted
s[3,4].tainted? #true:substrings are tainted