Archive for the ‘ Ruby ’ Category

Sending email with ruby – TMail

Simple email sending:

require 'NET / SMTP'
require 'TMail'
require 'base64'
Mail = TMail:: = ''
Mail.from = ''
Mail.subject = 'Test Mail ' =
Mail.Mime_Version = '1 .0 '
Mail.Set_Content_Type 'text', 'plain', {'charset'=>'UTF-8'}
Mail.Body = 'This is Test Mail . '
Net:: SMTP.start("", 587, "", "username", "password") do | SMTP | SMTP.sendmail(Mail.encoded, Mail .from,

Send attachments:

require 'NET / SMTP'
require 'TMail'
require 'base64'
# object
Mail = TMail:: = ''
Mail.from = ''
Mail.subject = 'Test Mail' =
Mail.Mime_Version = '1 .0 '
Mail.Set_Content_Type 'multipart', 'mixed', {"boundary" => "Somethingspecial"}
# body
Mail_Body = TMail::
Mail_Body.Set_Content_Type 'text', 'plain', {'charset'=>'UTF-8'}
Mail_Body.Body = 'Ge almost'
Mail.Parts.push(@ Mail_Body)
# Attachments
Attach = TMail::
Attach.Body = Base64.Encode64 File.Open('foo.jpg', "rb").read
Attach.Set_Content_Type 'image','jpg','name'=>'foo.jpg'
Attach.Set_Content_Disposition ' Attachment ','filename'=>'foo.jpg'
Attach.transfer_encoding = 'base64'
# attach
# send
Net:: SMTP.start("", 587, " ", "username", "password") do | SMTP | SMTP.sendmail(Mail.encoded, Mail.from,

21 Ruby Tricks You Should Be Using In Your Own Code

21 Ruby Tricks You Should Be Using In Your Own Code.

Writing for Ruby Inside, I get to see a lot of Ruby code. Most is good, but sometimes we forget some of Ruby’s shortcuts and tricks and reinvent the wheel instead. In this post I present 21 different Ruby “tricks,” from those that most experienced developers use every day to the more obscure. Whatever your level, a refresh may help you the next time you encounter certain coding scenarios.

Note to beginners: If you’re still learning Ruby, check out my Beginning Ruby book.

1 – Extract regular expression matches quickly

A typical way to extract data from text using a regular expression is to use the match method. There is a shortcut, however, that can take the pain out of the process:

email = "Fred Bloggs <>"
email.match(/<(.*?)>/)[1]            # => ""
email[/<(.*?)>/, 1]                  # => ""
email.match(/(x)/)[1]                # => NoMethodError [:(]
email[/(x)/, 1]                      # => nil
email[/([bcd]).*?([fgh])/, 2]        # => "g"

Ultimately, using the String#[] approach is cleaner though it might seem more “magic” to you. It’s also possible to use it without including an extra argument if you just want to match the entire regular expression. For example:

x = 'this is a test'
x[/[aeiou].+?[aeiou]/]    # => 'is i'

In this example, we match the first example of “a vowel, some other characters, then another vowel.”

God – A Process Monitoring Framework in Ruby

God – A Process Monitoring Framework in Ruby.