x=[]
x << "Apple"
x << "orange"
x << "banana"
puts x.length
puts x[0]
puts x[1]
puts x[2]
puts x.pop
puts x.pop
puts x.length
2009年5月3日 星期日
iteration string
string1="this is a ruby"
string1.scan(/./) {|letter| puts letter}
string1.scan(/../) {|letter| puts letter}
string2="there are $1000 dollers here 10 days"
string2.scan(/\d+/) do |x|
puts x
end
string1.scan(/./) {|letter| puts letter}
string1.scan(/../) {|letter| puts letter}
string2="there are $1000 dollers here 10 days"
string2.scan(/\d+/) do |x|
puts x
end
string substitution
string1="the language name is ruby"
puts string1.sub('a','A')
puts string1.gsub('a','A')
puts string1.sub('a','A')
puts string1.gsub('a','A')
interpolation string
x=10
y=20
puts "#{x}+#{y}=#{x+y}"
my_string="It\'s a #{"bad " *5} world"
puts my_string
y=20
puts "#{x}+#{y}=#{x+y}"
my_string="It\'s a #{"bad " *5} world"
puts my_string
mutli line in ruby string
string1="<<"NEF
welcome to use ruby language
nice to meet you
NEF
puts string1
string2=%q{
welcome to use ruby language
nice to meet you
}
puts string2
string3=%q!
welcome to use ruby language
nice to meet you
!
puts string3
welcome to use ruby language
nice to meet you
NEF
puts string1
string2=%q{
welcome to use ruby language
nice to meet you
}
puts string2
string3=%q!
welcome to use ruby language
nice to meet you
!
puts string3
loop syntax
syntax 1:
4.times {
puts "welcome to use ruby language"
}
syntax 2:
1.upto(5){
puts "nice to meet you"
}
10.downto(5){
puts "nice to meet you"
}
0.step(50,5){
puts "welcome to use ruby language"
}
1.upto(5) do |number|
puts number
end
4.times {
puts "welcome to use ruby language"
}
syntax 2:
1.upto(5){
puts "nice to meet you"
}
10.downto(5){
puts "nice to meet you"
}
0.step(50,5){
puts "welcome to use ruby language"
}
1.upto(5) do |number|
puts number
end
if-else syntax
age=15
color="blue"
if age > 10 && age <25
puts "you are very young"
else
puts "you are not young"
end
if color=="blue" || color!="red"
puts "cold color"
else
puts "warm color"
end
color="blue"
if age > 10 && age <25
puts "you are very young"
else
puts "you are not young"
end
if color=="blue" || color!="red"
puts "cold color"
else
puts "warm color"
end
class string method
string1="Test"
puts string1.upcase
puts string1.downcase
puts string1.capitalize
puts string1.reverse
puts string1.chop
puts string1.swapcase
puts string1.hash
puts string1.sum
puts string1.next
puts string1.class
puts string1.length
puts string1.slice(2,2)
puts string1.upcase
puts string1.downcase
puts string1.capitalize
puts string1.reverse
puts string1.chop
puts string1.swapcase
puts string1.hash
puts string1.sum
puts string1.next
puts string1.class
puts string1.length
puts string1.slice(2,2)
define a function
class Cat
attr_accessor:name, :size
def bark
puts "bark!!!"
end
end
cat1=Cat.new
cat1.name="putty"
cat1.size=20
puts cat1.name
puts cat1.size
cat1.bark
attr_accessor:name, :size
def bark
puts "bark!!!"
end
end
cat1=Cat.new
cat1.name="putty"
cat1.size=20
puts cat1.name
puts cat1.size
cat1.bark
define a class inherit another class
class Person
attr_accessor:name, :age, :email
end
class Cat < Person
attr_accessor:eye
end
pson1=Person.new
pson1.name="Tommy"
pson1.age=22
pson1.email="tommy@gmail.com"
cat1=Cat.new
cat1.name="putty"
cat1.age=5
cat1.eye="black"
puts cat1.name
puts cat1.age
puts cat1.eye
attr_accessor:name, :age, :email
end
class Cat < Person
attr_accessor:eye
end
pson1=Person.new
pson1.name="Tommy"
pson1.age=22
pson1.email="tommy@gmail.com"
cat1=Cat.new
cat1.name="putty"
cat1.age=5
cat1.eye="black"
puts cat1.name
puts cat1.age
puts cat1.eye
Define a class
class Person
attr_accessor:name, :age, :email
end
pson1=Person.new
pson1.name="Tommy"
pson1.age=22
pson1.email="tommy@gmail.com"
puts pson1.name
puts pson1.age
puts pson1.email
attr_accessor:name, :age, :email
end
pson1=Person.new
pson1.name="Tommy"
pson1.age=22
pson1.email="tommy@gmail.com"
puts pson1.name
puts pson1.age
puts pson1.email
訂閱:
意見 (Atom)