SyntaxHighlighter

Monday, April 11, 2011

Very true! Makes a change to see someone spell it out like that

I just got this 'comment' on a wordpress blog I write (unrelated to web development). After doing a quick search on google for this phrase: Makes a change to see someone spell it out like that, you can see a lot of other blogs have received the same post.

There's an interesting little code in front of it. It's random, and never the same on the blog. Mine was 'TRuxcI'.

Makes me think someone is out there testing to see if they can get you to add a special code to your blog. If so, they'll probably spam you.

Just delete them.

Sunday, April 03, 2011

String Truncate

Here's a truncate method that's a little smarter than the one that comes with rails. It won't cut off in the middle of a word. Just put this code in a file (like string_initializers.rb) and put that your initializers folder under config.



class String

def truncate length
if length > 0
self.strip!
if self.length > length
out_array = []
words = self.split(' ')
for word in words
if ((out_array + [word]).join(' ').length) < length
out_array << word
else
break
end
end
string_to_return = out_array.join(' ')

# in case truncate is too short
if string_to_return.length == 0
string_to_return = self[0..length]
end

# remove any trailing spaces.
string_to_return.strip!

# special last characters
if string_to_return[-1,1] == '.'
string_to_return += '..'
elsif string_to_return[-1,1] == ','
string_to_return.chop!
string_to_return += '...'
else
string_to_return += '...'
end
return string_to_return
else
return self
end
end
end
end