Ruby调用基于elseif语句的方法 [英] Ruby Calling a method based on an elseif statement

查看:62
本文介绍了Ruby调用基于elseif语句的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我仍然在学习如何在Ruby中进行编码,并且遇到了一些我很好奇的新知识.我的老师刚刚开始教我们关于方法的知识,我想知道您是否可以基于if-else语句调用/创建方法.例如,如果您有一个程序要求用户输入某人的名字,那么您可以使用该输入来决定使用哪种方法吗?

So I'm still chugging along with learning how to code in Ruby and I've come across something new that I'm curious about. My teacher just started teaching us about methods and I was wondering if you could call/create a method based on an if-else statement. Like for example if you had a program that asked the user to type in someone's name could you then use that input to decide which method would be used?

示例:

puts "Please enter name(Brian, Andy, Tod)"
string = gets.to_i
if string == "Brian"
   def b(string)
       puts "Hi Brian"
   return b

elsif string == "Andy"
   def a(string)
       puts "Hi Andy"
   return a

elsif string == "Tod"
   def t(string)
       puts "Hi Tod"
   return t
else
   puts "Not a valid entry"
end

我知道代码可能无效,我只是在脑海中创建了代码以阐明我的意思,但是实际上有办法吗?

I know that code likely does not work, I just created it off the top of my head to clarify what I meant, but is there actually a way to do this?

推荐答案

是的,执行此操作的通常方法是预先定义方法,然后从(elsif)条件中调用: >

Yeah, the normal way to do this is to define the methods beforehand then invoke them from the (elsif) conditional:

def b
  puts "Hello brian"
end

def a
  puts "Hello andy"
end

def t
  puts "Hello tod"
end

puts "Please enter name(Brian, Andy, Tod)"
string = gets.chomp
if string == "Brian"
   b
elsif string == "Andy"
   a
elsif string == "Tod"
   t
else
   puts "Not a valid entry"
end

当您自己说a时,与说a()相同-调用该方法.您可以从技术上定义条件内的方法(在调用它们之前),但这不是好的样式,很少这样做.

when you say a by itself that's the same as saying a() - invoking the method. You could technically define the methods inside the conditional (before invoking them) but this isn't good style and is rarely done.

其他一些要点-

  1. 这些方法不使用string参数,因此您可以像我一样删除它
  2. gets.to_i所说的获取输入并将其转换为整数"-不是您要在此处执行的操作.您要查找的是gets.chomp,它会获得一行输入并从末尾删除\n换行符(所有gets输入都将在末尾有一个换行符)
  1. these methods don't use the string parameter so you can remove it, like I've done
  2. gets.to_i is saying "get input and convert it to integer" - not what you want to do here. What you're looking for is gets.chomp, which gets a line of input and removes the \n newline character from the end (all gets input will have a newline character at the end)

请注意,此条件链似乎是case的良好候选者,您可以将puts重构到单个位置-

Note this conditional chain seems like a good candidate for case, and you can refactor the puts into a single place -

def b
  "Hello brian"
end

def a
  "Hello andy"
end

def t
  "Hello tod"
end

input = gets.chomp
puts case input
when "Brian" then b
when "Andy" then a
when "Tod" then t
else "not a valid entry"
end

或者您可以使用哈希结构代替方法

or you could use a hash structure instead of methods

puts {
  "Brian" => "Hello brian",
  "Andy" => "Hello andy",
  "Tod" => "Hello tod"
}.fetch(gets.chomp, "Invalid input")

这篇关于Ruby调用基于elseif语句的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆