Strip HTML in any string in Ruby


A little code snippet to let you remove HTML from any string in Ruby:
class String

# remove all HTML tags from the source string
def strip_html
self.gsub(/]*>/, "")

Then to run the code just do this:
blurb_html = "

Thanks for visiting our site.

blurb_text = blurb_html.strip_html

Programming for kids, and older kids


One fault that could be levied at the computer industry is that they've lost a lot of the out-of-the-box creativity experience from the early days, back in the 80's when every computer you bought came out of the box with a programming language that anyone (with a manual or two) could start writing their own Hello World apps. Back then most people didn't learn to program at college, they learned from their $200 home computers. There is a new option for people seeking this experience again called Hackety Hack, a new programming kit based on the existing Ruby language bundled in a really easy-to-use package. Although aimed at children it is also well suited to anyone who wants to try their hand at something a little bit different, and is well worth giving a try.

N00b FAQ: belongs_to is singular


A little tidbit I forgot along the way - in Rails the belongs_to (1-to-many association directive at the 1 side) directive needs you to assign it a singular symbol and not a multiple, e.g.:
[source:ruby]class Product belongs_to :category

MySQL + Rails on Cygwin tip


Here's a really quick tip for anyone using Rails on Cygwin. If you want to connect to a MySQL database the understood practice is to compile MySQL and go through the hassle of getting it all working within Cygwin itself. After what I've gone through with it all, the best way of getting this combination to work is to:

  • Install MySQL under Windows itself rather than Cygwin.
  • Make sure that the MySQL binaries are in your system path under Windows and, if installing via SSH, restart the Cygwin SSH service so that your login will be able to see the new path.
  • When installing the MySQL gem (gem install mysql) select the mswin32 option rather than the pure Ruby option.
  • Now, when you define your database connections just give the machine's IP address instead of saying "localhost" and it'll work great!

After wasting several days getting it all to work the "proper" way, the above is a much cleaner solution.


Subscribe to Ruby