在Ruby中为if语句赋一个变量

时间:2014-09-04 21:17:56

标签: ruby if-statement

我有这个if语句:

  if value_n_in_f > 0 && value_m_in_f != 1
    puts "I: f(x)=#{value_m_in_f}x+#{value_n_in_f}"
  elsif value_n_in_f == 0 && value_m_in_f != 1
    puts "I: f(x)=#{value_m_in_f}x"
  elsif value_n_in_f < 0 && value_m_in_f != 1
   puts "I: f(x)=#{value_m_in_f}x#{value_n_in_f}"
  elsif value_n_in_f > 0 && value_m_in_f == 1
    puts "I: f(x)=x+#{value_n_in_f}"
  elsif value_n_in_f == 0 && value_m_in_f == 1
    puts "I: f(x)=x"
  elsif value_n_in_f < 0 && value_m_in_f == 1
    puts "I: f(x)=x#{value_n_in_f}"
  end`

我必须经常在其他语句中使用此语句,这会使我的代码不必要地长。显然if_for_f = if ....... end将无法正常工作。还有其他方法吗? 这是我想要的样子:

puts "Now we insert #{value_of_x} in #{if_for_f}"

我有什么方法可以做这样的事吗?请注意,我绝对是新手。

提前致谢,  Kaiton

3 个答案:

答案 0 :(得分:4)

  

我不能用函数来定义函数来运行这种情况   声明,然后在需要它时调用它?

当然:

def do_stuff(m, n)
  if m == 1
    if n > 0      then "I: f(x)=x+#{n}"
    elsif n == 0  then "I: f(x)=x"
    elsif n < 0   then "I: f(x)=x#{n}"
    end
  else
    if n > 0      then "I: f(x)=#{m}x+#{n}"
    elsif n == 0  then "I: f(x)=#{m}x"
    elsif n < 0   then "I: f(x)=#{m}x#{n}"
    end
  end
end

puts do_stuff(1, 1)  #I: f(x)=x+1

或者,如果目标是紧凑性,我们可以达到这个目的:

def do_stuff(m, n)
  if m == 1
    n == 0  ? "I: f(x)=x"     : "I: f(x)=x#{sprintf("%+d", n)}" 
  else
    n == 0  ? "I: f(x)=#{m}x" : "I: f(x)=#{m}x#{sprintf("%+d", n)}" 
  end
end

......然后是一个班轮:

def do_stuff(m, n)
  (m == 1) ? (n == 0  ? "I: f(x)=x" :  "I: f(x)=x#{sprintf("%+d", n)}") : (n == 0  ? "I: f(x)=#{m}x" : "I: f(x)=#{m}x#{sprintf("%+d", n)}")
end
end

但是你的方法有零和-1的问题:

def do_stuff(value_m_in_f, value_n_in_f)
 if value_n_in_f > 0 && value_m_in_f != 1
    puts "I: f(x)=#{value_m_in_f}x+#{value_n_in_f}"
  elsif value_n_in_f == 0 && value_m_in_f != 1
    puts "I: f(x)=#{value_m_in_f}x"
  elsif value_n_in_f < 0 && value_m_in_f != 1
   puts "I: f(x)=#{value_m_in_f}x#{value_n_in_f}"
  elsif value_n_in_f > 0 && value_m_in_f == 1
    puts "I: f(x)=x+#{value_n_in_f}"
  elsif value_n_in_f == 0 && value_m_in_f == 1
    puts "I: f(x)=x"
  elsif value_n_in_f < 0 && value_m_in_f == 1
    puts "I: f(x)=x#{value_n_in_f}"
  end
end

do_stuff(1, 0)
do_stuff(1,-1)
do_stuff(1, 1)
do_stuff(0,-1)
do_stuff(0, 0)
do_stuff(-1, 1)

--output:--
I: f(x)=x
I: f(x)=x-1
I: f(x)=x+1
I: f(x)=0x-1   #<---HERE
I: f(x)=0x     #<---HERE
I: f(x)=-1x+1  #<---HERE

所以让我们解决这个问题:

def get_line_equ(m, b)
  constant = (b == 0) ? "" : sprintf("%+d", b) # 2 => "+2"

  case m
  when 0 
    xterm = ""
    constant = b
  when 1 
    xterm = "x"
  when -1 
    xterm = "-x"
  else 
    xterm = "#{m}x"
  end

  "I: f(x)=#{xterm}#{constant}"
end

puts get_line_equ(0, 0)
puts get_line_equ(0, -1)
puts get_line_equ(0, 1)

puts get_line_equ(1, 0)
puts get_line_equ(1,-1)
puts get_line_equ(1, 1)

puts get_line_equ(-1, 0)
puts get_line_equ(-1, -1)
puts get_line_equ(-1, 1)

puts get_line_equ(2, 0)
puts get_line_equ(2, -1)
puts get_line_equ(2, 1)

--output:--
I: f(x)=0
I: f(x)=-1
I: f(x)=1
I: f(x)=x
I: f(x)=x-1
I: f(x)=x+1
I: f(x)=-x
I: f(x)=-x-1
I: f(x)=-x+1
I: f(x)=2x
I: f(x)=2x-1
I: f(x)=2x+1

更好?

扰流器:

  

最终的def并不像它那样高效:第一行应该被移除并复制到每个when分支 - 除了第一行。

回应评论:

def my_sprintf(str, *numbers)
  str.gsub(/% .*? [df]/x) do |match|  #Looks for %...d or %...f sequences
    puts match
  end
end

my_sprintf("The answer is: %+d or %+d", -2, 3)


--output:--
%+d
%+d

下一步:

def my_sprintf(str, *numbers)

  str.gsub(/% .*? [df]/x) do |format_sequ|
    number_as_str = numbers.shift.to_s
    p number_as_str

    if format_sequ[1] == "+" and number_as_str[0] != "-"
      "+#{number_as_str}"
    else
      number_as_str
    end

  end
end

puts my_sprintf("The answer is: %+d or %+d.", -2, 3)

--output:--
"-2"
"3"
The answer is: -2 or +3.

答案 1 :(得分:4)

这是编写案例陈述的更紧凑方式。回想一下a <=> b => -1 if a < ba <=> b => 0 if a == ba <=> b => -1 if a > b

"I: f(x)=" +
case [value_n_in_f <=> 0, value_m_in_f == 1]
when [ 1,false] then "#{value_m_in_f}x+#{value_n_in_f}"
when [ 0,false] then "#{value_m_in_f}x"
when [-1,false] then "#{value_m_in_f}x#{value_n_in_f}"
when [ 1,true]  then "x+#{value_n_in_f}"
when [ 0,true]  then "x"
when [-1,true]  then "x#{value_n_in_f}"
end

如果您希望演示如何构建字符串,可以执行以下操作(分别使用value_n_in_fvalue_m_in_f重命名为interceptslope):< / p>

"I: f(x)=" +
case
when slope.zero?
  intercept.zero? ? "0" : "#{intercept}"
else
  case slope.to_f
  when  1.0 then ""
  when -1.0 then "-"
  else "#{slope}"
  end + "x" +
  case intercept <=> 0
  when  0 then ""
  when -1 then "#{intercept}"
  else "+#{intercept}"    
  end
end

请注意,这允许slope < 0,这不是规范的一部分。我针对interceptslope的各种组合对此进行了测试:

   intercept slope    string
       -2.1   4      I: f(x)=4x-2.1
       -2.1   -2.2   I: f(x)=-2.2x-2.1
       -2.1   0      I: f(x)=-2.1
       -2.1   0.0    I: f(x)=-2.1
       -2.1   -1     I: f(x)=-x-2.1
       -2.1   1.0    I: f(x)=x-2.1
       0      4      I: f(x)=4x
       0      -2.2   I: f(x)=-2.2x
       0      0      I: f(x)=0
       0      0.0    I: f(x)=0
       0      -1     I: f(x)=-x
       0      1.0    I: f(x)=x
       0.0    4      I: f(x)=4x
       0.0    -2.2   I: f(x)=-2.2x
       0.0    0      I: f(x)=0
       0.0    0.0    I: f(x)=0
       0.0    -1     I: f(x)=-x
       0.0    1.0    I: f(x)=x
       3      4      I: f(x)=4x+3
       3      -2.2   I: f(x)=-2.2x+3
       3      0      I: f(x)=3
       3      0.0    I: f(x)=3
       3      -1     I: f(x)=-x+3
       3      1.0    I: f(x)=x+3

答案 2 :(得分:2)

这里有几件事:你可以把&#34; puts&#34;在整个if / elsif块之前,避免对每一行进行放置,如下所示:

puts case 
  when (value_n_in_f > 0 && value_m_in_f != 1) then "I: f(x)=#{value_m_in_f}x+#{value_n_in_f}"
  when (value_n_in_f == 0 && value_m_in_f != 1) then "I: f(x)=#{value_m_in_f}x"
end

其次,案例陈述更具可读性,如下:

def big_compare(value_n_in_f, value_m_in_f)
  msg = case
    when (value_n_in_f > 0 && value_m_in_f != 1) then "I: f(x)=#{value_m_in_f}x+#{value_n_in_f}"
    when (value_n_in_f == 0 && value_m_in_f != 1) then "I: f(x)=#{value_m_in_f}x"
    when (value_n_in_f < 0 && value_m_in_f != 1) then "I: f(x)=#{value_m_in_f}x#{value_n_in_f}"
    when (value_n_in_f > 0 && value_m_in_f == 1) then "I: f(x)=x+#{value_n_in_f}"
    when (value_n_in_f == 0 && value_m_in_f == 1) then "I: f(x)=x"
    when (value_n_in_f < 0 && value_m_in_f == 1) then "I: f(x)=x#{value_n_in_f}"
  end
end
puts big_compare(0, 0)