Topic: Why is this join needed?

In Agile Web Development with Rails, the following statement is given:

assert_equal "has already been taken", product.errors[:title].join('; ')

My question is: Why is the join(...) needed and what exactly does it do here?

Last edited by BeSharp (2010-11-12 20:07:16)

Re: Why is this join needed?

a = ['one','two','three']
b = a.join(';')

b is now : "one;two;three"

In your case product.errors[:title] returns an array of error titles,  the titles are put together in a string separated by semi-colon and a space. 

join iterates, converts each element to string, and combines them separated by the argument string.

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: Why is this join needed?


This reconfirms what I thought it would do. But then my question is: Why is this needed? Will the assert ever be equal as it compares a string without a semicolon with one with a semicolon?

Or did I get anything wrong?

Re: Why is this join needed?

>> ["one"].join(";")
=> "one"
>> ["one", "two"].join(";")
=> "one;two"

The semicolon is only inserted if there is more than one element in the list. The only way this assert will pass is if there is only one error in the list, and it says "has already been taken".

That being said, this test is poorly written and really doesn't make any sense being written this way.