首页 新闻 搜索 专区 学院

关于ruby的语法问题

0
[已解决问题] 解决于 2015-11-18 14:27

大家好,我最近刚接触到ruby,因为要用到,就学习了。中间遇到了很多问题,其中下面的这个就是比较头疼的一个,可能对于熟悉的人来说不算什么。 谁帮我看看? 代码如下,最后编译执行的时候,第129行出现了错误:syntax error,unexpected $end,expecting kEND

 

 

  1 require 'yaml'
  2 
  3 $cd_arr = Array.new
  4 
  5 $fn = 'cd_db.yml'
  6 
  7 class CD
  8     def initialize(arr)
  9         @name = arr[0]
 10         @artist = arr[1]
 11         @numtracks = arr[2]
 12     end
 13 
 14     def getdetails
 15         return [@name,@artist,@numtracks]
 16     end
 17 end
 18 
 19 
 20 # PopCD is a child class of CD.
 21 class PopCD < CD
 22 
 23     def initialize(arr)
 24         super(arr)
 25         @genre =arr[3]
 26     
 27 
 28         def getdetails
 29         return (super << @genre)
 30     end
 31 end
 32 
 33 class ClassicalCD < CD
 34     def initialize(arr)
 35         super(arr)
 36         @conductor = arr[3]
 37         @composer = arr[4]
 38     end
 39 
 40     def getdetails
 41         return (super << @conductor << @composer)
 42     end
 43 end
 44 
 45 #some methods to get data from the user
 46 
 47 def otherCD
 48     print("Enter CD name: ")
 49     cd_name = gets().chomp()
 50     print("Enter artist's name: ")
 51     a_name = gets().chomp()
 52     print("Enter number of tracks: ")
 53     num_tracks = gets().chomp().to_i
 54     return [cd_name,a_name,num_tracks]
 55 end
 56 
 57 def classicCD
 58     cdInfo = otherCD
 59     print("Enter conductor's name: ")
 60 
 61     con_name=gets().chomp()
 62     print("Enter composer's name: ")
 63     comp_name = gets().chomp()
 64     cdInfo << con_name << comp_name
 65     return cdInfo
 66 end
 67 
 68 def popCD
 69     cdInfo = otherCD
 70     print("Enter genre of music: ")
 71     genre = gets().chomp()
 72     cdInfo << genre
 73     return cdInfo
 74 end
 75 
 76 #add a CD object to the array variable , $cd_arr
 77 
 78 def addCD(aCD)
 79     $cd_arr << aCD
 80 end
 81 
 82 #save data to disk in YAML format
 83 
 84 def saveDB
 85     File.open($fn,'w'){
 86         |f|
 87         f.write($cd_arr.to_yaml)
 88     }
 89 end
 90 
 91 #loads data from disk and recreates the array of
 92 # cd objects, $cd_arr, from the data
 93 #
 94 def loadDB
 95     input_data = File.read($fn)
 96     $cd_arr = YAML::load(input_data)
 97 end
 98 
 99 #prints the data from the array to screen in human-
100 #readable(YAML) format
101 
102 def showData
103     puts($cd_arr.to_yaml)
104 end
105 
106 #Start of Program
107 
108 if File.exist?($fn) then
109     loadDB
110     showData
111 else
112     puts("The file #{$fn} cannot be found.")
113 end
114 
115 # 'main' loop
116 ans = ''
117 
118 until ans == 'q' do
119     puts("Create (P)op CD (C)lassical CD,(O)ther CD - (S)ave or (Q)uit?")
120     print("> ")
121     ans = gets[0].chr().downcase()
122     case ans
123         when 'p' then addCD(PopCD.new(PopCD()))
124         when 'c' then addCD(ClassicalCD.new(classicCD()))
125         when 'o' then addCD(CD.new(otherCD()))
126         when 's' then saveDB
127     end
128     showData
129 end
夏石的菜园子的主页 夏石的菜园子 | 初学一级 | 园豆:196
提问于:2015-10-13 21:23
< >
分享
最佳答案
0

可能是有些特殊字符导致的原因,现在我重新key一遍代码,问题解决了...( ˇˍˇ )

夏石的菜园子 | 初学一级 |园豆:196 | 2015-10-22 22:03
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册