1

My first ever stack overflow post. I am supposed to be succinct and to the point, but please indulge me in some background the first time around. I have been programming in C and Visual Basic for 16 years, but only part time to support my work as a scientist. Started learning ruby about a year ago and really enjoy it. I am writing a program that processes data. This works with folders and files rather than a database. Every time I get stuck, I can always find a solution here (what a great resource!) or on Google. This time is different. So to the point:

I want to use the highline gem in ruby to display a menu with a variable number of menu items. Essentially I want the user to select a directory. There could be any number of directories in the parent directory. My code is as follows:

@allArchiveDirs =  Dir.entries(@dirArchive).select {|entry| File.directory? File.join(@dirArchive,entry) and !(entry =='.' || entry == '..') }
p @allArchiveDirs

choose do |menu|
  menu.prompt = 'Please choose a project to access:'
  temp = @allArchiveDirs.map &:to_sym
  # todo here. Cannot get temp into correct format for choices call.
  temp = temp.map{|x| x.inspect}.join(', ')
  p temp
  menu.choices(:old1, :old5) do |chosen|
    puts "Item chosen: #{chosen}"
  end
end

In parenthesis for the menu.choices call I would normally have temp, but currently I have :old1, :old5 just for my own debugging purposes or I get an error.

The output is:

["deletemetest", "old2", "old3", "old4", "TestData", "testy123", "tsty"]

":deletemetest, :old2, :old3, :old4, :TestData, :testy123, :tsty"

  1. old1
  2. old5

Please choose a project to access:

So the symbols seem to be a string (on account of the quotes around it). If I can remove these I may succeed in getting temp into the correct format for the menu.choice call.

Juz0
  • 13
  • 2
  • can you give us a sample output of `@allArchiveDirs` ? – Anthony Jul 26 '14 at 01:20
  • I have a line in the code p @allArchiveDirs. Thus the output is in the post. It is the first line after "The output is:". It is an array of strings which I later map to symbols to create a single string. – Juz0 Jul 26 '14 at 01:23

1 Answers1

3

You actually don't need to do all this type conversion, all you need is the splat operator

@allArchiveDirs = ["deletemetest", "old2", "old3", "old4", "TestData", "testy123", "tsty"]
choose do |menu|
  menu.prompt = 'Please choose a project to access:'
   menu.choices(*@allArchiveDirs) do |chosen|
    puts "Item chosen: #{chosen}"
  end
end

Output:

1. deletemetest
2. old2
3. old3
4. old4
5. TestData
6. testy123
7. tsty
Please choose a project to access:
Anthony
  • 15,435
  • 4
  • 39
  • 69
  • Works great and all I needed was to splat one variable. I will read up on what splat does now. Seems to allow variable number of arguments, but will look up more examples online now to understand how this works. As you indicate all those other lines of type conversion are now unnecessary. Thank you Anthony. – Juz0 Jul 26 '14 at 04:52
  • This works, but I don't really understand how. Arguments for menu.choices in highline gem have to be :symbols. Splatting seems to make a list of strings with carriage returns after each one. No symbols to be seen.... and yet it works.... – Juz0 Jul 26 '14 at 05:08
  • The splat operator unpacks an array passed to a function so that each element is sent to the function as an individual parameter. – Anthony Jul 26 '14 at 11:57