将方法结果赋给ruby中的变量

时间:2021-09-22 23:29:47

I'm sure it would be hard to find an easier question, but I'm a complete newbie. I have searched extensively and for some reason can't find the answer to this. Here's my code:

我敢肯定很难找到一个更简单的问题,但我是一个完整的新手。我进行了广泛的搜索,由于某种原因无法找到答案。这是我的代码:

puts "Enter F for Fahrenheit and C for Celsius."

x = gets.chomp.downcase

def ftoc(fahrenheit)
  (fahrenheit.to_f - 32.0) * (5.0 / 9.0)
end

if x == "f"
  puts "Enter your temp:"
  temp = gets.chomp.to_i
  ftoc temp
elsif x == "c"
  puts "Enter your temp:"
  temp = gets.chomp.to_i
  ctof temp
else
  puts "That does not compute."
end

I'm just trying to get the returned result of the method into a variable so I can use it elsewhere....

我只是想把方法的返回结果变成一个变量,所以我可以在别处使用它....

1 个解决方案

#1


0  

Remember that calls like ctof temp just initiate a method and then, as you're not putting the result anywhere, discard it immediately.

请记住,像ctof temp这样的调用只会启动一个方法然后,因为你没有将结果放在任何地方,所以立即丢弃它。

To clean up this code let's organize it better:

要清理此代码,我们可以更好地组织它:

# Temperature conversion method
def ftoc(fahrenheit)
  (fahrenheit.to_f - 32.0) * (5.0 / 9.0)
end

# User input method
def temperature_prompt!
  puts "Enter F for Fahrenheit and C for Celsius."
  x = gets.chomp.downcase

  case (x)
  when "f"
    puts "Enter your temp:"
    temp = gets.chomp.to_i
    ftoc temp
  when "c"
    puts "Enter your temp:"
    temp = gets.chomp.to_i
    ctof temp
  else
    puts "That does not compute."
  end
end

Now you can make use of the fact that in Ruby things like if and case actually return values. In this case it's the value of the last thing to execute in each block, so that result isn't discarded, it's just passed along:

现在你可以利用Ruby这样的事实,比如if和case实际返回值。在这种情况下,它是在每个块中执行的最后一件事的值,因此结果不会被丢弃,它只是传递:

temp = temperature_prompt!

If you enter an invalid value you get the result of puts which is conveniently nil.

如果输入的值无效,则得到puts的结果,方便为零。

Here's something to consider: Ruby is very good at parsing arbitrary text if you can describe the patterns. Here's a simple input routine:

这是需要考虑的事情:如果你可以描述模式,Ruby非常擅长解析任意文本。这是一个简单的输入例程:

def temperature_prompt!
  puts "Enter degrees (e.g. 8F, 2C)"

  case (input = gets.chomp.downcase)
  when /(\d+)f/
    ftoc $1
  when /(\d+)c/
    ctof $1
  else
    puts "That does not compute."
  end
end

You could add to those patterns to allow things like -2C and 3.4°F if you wanted.

如果需要,您可以添加这些模式以允许-2C和3.4°F之类的东西。

#1


0  

Remember that calls like ctof temp just initiate a method and then, as you're not putting the result anywhere, discard it immediately.

请记住,像ctof temp这样的调用只会启动一个方法然后,因为你没有将结果放在任何地方,所以立即丢弃它。

To clean up this code let's organize it better:

要清理此代码,我们可以更好地组织它:

# Temperature conversion method
def ftoc(fahrenheit)
  (fahrenheit.to_f - 32.0) * (5.0 / 9.0)
end

# User input method
def temperature_prompt!
  puts "Enter F for Fahrenheit and C for Celsius."
  x = gets.chomp.downcase

  case (x)
  when "f"
    puts "Enter your temp:"
    temp = gets.chomp.to_i
    ftoc temp
  when "c"
    puts "Enter your temp:"
    temp = gets.chomp.to_i
    ctof temp
  else
    puts "That does not compute."
  end
end

Now you can make use of the fact that in Ruby things like if and case actually return values. In this case it's the value of the last thing to execute in each block, so that result isn't discarded, it's just passed along:

现在你可以利用Ruby这样的事实,比如if和case实际返回值。在这种情况下,它是在每个块中执行的最后一件事的值,因此结果不会被丢弃,它只是传递:

temp = temperature_prompt!

If you enter an invalid value you get the result of puts which is conveniently nil.

如果输入的值无效,则得到puts的结果,方便为零。

Here's something to consider: Ruby is very good at parsing arbitrary text if you can describe the patterns. Here's a simple input routine:

这是需要考虑的事情:如果你可以描述模式,Ruby非常擅长解析任意文本。这是一个简单的输入例程:

def temperature_prompt!
  puts "Enter degrees (e.g. 8F, 2C)"

  case (input = gets.chomp.downcase)
  when /(\d+)f/
    ftoc $1
  when /(\d+)c/
    ctof $1
  else
    puts "That does not compute."
  end
end

You could add to those patterns to allow things like -2C and 3.4°F if you wanted.

如果需要,您可以添加这些模式以允许-2C和3.4°F之类的东西。

相关文章