ruby语法课1

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

补充1:Hash更加一般的写法

{'a':'b'}.to_s
=> "{:a=>\"b\"}"
{'a':'b'}['a']
=> nil
{'a':'b'}[:a]
=> "b"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值