2014-06-10 19 views
-1

我有一個類,它的方法產生一個哈希輸出。我的問題是我怎樣才能把這個產生的散列發送到另一個類,有進一步處理這個散列數據的方法?Ruby - 將一個類的方法創建的哈希數據發送到另一個類的方法,以進一步處理這個哈希數據

我讀過紅寶石文檔並在StackOverflow上搜索,但似乎無法弄清楚如何讓這個新類接收這些數據(來自原始類)。 當第二課時嘗試調用第一個類的方法時,出現「未定義的局部變量或方法」錯誤。

感謝您的幫助。 希望我在我的問題上提供了足夠的背景知識,有人可以提供一些指導。

編輯 - 這裏是我有的代碼,它產生了上述的散列。實際上在這個階段它是一個數組。 理想情況下,我希望將build_list方法中的所有代碼放在一個完全獨立的類中。這樣做需要我將生成的數組數據(來自用戶輸入)傳遞給這個新類中的其他方法。在這個新班級中,我希望打印完成的播放列表。所以我想將這個示例代碼吐在兩個類中,第二個類對用戶提供的藝術家進行所有處理工作。我希望這更清楚。

require 'lib/uri.rb' 
require 'json' 
require 'rest_client' 
require 'colorize' 

class Playlist 
    attr_accessor :artistInput, :artist_list 

    def initialize 
    @artistInput = artistInput 
    @artist_list = [] 
    @artist = @artist 


    end 

    def self.start #welcome to the application + rules 
    puts "-------------------------------------------------------------------------------------------------------------------" 
    puts "|This Playlist Creator application will create you a static playlist from a given list of artists supplied by you.|".colorize(:color => :white, :background => :black) 
    puts "-------------------------------------------------------------------------------------------------------------------" 
    puts "Hint: To stop adding artists, type '\N\' + the [enter] key at any time." 
    # puts "Hint: For artists with spaces, eg \'Lady Gaga\' - please remove the spaces and write as such \'LadyGaga\'." 
    end 
system "clear" 
    def artistInput #loop that creates an artist_list array of an infinate amount of artists, stops at the keyword 'N' 
    @artist_list = [] 

    while @artist != "" 
     puts "\nWhat artist would you like to add to the Playlist?" 
     @artist = gets.gsub(/\s+/, "") 
     if @artist != 'N' 
     puts "You have chosen to add #{@artist} to the Playlist." 
     end 
     break if @artist =~ /N/ 
     @artist_list << @artist.gsub(/\s+/, "") #.gsub removes spaces. 'Lady Gaga' => 'LadyGaga' 
    end 

    def build_list 
    #@artist_list 
    list = @artist_list 
    list.join('&artist=') 
    end 

    def build_url_string 
    string = build_list 
    url = 'http://developer.echonest.com/api/v4/playlist/static?api_key=G3ABIRIXCOIGGCCRQ&artist=' + string 
    end 
system "clear" 
    def parse_url 
    url = build_url_string 
    response = JSON.parse(RestClient.get url) 

    song = response['response']['songs'].each do |song| 
    song.delete("artist_id") 
    song.delete("id") 
    puts 
    song.each {|key, value| puts "#{key.colorize(:white)}: \'#{value.colorize(:color => :white, :background => :black)}\'" } 
    end 
    # a = response['response']['songs'] 
    # a.fetch('id') 
    end 



    a = parse_url 
puts "\nThere are #{a.length} songs in this Playlist\n\n" 




#Uncomment this section to see the raw data feed 
# puts "////////////////raw data hash feed//////////////" 
# puts "#{a.to_s}" 

    end 


end 
+0

將它作爲參數傳遞給你正在調用的函數。如果這不足以解決您的問題,請張貼一些代碼;你真的沒有給我們任何工作。 – meagar

+0

謝謝你和@Vimsha。是的,我確實需要提供一個實際的例子。我編輯我的原始帖子與我需要一些工作的當前課程。感謝您的輸入! – eaqa

回答

0

如果沒有代碼示例,它會很難回答你的問題。但我會盡力用這個例子

class A 
    def get_hash 
    {"A"=>"1", "B"=>"2"} 
    end 
end 

class B 
    def process_hash(hash) 
    #do something with the hash 
    end 
end 

hash = A.new.get_hash 
B.new.process_hash(hash) 
相關問題