2017-04-23 1 views
1

Je me suis heurté à un problème de forage d'étude, et je ne pouvais pas le comprendre. Voici le lien vers l'exercice. https://learnrubythehardway.org/book/ex40.htmlPassage Ruby Hash dans les classes

Ci-dessous, vous trouverez mon travail. Sur Study Drill 2, j'ai passé en variables et cela a fonctionné. Cependant, à l'exercice d'étude 3, j'ai cassé mon code. J'ai réalisé que je ne passais pas en variable, mais un hachage. Et parce que ma classe prend 2 arguments, je ne pouvais pas comprendre comment passer un dictionnaire en 2 arguments.

class Song 

    def initialize(lyrics, singer) 
    @lyrics = lyrics 
    @singer = singer 
    end 

    def sing_along() 
    @lyrics.each {|line| puts line} 
    end 

    def singer_name() 
    puts "The song is composed by #{@singer}" 
    end 

    def line_reader(lineNum) 
    line = @lyrics[lineNum-1] 
    puts "The lyrics line #{lineNum} is \"#{line}\"." 
    end 

end 

# The lyrics are arrays, so they have [] brackets 
practiceSing = Song.new(["This is line 1", 
       "This is line 2", 
       "This is line 3"],"PracticeBand") 

practiceSing.sing_along() 
practiceSing.singer_name() 
practiceSing.line_reader(3) 

puts "." * 20 
puts "\n" 

# Variable for passing. Working on dictionary to pass the singer value. 
lovingThis = {["Don't know if I'm right", 
       "but let's see if this works", 
       "I hope it does"] => 'TestingBand'} 

# Everything after this line is somewhat bugged 
# Because I was using a variable as an argument 
# I couldn't figure out how to use dictionary or function to work with 
this 

practiceVariable = Song.new(lovingThis,lovingThis) 

practiceVariable.sing_along() 
practiceVariable.singer_name() 
practiceVariable.line_reader(3) 

Voici le Output. Ce qu'il faut faire, c'est renvoyer le chanteur/groupe, et renvoyer la ligne des paroles demandées.

Je suis novice en matière de codage, veuillez indiquer comment transmettre les hachages aux classes. Comment faire pour passer lovingThis hash dans Song.new() et lu comme 2 arguments?

+0

lyrics Ivar devrait être un tableau? Si c'est le cas, vous pouvez créer une variable de hachage où la clé est un 'nom_singer' et une valeur est un tableau des lignes de paroles. En passant en classe, utilisez: Song.new (some_hash ['singer_name'], some_hash.keys.first). – Anton

Répondre

1

vous pouvez passer hachage au constructeur de la classe de la même manière que nous passons toute autre variable, mais pour que vous devez changer votre définition de constructeur de prendre nombre variable d'arguments à savoir def initialize(*args)

class Song 
    def initialize(*args) 
    if args[0].instance_of? Hash 
     @lyrics = args[0].keys.first 
     @singer = args[0].values.first 
    else 
     @lyrics = args[0] 
     @singer = args[1] 
    end 
    end 

    def sing_along() 
    @lyrics.each {|line| puts line} 
    end 

    def singer_name() 
    puts "The song is composed by #{@singer}" 
    end 

    def line_reader(lineNum) 
    line = @lyrics[lineNum-1] 
    puts "The lyrics line #{lineNum} is \"#{line}\"." 
    end 
end 

# The lyrics are arrays, so they have [] brackets 
practiceSing = Song.new(["This is line 1", 
       "This is line 2", 
       "This is line 3"],"PracticeBand") 

practiceSing.sing_along() 
practiceSing.singer_name() 
practiceSing.line_reader(3) 

puts "." * 20 
puts "\n" 

# Variable for passing. Working on dictionary to pass the singer value. 
lovingThis = {["Don't know if I'm right", 
       "but let's see if this works", 
       "I hope it does"] => 'TestingBand'} 

practiceVariable = Song.new(lovingThis) 

practiceVariable.sing_along() 
practiceVariable.singer_name() 
practiceVariable.line_reader(3) 
+0

Merci beaucoup, je n'ai jamais pensé à changer le nombre de arguments acceptés. J'ai essayé d'obtenir 2 arguments en changeant le hachage, ou en créant des fonctions qui retournent 2 arguments. Cela fonctionne parfaitement! – Joey