0

I am getting some weird output with my Ruby code. Here is the code:

            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}"

Here is the output:

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

Can anyone shed some light as to why 1. values are changing 2. why concat is adding the value twice 3. wtf I'm doing wrong here?

1 Answer 1

2
sDate = mDate.clone
eDate = mDate.clone

otherwise they all end up being the same object.

Also, please explain something about the problem, instead of just dumping code and output. By the end of your question, I read "wtf I'm doing wrong here" as "wtf I'm doing here".

Sign up to request clarification or add additional context in comments.

2 Comments

Or just don't use mutating methods (like concat) when you don't intend to mutate.
@AndrewMarshall: Valid. "Why are values changing?" "Because you changed them!" :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.