0

In Ruby I'm looking to create a short program where a user is able to enter a certain number of words (5 for now) and the program needs to spit the words back out to the user in alphabetical order, and each word must alternate from ALL CAPS to all lowercase letters.

This program can only use methods like (.each do, .sort, .upcase, .downcase), arrays, loops, and if/else statements. NOTHING ELSE ... so no indexes, dictionaries, modules, arguments, etc. The syntax shouldn't diverge too much from what I've already written.

This is the code I have so far ... I keep getting a no method error ... any help would be greatly appreciated.

words = []

5.times do
  puts "Please enter a word"
  words << gets.chomp
end

words.sort.each do |odd, even|
    if odd puts words.upcase
    elsif even puts words.downcase
  end
end
2
  • 1
    What do you mean by "methods like"? Whenever an exception is raised it is important to identify the line where it occurs. Commented Sep 4, 2015 at 21:54
  • 1
    words.sort will give you and array of sorted words. each then passes each element of the sorted array to the block, assigning them to the first block variable, odd. Since a single string is passed each time, the block variable even will always equal nil. That's why the block is not performing as you would like. You might find it helpful to add the following at the beginning of the block: puts "odd=#{odd}, even=#{even}". Commented Sep 4, 2015 at 22:00

3 Answers 3

1

The first loop, filling the array, is fine.

words.sort.each do |odd, even|
    if odd puts words.upcase
    elsif even puts words.downcase
  end
end

This does not work. each takes one word from the sorted array and "feeds" it to the block. The block uses the variable "odd" for it. Since the variable "even" did not get a value, it will be nil.

In the next line Ruby decides if odd is true, and since it is not falseor nil, Ruby decides it is, so puts words.upcase is executed. words however, being an Array, does not know about upcasing things - and tells you so by giving an error.

The following code uses a variable as a "toggle"; it is switched on if it's off and vice versa.

words = []

5.times do
  puts "Please enter a word"
  words << gets.chomp
end

up = true # the toggle

words.sort.each do |word|
  if up then 
    puts word.upcase
    up = false
  else 
    puts word.downcase
    up = true
  end 
end
Sign up to request clarification or add additional context in comments.

Comments

0

Edit: I had not seen the post by @steenslag when I posted this answer, but it is slightly different in the way the input is generated so I will leave it unless it is desired that I remove it, in which case I will.

puts "Enter 5 words separated by spaces: "
words = gets.chomp! 
results = words.split(" ").sort!
caps = true
results.each do |word|
  if caps == true
    puts word.upcase
    caps = false
  elsif caps == false
    puts word.downcase
    caps = true
  end
end

Comments

0

I agree that the idea with the toggle is the way to go as other anwers already suggest.

I would just simplify the block a bit, because in Ruby is no need to check the toggle explicitly against true or false nor you need change the toggle explicitly to true or false in each iteration, just toggle it:

upcase = true
words.sort.each do |word|
  puts(upcase ? word.upcase : word.downcase)
  upcase = !upcase
end 

Comments

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.