0

I am trying to create a program where by the user can enter multiple names. those names are then displayed under each other in alphabetical order, and print(display) every second name backwards. i have gone through several tutorials this is my second day using ruby.. here is what i have so far.

name_list = {}
puts 'please enter names seperated by a space:'
name_list = gets.chomp
names = name_list.split(" ")

to grab names...

names.sort do |a,b| a.upcase <=> b.upcase end
display = "#{names}"
for ss in 0...display.length
       print ss, ": ", display[ss], "\n"
end

to arrange them alphabetically and under each other. i am really struggling to mesh it all together i think i have got at least half a dozen errors in here...if i am on the wrong path could someone guide me to some info so i can start again??

EDIT

i also had this idea of using a class. but i would have to program the names in i wanted the user to be able to add info via the consol. class A

def initialize(name) @name = name end def to_s @name.reverse end end

>> a = [A.new("greg"),A.new("pete"),A.new("paul")]

>> puts a
1
  • What are you trying to achieve with the last loop? What is the expected output? Commented Oct 12, 2011 at 7:57

3 Answers 3

3

Problems in your code:

  • name_list defined as an empty hash at the top but not used.
  • split(" ") -> split
  • sort { |a, b| a.method <=> b.method } -> sort_by { |x| x.method } -> sort_by(&:method)
  • sort is not an in-place operation, assign the result (or directly use it).
  • display = "#{names}" -> display = names
  • for ss in 0...display.length -> enumerable.each_with_index { |item, index| ... }
  • don't write do/end in one-liners, use { ... }

I'd write:

puts 'Please enter names separated by spaces'
gets.split.sort_by(&:upcase).each_with_index do |name, index|
  puts "%s: %s" % [index, (index % 2).zero? ? name : name.reverse]
end
Sign up to request clarification or add additional context in comments.

1 Comment

thanks man i thought i had a few problems!! yeah... i started using ruby just yesterday. the syntax is so simple is confusing. i am used to a lot of uniform procedures... you rock my socks..
2

A few pointers then:

names.sort do |a,b| a.upcase <=> b.upcase end # Will not modify the "names" array, but will return a sorted array.
names.sort! do |a,b| a.upcase <=> b.upcase end # Will modify the "names" array.

To display your names:

names.each_with_index do |name, index|
    if index % 2 == 0
        puts name
    else
        puts name.reverse
    end
end

1 Comment

so if i wanted to print them under eachother would i add /n at the end or the print name??
1
puts 'please enter names seperated by a space`enter code here` :'
names = gets.chomp.split(" ")

names.sort! {|a,b| a.upcase <=> b.upcase }  # For a single line use {..} instead of do..end

names.each_with_index do |n,i|
  if i % 2 == 0
    p n
  else
    p n.reverse
  end
end

You can also use a ternary operator, I used the full if else block for readability in this case.

names.each_with_index do |n,i|
  p (i % 2 == 0) ? n : n.reverse
end

EDIT

command = ""
names = []
while command != "exit"
  puts 'please enter names seperated by a space`enter code here` :'

  command = gets.chomp!

  if command == "display"
    names.sort! {|a,b| a.upcase <=> b.upcase }  # For a single line use {..} instead of do..end        
    names.each_with_index do |n,i|
      if i % 2 == 0
        p n
      else
        p n.reverse
      end
    end
  else
    names << command
  end
end

4 Comments

I'd generally avoid the ternary operator because it impairs readability of the code. In this case however, it's an OK-ish situation to use it. Also, OP sorts in a case-insensitive manner, you should do the same.
thanks man.. would it be possible to put in a trigger say he could enter as many names as he wants but when he types "display" for instance then it will display the names.. by the way you rock my socks. i have used my google-fu and couldnt get anywhere :)
@Romain, well spotted. I have changed that.
@Sliknox I have updated my code to do this quickly before I head to work. It should do the trick though.

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.