Working with HTML Document in Ruby

Ruby is very versatile programming language when it comes to HTML. This language was designed for the web in mind and as such it has a lot of methods and classes to offer for dealing with HTML pages.

One common example often time used when dealing with HTML is URL extraction. Ruby requires you to include “uri” class before you can proceed

include “uri”
text =”My http://www.site.com site on the web”

URI.extract(text) //extracts URL from the string text
URI.extract(text, [‘http’,’https’]) //extracts http and https urls from the text

URI.extract relies on regular expressions to extract needed part of HTML

Ruby has multiple other gems that allow you to convert text to HTML. One such gem is called RedCLoth. It’s to_html method convert text into HTML.

gem ‘RedCloth’
require ‘redcloth’

text = RedCloth.new%{MY_TEXT_HERE}

puts text.to_html

Ruby allows a web html document conversion into text as well.

Featured pages

Ruby

Set of Ruby Object Oriented Programming Language tutorials that cover such topics as Ruby strings, …

Rails

Rails Framework tutorial teaches you how to utilize de facto framework of choice for Ruby developme…

Ruby Duck Typing

“If an object quacks like a duck just go ahead and treat it as a duck” – this fun…

Regular Expressions

Ruby uses the =~ operator to check any given string against regular expression. For example, a stri…

Credit Card Number

Every last digit of any credit card is a check sum digit that is determined by all digits in front …

Ruby Arrays

Ruby Programming Language has built in support for Arrays. Arrays help you define some of the compl…

Ruby Hashes

Hashes are very similar to arrays in Ruby and hashes interface is similar to Ruby array interface. …

Ruby Code Block

Ruby is very unique language when it comes to code blocks. You can simply pass a code block to a me…