0

How would you solve this problem in Ruby. "Define a method called yeller that takes in an array of characters and returns a string with an ALLCAPS version of the input. Verify that yeller([’o’, ’l’, ’d’]) returns "OLD". Combine the .join, .map, .upcase methods."

So far I have:

def yeller(x) 
  x.map do |y|  
  y.upcase.join     
    puts y
  end
end
yeller(['o', 'l', 'd'])
1
  • 1
    Another note, in ruby you can also do %w(o l d) instead of ['o', 'l', 'd']. Commented Nov 11, 2016 at 21:02

3 Answers 3

7

It's so simple

def yeller(x)
  x.join.upcase
end

yeller(['o', 'l', 'd'])
 => "OLD" 

join makes your character list a string and upcase makes that string uppercase

Sign up to request clarification or add additional context in comments.

Comments

1

This is the answer from the textbook where you got the question from.

def yeller(chars)
  chars.map(&:upcase).join
end
yeller(['o', 'l', 'd'])
=> "OLD"

Comments

-1

try this

def yeller(x)
  imsupercool = x.map do |y|
    y.upcase
  end
   imsupercool.join
end
puts yeller(['o', 'l', 'd'])

2 Comments

"Define a method called yeller that takes in an array of characters and returns a string(...)" . This returns nil (because puts returns nil).
remove the puts from the method definition and use puts before calling the function i.e. puts yeller(['o','l','d'])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.