【问题标题】:Getting 'choice' to work in Highline Ruby Gem without error and getting variable from it让“选择”在 Highline Ruby Gem 中正常工作并从中获取变量
【发布时间】:2010-05-23 14:31:31
【问题描述】:

我在 Ruby 中使用 Highline 时遇到了一些问题,并试图让选择元素 detailed here 工作。

  1. 目前以下代码产生错误 “错误:参数数量错误(0 代表 1)。使用 --trace 查看回溯”
  2. 如何使变量无法选择?目前我有“做”设置,但我不知道如何将用户选择的变量放入变量中以供其他地方使用。

对不起,如果这有点初学者,我对 ruby​​ 是全新的,这是我的第一个项目,在最深处。

提前致谢。

if agree("Are these files going to be part of a set? ") 
      set_title = ask("Title: ")
      set_desc = ask("Description:")
      set_genre = ask("Genre: ")
      set_label = ask("Record Label: ")
      set_date = ask_for_date("Release Date (yy-mm-dd): ")
      set_label = ask("EAN/UPC: ")
      set_buy = ask("Buy this set link: ")
      set_tags = ask_for_array("Tags (seperated by space): ")

      # Sort out license
      choose do |menu|
        menu.prompt = "Please choose the license for this set?  "

        menu.choices(:all_rights_reserved, :cc_by) do 
          # put the stuff in a variable
        end
      end
    end # End setup set

【问题讨论】:

  • 究竟哪一行给你带来了问题? choose 部分在 Ruby 1.8 或 1.9 中运行良好。 ask_for_date 似乎是自定义方法,ask_for_array 也是如此,所以无法帮助您...
  • 这是我的会话记录:Alex-Andrewss-MacBook-Pro:SoundCloud-CLI alex$ ruby​​ sc.rb upload /Users/alex/SoundCloud-CLI 这些文件是否会成为其中的一部分一套? y 标题:你好 描述:你好 类型:你好 唱片标签:你好 发布日期 (yy-mm-dd):09-10-10 EAN/UPC:你好 购买此套装链接:你好 标签(以空格分隔):你好 gello 错误: 参数数量错误(0 代表 1)。使用 --trace 查看回溯非常感谢您迄今为止的帮助。

标签: ruby command-line command-line-interface gem highline


【解决方案1】:

1) 提供的信息不足(见我的评论)

2) 使用块参数:

menu.choices(:all_rights_reserved, :cc_by) do |chosen|
  puts "Item chosen: #{chosen}"
end

您也可以分开选择:

menu.choice(:all_rights_reserved) do
  puts "Chosen: All Rights Reserved"
  #...
end

menu.choice(:cc_by) do
  puts "Chosen: CC by"
  #...
end

【讨论】:

  • 2) 的解决方案效果很好,谢谢。关于 1) 我将以下内容复制到一个新文件中。需要'rubygems' 需要'commander/import' 选择做|menu| menu.prompt = "请选择这个集合的许可证?" menu.choices(:all_rights_reserved, :cc_by) do |chosen| puts "Item selected: #{chosen}" end end 这失败并出现以下错误:scratch.rb:5:in `choose': wrong number of arguments (0 for 1) (ArgumentError) 将行更改为要求 'commander'它可以工作,但其他事情会中断
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-18
  • 2021-06-13
  • 1970-01-01
  • 1970-01-01
  • 2020-11-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多