From: Sharon Phillips Date: 2007-07-22T15:14:00+09:00 Subject: Re: extra text in puts command On 22/07/2007, at 3:45 PM, Jenny Purcell wrote: > This one seems incredibly simple and I'm sure I'm just missing > something obvious. > > I have the following 'puts' command to push my results to a csv file: > > puts CSV.generate_line([pole_new[j], horse_name[j], horse_info[j], > owners_initials[j], best_result[j], race_style[j], best_time[j], > pre_points[j], roll_1[j], roll_2[j], roll_3[j], roll_4[j], roll_5[j], > score[j], position[j]]) > > > What I'd like to do is add "pp" in front of the entry pole_new[j]. > > So, the results in the csv file will look like "pp6" when the contents > of pole_new is "6" > > For position, I'd like to do something similar, but, instead I'd like > to it come out as "/". So, I want to combine > position[j] and count in one CSV field. If the position is first of > ten, it would look like "1/10" in the CSV file. > > Do I need to escape the slash "/" and "pp" in some way? > > Jenny Hi Jenny, you could try puts CSV.generate_line(["pp#{pole_new[j]}", horse_name[j], ... , "# {position[j]}/#{count}" ] ) By using double quotes, Ruby checks the string first for special sequences which get evaluated first and the results inserted into the string. To 'drop in' a value we can surround it with #{ and }. Example puts "2 + 3 = #{2+3}" or puts "pp#{pole_new[j]}" In these examples, the expression between #{ and } is evaluated first and the result substituted, so #{2+3} is replaced with the result, 5. '/' does not require escaping, but '\' does. To escape '\' just put another before it "\\" Note that using single quotes makes Ruby take the string 'as is'. It ignores any escape chars and #{...} expressions. puts "2 + 3 = #{2+3}" # double quotes > 2 + 3 = 5 puts '2 + 3 = #{2+3}' # single quotes > 2 + 3 = #{2+3} I hope this makes sense... Cheers, Dave