【问题标题】:Can Ruby call methods or procs like c calls functions?Ruby 可以调用方法或过程,如 c 调用函数吗?
【发布时间】:2013-03-12 01:32:08
【问题描述】:

我对 Ruby 很陌生。我是大学,刚刚上了一门涵盖常规 c 的编程课程。我的最后一个课程项目是一个 slop intercept 项目,这很容易,但我必须对所有事情都使用函数,例如:

#include <stdio.h>
#include <math.h>

int get_problem(int *choice){
  do {
  printf("Select the form that you would like to convert to slope-intercept form: \n");
  printf("1) Two-Point form (you know two points on the line)\n");
  printf("2) Point-slope form (you know the line's slope and one point)\n");
  scanf("%d", &*choice);
  if (*choice < 1 || *choice > 2){
      printf("Incorrect choice\n");}
  }while (*choice != 1 && *choice !=2);
  return(*choice);
}
...
int main(void);
{
  char cont;
    do {

  int choice;
  double x1, x2, y1, y2;
  double slope, intercept;
  get_problem (&choice);
...

我还有几个函数可以完成整个程序。我得到了一份新工作,我需要开始学习 Ruby,所以对于我的第一个项目,我想把这个程序转换成 Ruby,现在我可以简单地摆脱这些函数,只需要在没有方法或过程的情况下运行它。我想知道是否可以做同样的事情,定义一个方法,然后在不提供输入的情况下调用该方法,而是取回存储在该方法中的变量。是否可以使用方法或过程。以下是我目前使用 proc 所做的一些事情。

get_problem = Proc.new {
begin
puts "Select the form that you would like to convert to slope-intercept form: "
puts "1) Two-Point form (you know two points on the line)"
puts "2) Point-slope form (you know the lines slope and one point)"
choice = gets.chomp.to_i
if (choice < 1 || choice > 2)
    puts "Incorrect choice"
    end 
end while (choice != 1 && choice !=2)
}
....
begin
get_problem.call
case choice
when 1
    get2_pt.call
    display2_pt.call
    slope_intcpt_from2_pt.call
when 2
    get_pt_slope.call
    display_pt_slope.call
    intcpt_from_pt_slope.call

现在我知道我可能全都错了,但我想我会试一试。我以前有它作为方法

def get_problem(choice)
....
end
....
get_problem(choice)
....

我缺少一些基本的东西吗?如您所见,我在 c 中使用了指针,并且必须在 main 中初始化变量。

感谢您抽出宝贵时间帮助我。 罗伯特

【问题讨论】:

    标签: c ruby


    【解决方案1】:

    你不能在 Ruby 中传递一个指向变量的指针,但我认为你不需要这样做来完成你想要做的事情。试试这个:

    def get_problem
      puts "Select the form that you would like to convert to slope-intercept form: "
      puts "1) Two-Point form (you know two points on the line)"
      puts "2) Point-slope form (you know the lines slope and one point)"
    
      loop do
        choice = gets.chomp.to_i
        return choice if [1, 2].include? choice
        STDERR.puts "Incorrect choice: choose either 1 or 2"
      end
    end
    
    choice = get_problem
    puts "The user chose #{choice}"
    

    这定义了一个方法get_problem,该方法循环直到用户选择12,并返回他们选择的数字,您可以将其存储在顶级变量choice中。

    【讨论】:

    • 谢谢你,我认为当我有一个变量时这会有所帮助,但是对于我的下一个函数,我有 4 个变量,它会相似吗? double get2_pt(double *x1, double *x2, double *y1, double *y2){ do{ printf("\n输入第一个点的x-y坐标,用空格隔开:\n"); scanf("%lf%lf", &*x1, &*y1); printf("\n输入第二个点的x-y坐标,用空格隔开:\n"); scanf("%lf%lf", &*x2, &*y2); .... 返回(*x1,*x2,*y1,*y2); }
    • 那很臭,我不能像注释中的代码一样输入它,但你明白了,那是我的 c 函数从中获取 4 个变量。
    • 我想通了,在方法调用中我做了 (x1, y1, x2, y2) = get2_pt 并且如果他们必须发送一个变量我做了 display2_pt(x1, y1, x2, y2) .然后在方法中只是交互传入的变量并放入返回值。
    猜你喜欢
    • 1970-01-01
    • 2013-01-21
    • 2016-06-13
    • 1970-01-01
    • 2014-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多