Ruby 语法学习片段 - 参考 《Programming Ruby 第二版》

a = [1,'act',"3.14"]

puts a[1]


#

sec = { 'a' =>'zhang','b'=>"wang"}
puts sec['b']


#


class Song
  def initialize(name,artist)
    @name = name
    @artist = artist
   
  end
 
 
 
end

Song.new("a","b")


#


class Song
  @@stav = 0
  def initialize(name,artist)
    @name = name
    @artist = artist
    abc = name + artist + "  ennnnd" + @@stav
    puts(abc)
  end
 
  def put
 
    puts(@name+@artist)
  end
 
 
end


class SubSong < Song
  def a
  end
 
end


song = Song.new("a","b")


#


class Song
  @@stav = 0
  #@stav=0
  def initialize(name,artist)
    @@stav+=1
    @stav=0
    @stav+=1
    @name = name
    @artist = artist
    abc =@@stav
    puts(abc)
    puts(@stav)
  end
 
 
end



song1= Song.new("a","b")
song2= Song.new("c","d")


#


class AAA
 
    def a
       times = 0
    6.times do
     puts times+=1
    end
  end


end

AAA.new.a


#


def x
['cat','dog','horse'].each { |name| print name," "}

('a'..'e').each{|char| print char}
end


x


#


def x
  return "a" "b"
end

x.each{|name|puts name}


#


for x in ['a','b','c']
  puts x
end

 ['a','b','c'].each do|x|
   puts x
 end
 


#


module Test
class X
  def sub
    begin

    for x in ['a','b','c']
      puts x
      raise RetryException.new(true),"errrrror"
    end
    
     rescue
      puts "error occur"

     end
  
 end
 end
 
 end
 
 class ABC
  include Test
 
 end
 
 ABC.new.Xsub
 


#


puts [1,2,3,4,5].inject{|v,n|  v+n}


#


load 'main/x.rb'

puts [1,2,3,4,5].inject{|v,n|  v+n}

#

file = File.new("main/x.rb","r")

file.close

File.open("main/x.rb","r") do|file|

end

#

File.open("main/test.rb","r") do|file|
  
  while line = file.gets
    puts line
  end
  

end

#

# ruby -r debug test.rb
a =1
puts 'a'

a = 2
puts 'ab'

a = 3
puts 'abc'

#


class Test

  def begin
    @v1 = 1
    puts @v1
  end

  def ending
    puts @v1
  end


  end

Test.new.begin
Test.new.ending

# @v1 作用域 只在方法内
#
 

你可能感兴趣的:(C++,c,C#,Ruby)