【问题标题】:How do I modify my addition and subtraction functions so that arrays can be passed as arguments using Ruby?如何修改我的加法和减法函数,以便可以使用 Ruby 将数组作为参数传递?
【发布时间】:2019-08-27 23:09:21
【问题描述】:

我正在做一个作业,我创建了两个使用 splatter 方法获取参数的方法(加法和减法)。我正在尝试修改每种方法,以便我也可以将数组作为参数传递。我尝试使用条件语句来检查我的参数是否为数组,但这不起作用。

这是我的代码:

class Math
def initialize 
    puts "Welcome to Math Dojo" 
    self    
end

def add (*nums)
    @sum = nums.reduce :+
    if nums == []
        nums.reduce :+
    puts "The sum is #{@sum}"
    self
end

def subtract (*nums)
    @diff = nums.reduce :-
    if nums == []
        nums.reduce :-
    end
    puts "The difference is #{@diff}"
    self
end
end

我需要代码通过以下测试:

challenge1 = Math.new.add(2).add(2, 5).subtract(3, 2) # => 4
challenge2 = Math.new.add(1).add([3, 5, 7, 8], [2, 4.3, 1.25]).subtract([2,3], [1.1, 2.3])
challenge3 = Math.new.add(2, 5).subtract(9, 3, 5, 2)

代码当前将通过challenge1 和challenge3。如何修改它以通过所有三个?

【问题讨论】:

  • 请(编辑)用文字解释什么是“飞溅法”。您指的是“以下测试”。在第一个中,所需结果显示为4,但其他两个未显示所需结果。也许您可以显示计算结果所涉及的计算(而不是代码),例如,Math.new.add(1,2).add([3, 4], [5,6]).subtract([7,8], [12,10]),第一步是将其减少到Math.new.add(3).add(7,11).subtract(-1, 2),然后将其减少到Math.new.add(3).add(18).subtract(-3),最后获得18?顺便说一句,Ruby 有一个内置模块 Math
  • "that didn't work" 不是一个足够精确的错误描述,我们无法为您提供帮助。 什么不起作用? 如何不起作用?你的代码有什么问题?您收到错误消息吗?错误信息是什么?你得到的结果不是你期望的结果吗?你期望什么结果,为什么,你得到的结果是什么,两者有什么不同?您正在观察的行为不是期望的行为吗?期望的行为是什么,为什么,观察到的行为是什么,它们有何不同?

标签: arrays ruby methods addition


【解决方案1】:

添加数组参数,即使它们与数字混合,也不会增加复杂性,因为包含数组和可能数字的数组只需要在获得总数之前被展平。

class MyMath
  attr_reader :tot

  def initialize
    @tot = 0
  end

  def add(*obj)
    compute(*obj, :+)
  end

  def subtract(*obj)
    compute(*obj, :-)
  end

  def multiply(*obj)
    compute(*obj, :*)
  end

  def divide(*obj)
    compute(*obj, :/)
  end

  def compute(*obj, op)
    @tot = obj.flatten.reduce(@tot, op)
    self
  end
end

MyMath.new.add(2).add(2, 5).subtract(3, 2).tot
  #=> 4
MyMath.new.add(2).add(2, 5).subtract(3, 2).multiply(2, 4).tot
  #=> 32
MyMath.new.add(2).add(2, 5).subtract(3, 2).divide(2.0, 4.0).tot
  #=> 0.5
MyMath.new.add(1).add([3, 5], [2, 4.3]).subtract([2,3], [1.1, 2.3]).tot
  #=> 6.9
MyMath.new.add(2, 5).subtract(9, [3, 5], 2).tot
  #=> -12

【讨论】:

  • 如果您已经知道方法的名称(reduce),则无需使用public_send
  • 也不需要使用send__send__。仅当您不知道方法名称时才需要它。在您的情况下,您知道名称:reduce。你可以直接调用它。
  • @Jörg,这很明显!谢谢。
【解决方案2】:

一些事情:

  1. 如果你想确定 nums 是否是一个数组,你可以使用nums.is_a?(Array)nums == [] 检查 nums 是否为空数组。
  2. nums 始终是一个数组。你真正想要确定的是nums 中的任何元素是否是一个数组,如果是,你想减少它们。您要么需要停止在 nums 上使用 reduce(因为它不适用于数组),要么使用更简单的路由和 flatten! 数组。

https://ruby-doc.org/core-2.2.0/Array.html#method-i-flatten-21

flatten! 获取一个数组并将其转换为单个数组。它在原地修改数组而不是创建新数组,这意味着它比 flatten 对应物(确实有它的位置)更有效。

看看这个:

class Math
def initialize 
    puts "Welcome to Math Dojo" 
    self    
end

def add (*nums)
    nums.flatten!
    @sum = nums.reduce :+
    puts "The sum is #{@sum}"
    self
end

def subtract (*nums)
    nums.flatten!
    @diff = nums.reduce :-
    puts "The difference is #{@diff}"
    self
end
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-14
    • 1970-01-01
    • 2015-12-29
    • 2011-10-31
    • 1970-01-01
    • 1970-01-01
    • 2015-07-31
    • 2016-11-02
    相关资源
    最近更新 更多