0

I have the following line of code in my model method.

subjectsinlist='['
subjectlist.subjects.each do |subject|
     subjectsinlist=subjectsinlist+subject.subject_code+', '
end
subjectsinlist.chomp(', ')
subjectsinlist+="]"

An example of the strings to append are:

CPE205 CPE206 CPE301 CPE302 HW0210

I am expecting the results to hence be:

[CPE205, CPE206, CPE301, CPE302, HW0210]

But instead I am getting:

[CPE205, CPE206, CPE301, CPE302, HW0210, ]

The chomp method does not seem to be working. Any advice on why that happened would be appreciated.

tereško
  • 58,060
  • 25
  • 98
  • 150
Butter Beer
  • 1,100
  • 3
  • 16
  • 32

3 Answers3

3

chomp returns a new string, see here

but u have to assign the new string to a variable:

subjectsinlist = subjectsinlist.chomp(', ')
Flo
  • 540
  • 6
  • 20
  • Thanks, I didn't have a clue why it didn't work since I did the exact same thing in console and received the correct output. – Butter Beer Feb 20 '13 at 19:17
  • 1
    because in your model you forgot to assign subjectsinlist.chomp(', ') to a variable. if you do it like this subjectsinlist = subjectsinlist.chomp(', ') the new string returned from chomp() will be assigned to subjectsinlist :) In console using subjectsinlist.chomp(', ') will give you the correct output but it only renders that output. – Flo Feb 20 '13 at 19:20
  • I got it now. Thanks for your help. – Butter Beer Feb 20 '13 at 19:24
  • 1
    You can also use the chomp! method to modify the string in place and avoid reassigning the variable. Example: subjectsinlist.chomp!(', ') – Michael Venable Feb 20 '13 at 19:45
2

This can do the trick:

codes = "[#{subjectlist.subjects.map(&:subject_code).join(', ')}]"

Some explanations:

  • The map(&:subject_code) will call the method subject_code on each element of the array returned by subjectlist.subjects

  • The join(', ') will put ', ' (coma-space) between each element of the array (except the last one).

The join method is what you need here ;-)

MrYoshiji
  • 54,334
  • 13
  • 124
  • 117
  • Just tried it and this worked perfectly while being neater but I would have to accept the answer on chomp since he answered specific to what I asked for. Thanks anyway. – Butter Beer Feb 20 '13 at 19:15
  • 1
    Well, using `chomp` is kinda wrong since we don't want to iterate on each x elements of the array. We just want to iterate on the x-1 elements (all except the last one). This is why `join` fits totally here. – MrYoshiji Feb 20 '13 at 19:18
  • Thanks. I am using this as well and your explanation was really clear. Being new to ruby I just basically tried whatever I knew to get it to work. So I really appreciate you introducing to me a new method. – Butter Beer Feb 20 '13 at 19:26
1

subjectsinlist = '[' + subjectlist.subjects.join(', ') + ']'

That should work.

The reason chomp isn't working for you is because it returns a new string, rather than changing the existing string: http://apidock.com/ruby/String/chomp

ChrisC
  • 2,461
  • 1
  • 16
  • 25
  • Tried this out but I wanted a specific attribute of the list. This is a much cleaner way of putting it though so thanks! – Butter Beer Feb 20 '13 at 19:18
  • `subjectlist.subjects` will return Subject objects, which you are trying to join. (chomp before your edit), but we need the `subject_code` attribute of each subjects ... – MrYoshiji Feb 20 '13 at 19:20
  • Yep, in the initial code it wasn't immediately clear as the formatting was bad: Your solution is correct. – ChrisC Feb 20 '13 at 19:23