Merging Elements (RubyMonk Chapter 6 Lesson 1)

I really tried to investigate this problem, but now I am so close to it that I can’t find a solution without asking for help. I walk through RubyMonk, and one of the exercises completely surpassed me.

class Hero def initialize(*names) @names = names end def full_name # a hero class allows us to easily combine an arbitrary number of names end end def names heroes = [Hero.new("Christopher", "Alexander"), Hero.new("John", "McCarthy"), Hero.new("Emperor", "Joshua", "Abraham", "Norton")] # map over heroes using your new powers! end 

In the comments that the code asks for, you can see; take the names in the variable of the heroes and combine them into one name. I tried to test some puts and I can’t get anything in STDOUT except β€œ#” or β€œnil”, so I don’t work with it clearly.

Requirements for the goal say not to use .map or .collect, but I think that you really should, because it does not work out the requirement if you do not use .map or .collect.

Ideas?

+4
source share
3 answers
 class Hero def initialize(*names) @names = names end def full_name @names.join(' ') end end def names heroes = [Hero.new("Christopher", "Alexander"), Hero.new("John", "McCarthy"), Hero.new("Emperor", "Joshua", "Abraham", "Norton")] heroes.map(&:full_name) end 
+5
source

The question is actually asking you to use the map method. Here is the link for the link: http://rubymonk.com/chapters/9-more-ruby/lessons/42-functional-programming-in-ruby

You can call the full_name method of the Hero object on the map. But first you need to write code for the full_name method. The hint says you can use Array # to do this.

Hope this is clear now!

+1
source

I solved this almost the same as the message above, but my .map line looks like this:

 heroes.map { |hero| hero.full_name } 

This is more consistent with the instructions for the problem.

I have to say that I was a little embarrassed, and the feedback was part of the confusion (as before). I originally used the .each method, and the feedback didn't help me using .map or .collect, so I think .each is equal to .map?

I finally used .map, and the feedback passed my code with a comment:

does not include calling "map" or "collect" βœ”

I get the code, but not the comment.

+1
source

Source: https://habr.com/ru/post/1381716/


All Articles