Ruby输出不一致

时间:2012-12-05 00:27:19

标签: ruby

我使用Ruby代码获得了一些奇怪的输出。这是代码:

            mDate = schedule['date']
            sTime = schedule['startTime']
            eTime = schedule['endTime']

            puts "Original Start Time: #{sTime}"
            puts "Original End Time: #{eTime}"
            puts "Original Date: #{mDate}"

            sDate = mDate
            eDate = mDate

            puts "sDate: #{sDate}"
            puts "eDate: #{eDate}"

            @startTime = sDate.concat("T" + sTime + "Z")
            @endTime = eDate.concat("T#{eTime}Z")
            @date = schedule['date']

            puts "Start Time: #{@startTime}"
            puts "End Time: #{@endTime}"
            puts "Date: #{@date}"

            puts "Original Start Time: #{sTime}"
            puts "Original End Time: #{eTime}"
            puts "Original Date: #{mDate}"

这是输出:

Original Start Time: 15:00:00
Original End Time: 15:00:00
Original Date: 2012-12-05
sDate: 2012-12-05
eDate: 2012-12-05
Start Time: 2012-12-05T15:00:00ZT15:00:00Z
End Time: 2012-12-05T15:00:00ZT15:00:00Z
Date: 2012-12-05T15:00:00ZT15:00:00Z
Original Start Time: 15:00:00
Original End Time: 15:00:00
Original Date: 2012-12-05T15:00:00ZT15:00:00Z

任何人都可以了解为什么1.值正在改变2.为什么concat将值加两次3. wtf我在这里做错了?

1 个答案:

答案 0 :(得分:2)

sDate = mDate.clone
eDate = mDate.clone

否则它们最终都成为同一个对象

另外,请解释一下有关问题的内容,而不仅仅是转储代码和输出。在你的问题结束时,我读到“wtf我在这里做错了”作为“我在这里做的事情”。