Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 如何以简单的方式将输入与类连接起来_Ruby_Input - Fatal编程技术网

Ruby 如何以简单的方式将输入与类连接起来

Ruby 如何以简单的方式将输入与类连接起来,ruby,input,Ruby,Input,我正在写一个flash卡CLI游戏 class Intro def self.display while 1 puts "Welcome player! Choose one of the following!" puts "1. Create a new flash card?" puts "2. View all flash cards?" puts "3. Edit a flashcard?" puts "4. Del

我正在写一个flash卡CLI游戏

class Intro
  def self.display
    while 1
      puts "Welcome player! Choose one of the following!"
      puts "1. Create a new flash card?"
      puts "2. View all flash cards?"
      puts "3. Edit a flashcard?"
      puts "4. Delete a flashcard?"
      puts "5. View score and answers?"
      input = gets.chomp.to_i
      if (1..5).include? input
        self.select input
        break
      else
        puts "Invalid option." 
      end
    end
  end
  def self.select number
    @number = number
    puts "#{@number} selected!"
    puts "#{@back}"
  end
end
Intro.display
除非这是唯一的方法,否则在很多答案中始终使用if/else似乎是多余的


如何编写更干净的代码来比较用户的输入和对类的调用?如果有其他方法,我会很感激的。

清洁度有点主观,但这可能是你想要的

class Intro

  def self.display
    input = nil
    until (1..5).include? input
      puts "Welcome player! Choose one of the following!"
      puts "1. Create a new flash card?"
      puts "2. View all flash cards?"
      puts "3. Edit a flashcard?"
      puts "4. Delete a flashcard?"
      puts "5. View score and answers?"
      input = gets.chomp.to_i
      puts "Invalid option." unless (1..5).include? input
    end
    self.select(input)
  end

  def self.select(number)
    @number = number
    puts "#{@number} selected!"
    puts "#{@back}" # @back is uninitialized
  end
end

Intro.display
编辑:

如果您希望能够有多个答案,那么一种方法是创建
Intro
类的实例,每个实例都包含一个包含答案的实例变量

因此,您不需要在类本身上调用
display
,而是在类的实例上调用它

Intro
将具有一个
initialize
函数,该函数在实例化
Intro
类的新实例时自动调用

class Intro

  def initialize(answer)
    @answer = answer
  end

  #[...]
这意味着

new\u game=Intro.new(7)


您正在创建实例变量
@answer
设置为7的
Intro
类的实例,并将该实例存储在变量
new_game
中。从那里,您可以在
new\u game
上调用实例方法

你的问题非常广泛,因为有很多方法可以写。作为第一步,我会:

class Intro
  CHOICES = {
    1 => 'Create a new flash card',
    2 => 'View all flash cards',
    3 => 'Edit a flashcard',
    4 => 'Delete a flashcard',
    5 => 'View score and answers',
  }

  def self.display
    loop do
      puts "Welcome player! Choose one of the following!"
      CHOICES.each do |h|
        puts '%d. %s?' % h
      end

      input = gets.to_i

      return input if CHOICES[input]
      puts 'Invalid option.' 
    end
  end

  def self.select number
    puts "#{number} selected!"
  end
end

choice = Intro.display
Intro.select(choice)
您选择的
gets.chomp.to\u i
最好写为
gets.to\u i

(1..5)。包括?输入
可以,但如果添加或删除选项,则会强制您编辑代码。相反,根据选项列表计算最大值,因此您只需添加或删除它们,所有内容都会进行调整

self.select输入
可能不应该是单独的方法。它可能也不应该被
display
调用,但这主要是程序员的选择

为这样一段简单的代码使用一个类是过分的。可以简化为:

CHOICES = {
  1 => 'Create a new flash card',
  2 => 'View all flash cards',
  3 => 'Edit a flashcard',
  4 => 'Delete a flashcard',
  5 => 'View score and answers',
}

def display
  loop do
    puts "Welcome player! Choose one of the following!"
    CHOICES.each do |h|
      puts '%d. %s?' % h
    end

    input = gets.to_i

    return input if CHOICES[input]
    puts 'Invalid option.' 
  end
end

def select number
  puts "#{number} selected!"
end

choice = display
select(choice)
使用类来封装方法,而不使用它来拥有类的实例,很快就会过时。Ruby允许我们使用类或不使用类来编写代码。当你写作时,想一想什么是简明易懂的。有时,我们只是简单地开始,必须备份一点,然后再运行一次,因为我们认为使用类更有意义。我通常让程序的复杂性决定这一点

使用一系列的
if
/
elsif
/
语句有时很有用。我们通常可以使用
case
/
when
来清理代码,甚至比使用
if
链更有效:

CHOICES = [
  'Create a new flash card',
  'View all flash cards',
  'Edit a flashcard',
  'Delete a flashcard',
  'View score and answers',
]

def display
  loop do
    puts "Welcome player! Choose one of the following!"
    CHOICES.each.with_index(1) do |s, i|
      puts '%d. %s?' % [i, s]
    end

    input = gets.to_i

    case input
    when 1 .. CHOICES.size
      return input
    else
      puts 'Invalid option.' 
    end
  end
end
用这些方法代替以前的方法并进行实验


…如何(“%d.%s?”%[i,s])[工作]

请注意,
'%d.%s?“
是一个字符串。这将使
%
成为字符串的方法


请参见

这可能更适合作为一个问题。另外,在Ruby中使用两个空格作为缩进。欢迎使用堆栈溢出。我们不在乎你的经验水平,我们想要的是经过充分研究和简洁的问题。有关更多信息,请阅读“”和“”及其链接页。通过阅读github上各种流行Ruby项目的代码,可以更好地学习惯用Ruby代码。Rails实际上充满了Ruby更深层次的魔力,因此我建议首先花大量时间单独使用Ruby。Sinatra和Padrino是一个很好的起点,然后进入轨道。@theTinMan这是一个相当普遍的答案,当涉及到经验水平时,在一定程度上也可以理解。不过,我会公布我的经验水平,以确保人们知道如何给出答案。这个网站和访问者构成了不同的体验层次,如果你能理解如何正确地与人沟通,而不是在单独的页面上,这是一件好事。重要的是要理解,so不是一个“回答我的问题”的网站。相反,这是一个“这里有一个特定的编程问题,以及如何修复它的网站”。因此,提出的问题必须包含某些信息,以帮助未来的搜索者解决同样的问题。它必须写得清楚、简洁,并解决一个或最多两个密切相关的问题。“我如何改进我的代码”是一个非常开放的话题,并且非常开放地接受意见,因此,不适合这样做。可能是一个更好的选择,这取决于你阅读他们的问题要求。这比我目前拥有的要好。但是,当涉及到多个答案时,如何更好地调用class.methods呢?在这种情况下,您可能希望创建类简介的实例,每个实例都包含一个不同的答案作为实例变量。更改内容时不要使用“edit”或“update”标记。相反,将更改合并,就像它最初存在一样。如果需要的话,我们可以看到什么改变了,什么时候改变了。哦那个呵呵。我打算删除实例变量。移除。箱子绝对很棒。我感谢你的答复。这实际上使板材更加流动。你是否可以重复(“%d.%s?“%[i,s])我是如何尝试谷歌搜索的,但没有找到任何解释。我用谷歌搜索了每一个。这是一个非常方便的方法,谢谢。