4

I really have tried researching this problem, but I'm now getting so close to it I fear I won't find the solution without asking for help. I'm going through RubyMonk and one of the exercises has me completely stumped.

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

You can see in the comments what the code is asking for; take the names in the heroes variable and combine them into a single name. I've tried testing some puts and am unable to get anything in the STDOUT besides "#" or "nil" so clearly I am not working with it correctly.

The requirements for the goal say not to use .map or .collect but I think you actually are supposed to because it fails a requirement if you don't use .map or .collect.

Ideas?

lyonsinbeta
  • 919
  • 6
  • 25

3 Answers3

5
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
evfwcqcg
  • 15,755
  • 15
  • 55
  • 72
  • This is it! Can someone point me to some documentation about how the initialization method works; specifically the instance variable (which is apparently assigned automatically because it's a custom class) and the use of the *names in the arguments? – lyonsinbeta Nov 17 '11 at 15:03
  • @thekungfuman `initialize` method is constructor. (invokes every time when object is being created). `*names` is a shortcut for passing array in method, here is article about it http://kconrails.com/2010/12/22/rubys-splat-operator/ – evfwcqcg Nov 17 '11 at 15:15
  • @thekungfuman There is also useful information about initialization method and instance variables http://www.rubycentral.com/pickaxe/tut_classes.html – evfwcqcg Nov 17 '11 at 15:24
  • @thekungfuman Perhaps the section on constructing and initializing objects would help? http://rubymonk.com/chapters/7-classes-and-object-oriented-programming-in-ruby/lessons/40-building-your-own-class#magic_block-3034 – Kai Wren Nov 18 '11 at 07:11
  • This syntax `heroes.map(&:full_name)` unfortunately has not been discussed yet in the book and it jumps pas it. :( – Senthil Kumaran Jul 18 '12 at 18:56
1

I solved this nearly the same way as the post above, but my .map line looks like this:

heroes.map { |hero| hero.full_name }

This is more in line with the instructions for the problem.

I must say I was confused for a while and the feedback was part of the confusion (still is). Initially I used a .each method and the feedback failed me for using .map or .collect, so I guess .each equates to .map?

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

does not include a call to 'map' or 'collect' ✔

I get the code, but not the comment.

Bosky
  • 11
  • 2
  • Yes, I believe the feedback is wrong. It appears to contradict the instructions. And no, #map and #each are quite different. #map returns a new array and #each just iterates (potentially returning a new array, but it's not part of the method, it would be part of the bloc.) See [#each](http://ruby-doc.org/core-1.9.2/Array.html#method-i-each) and [#map](http://ruby-doc.org/core-1.9.2/Array.html#method-i-map) – lyonsinbeta Apr 18 '12 at 03:15
  • Oh, and the **&:**, which I'm assuming is the part you think looks different, is a symbol. It's shorthand for methods that enumerate over a collection. It's discussed in [this question](http://stackoverflow.com/questions/1217088/what-does-mapname-mean-in-ruby) for example. – lyonsinbeta Apr 18 '12 at 03:18
1

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

You can call the Hero object's full_name method in the map. But you'll have to write the code for the full_name method first. The hint says you can use Array#join for this.

Hope it is clear now!

Jasim
  • 490
  • 6
  • 14
  • A previous answer (which got deleted by the author I guess?) set me on the right path by showing the right way to use the instance variable. I think I might be able to get it now. – lyonsinbeta Nov 17 '11 at 14:58