ruby语法课1

来源:互联网 发布:DRGS网络上报 编辑:程序博客网 时间:2024/04/28 06:55
puts "hello word"=begintest about string=endputs "----------------------------------------"a="hello"b="ruby"c=:"we are going to say #{a} to #{b}"puts c.classputs cputs "#{c}" *3puts c[16]puts c[16,10]puts c[-4,4]puts "#{c}"=="we are going to say #{a} to #{b}"puts %("hello ruby")puts %("")=begintest for array=endputs "----------------------------------------"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"+fg=f.split(",")print "#{g}\n"puts g==earray_test=%w{aaa bbb ccc}puts array_test.classprint array_testputs "\n"print array_test.reverseputs "\n"test_array=[]test_array=test_array+['q']test_array=test_array+['p']print test_array=begintest for hash可以把哈希表看成一个由大括号{}中所包含的多个键值对构成的相关联的数组=endputs "\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"=begintest for rang=endputs "----------------------------------------"j='a'..'f'puts j.classputs j === 'x'puts j === 'b'=begintest about variables=endputs "----------------------------------------"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)}"  endendm=TestHello.newm.test_am.test_bclass TestHello1  def test_a    @l="hello"    puts "test_a #{defined?(@l)}"  end  def test_b    puts "test_b #{defined?(@l)}"  endendn=TestHello1.newn.test_an.test_b@@class_test="ruby"class C_class  def test    puts @@class_test  end  def test_set(a)    @@class_test=a  endendc_a=C_class.newc_b=C_class.newc_a.testc_b.testc_a.test_set("fffffuck")c_b.test=begintest about operator=endvar = nilvar ||= "fuck"puts varvar ||= "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

0 0
原创粉丝点击