puts "hello word"
=begin
test about string
=end
puts "----------------------------------------"
a="hello"
b="ruby"
c=:"we are going to say #{a} to #{b}"
puts c.class
puts c
puts "#{c}" *3
puts c[16]
puts c[16,10]
puts c[-4,4]
puts "#{c}"=="we are going to say #{a} to #{b}"
puts %("hello ruby")
puts %("")
=begin
test for array
=end
puts "----------------------------------------"
d=["we","are"]
e=d+["going","to"]
puts e[1]
print e[1,2]
puts "\n#{e[1,2].class}"
print e[0..4]
f=e.join(",")
puts "\n"+f
g=f.split(",")
print "#{g}\n"
puts g==e
array_test=%w{aaa bbb ccc}
puts array_test.class
print array_test
puts "\n"
print array_test.reverse
puts "\n"
test_array=[]
test_array=test_array+['q']
test_array=test_array+['p']
print test_array
=begin
test for hash
可以把哈希表看成一个由大括号{}中所包含的多个键值对构成的相关联的数组
=end
puts "\n----------------------------------------"
h={"one"=>"we","two"=>"are","three"=>"going","four"=>"to","five"=>"say"}
puts h["three"]
h["six"]="hello"
print "#{h}\n"
h.delete("six")
print "#{h}\n"
i={:a=>"we",:b=>"are",:c=>"going",:d=>"to",:e=>"say"}
print "#{i}\n"
=begin
test for rang
=end
puts "----------------------------------------"
j='a'..'f'
puts j.class
puts j === 'x'
puts j === 'b'
=begin
test about variables
=end
puts "----------------------------------------"
k="hello"
puts defined?(k)
class TestHello
def test_a
l="hello"
puts "test_a #{defined?(l)}"
end
def test_b
puts "test_b #{defined?(l)}"
end
end
m=TestHello.new
m.test_a
m.test_b
class TestHello1
def test_a
@l="hello"
puts "test_a #{defined?(@l)}"
end
def test_b
puts "test_b #{defined?(@l)}"
end
end
n=TestHello1.new
n.test_a
n.test_b
@@class_test="ruby"
class C_class
def test
puts @@class_test
end
def test_set(a)
@@class_test=a
end
end
c_a=C_class.new
c_b=C_class.new
c_a.test
c_b.test
c_a.test_set("fffffuck")
c_b.test
=begin
test about operator
=end
var = nil
var ||= "fuck"
puts var
var ||= "test"
puts var
运行结果
hello word
----------------------------------------
Symbol
we are going to say hello to ruby
we are going to say hello to rubywe are going to say hello to rubywe are going to say hello to ruby
s
say hello
ruby
true
"hello ruby"
""
----------------------------------------
are
["are", "going"]
Array
["we", "are", "going", "to"]
we,are,going,to
["we", "are", "going", "to"]
true
Array
["aaa", "bbb", "ccc"]
["ccc", "bbb", "aaa"]
["q", "p"]
----------------------------------------
going
{"one"=>"we", "two"=>"are", "three"=>"going", "four"=>"to", "five"=>"say", "six"=>"hello"}
{"one"=>"we", "two"=>"are", "three"=>"going", "four"=>"to", "five"=>"say"}
{:a=>"we", :b=>"are", :c=>"going", :d=>"to", :e=>"say"}
----------------------------------------
Range
false
true
----------------------------------------
local-variable
test_a local-variable
test_b
test_a instance-variable
test_b instance-variable
test_1.rb:124: warning: class variable access from toplevel
ruby
ruby
fffffuck
fuck
fuck