2 mem fox mem_fox 于 2016.03.01 00:48 提问

怎么能把这段Ruby代码改的更像Ruby的

def match_standard?()
standard = StandardBodyGrith.find_by_leight_and_sex(1700,'M')
print standard
print shoulder
is_match = Array.new(6)
is_match[0] = shoulder < standard.shoulder
is_match[1] = chest < standard.chest
is_match[2] = arm_relaxed < standard.arm_relaxed
is_match[3] = waist < standard.waist
is_match[4] = hip < standard.hip
is_match[5] = thigh < standard.thigh
return is_match
end

2个对象的shoulder/chest/arm relaxed/waist/hip/thigh 6个属性是一一对应的,我怎么比较直接生成一个boolean的数组?

3个回答

devmiao
devmiao   Ds   Rxr 2016.03.08 13:01

可以用hashtable

liuk10
liuk10   2016.04.01 08:15

1,在ruby中一般不用return,因为方法最后一行的执行结果就是返回值
2,定义方法时,如果没有参数,一般不写括号
3,这种场景下用Hash很合适,当然如果需要Array类型也是可以的。

4,具体修改如下:
(1)返回数组
def match_standard?
standard = StandardBodyGrith.find_by_leight_and_sex(1700,'M')
print standard
print shoulder

[
  shoulder < standard.shoulder,
chest < standard.chest,
arm_relaxed < standard.arm_relaxed,
waist < standard.waist,
hip < standard.hip,
thigh < standard.thigh
]

end

(1)返回Hash
def match_standard?
standard = StandardBodyGrith.find_by_leight_and_sex(1700,'M')
print standard
print shoulder

{
  shoulder:      shoulder < standard.shoulder,
chest:           chest < standard.chest,
arm_relaxed: arm_relaxed < standard.arm_relaxed,
waist:           waist < standard.waist,
hip:              hip < standard.hip,
thigh:           thigh < standard.thigh
}

end

liuk10
liuk10   2016.04.01 08:17

答案提交了以后不能再编辑吗???我想调整下格式。。。

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!