Ruby可以调用像c这样的方法或过程调用函数吗?

Ruby可以调用像c这样的方法或过程调用函数吗?,c,ruby,C,Ruby,我对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-intercep

我对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);
...
有什么基本的东西我遗漏了吗?正如您所看到的,我在c中使用了指针,并且必须在main中初始化变量

谢谢你抽出时间来帮助我。
Robert

在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
,该方法循环直到用户选择
1
2
,并返回他们选择的数字,您可以将其存储在顶级变量
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%lf,&*x2,&*y2);…返回(*x1,*x2,*y1,*y2)}这太糟糕了,我不能像在注释中输入代码一样输入它,但你明白了,那就是我的c函数从它们那里获取了4个变量。我在方法调用中找到了它,我做了(x1,y1,x2,y2)=get2_pt,如果它们必须发送一个变量,我会显示2_pt(x1,y1,x2,y2).然后在方法中,只需对输入的变量进行往复运算,并输入返回值。
def get_problem(choice)
....
end
....
get_problem(choice)
....
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}"