Archive for the ‘ Ruby ’ Category

Sending email with ruby – TMail

http://yakinikunotare.boo.jp/orebase/index.php?Ruby/TMail
http://tmail.rubyforge.org/rdoc/classes/TMail/Mail.html

Simple email sending:

require 'NET / SMTP'
require 'TMail'
require 'base64'
 
Mail = TMail:: Mail.new
 
Mail.to = 'you@gmail.com'
Mail.from = 'me@gmail.com'
Mail.subject = 'Test Mail '
Mail.date = Time.now
Mail.Mime_Version = '1 .0 '
Mail.Set_Content_Type 'text', 'plain', {'charset'=>'UTF-8'}
Mail.Body = 'This is Test Mail . '
 
Net:: SMTP.start("smtp.yourhost.com", 587, "smtp.yourhost.com", "username", "password") do | SMTP | SMTP.sendmail(Mail.encoded, Mail .from, Mail.to)
end

Send attachments:

require 'NET / SMTP'
require 'TMail'
require 'base64'
 
# object
Mail = TMail:: Mail.new
 
Mail.to = 'you@gmail.com'
Mail.from = 'me@gmail.com'
Mail.subject = 'Test Mail'
Mail.date = Time.now
Mail.Mime_Version = '1 .0 '
Mail.Set_Content_Type 'multipart', 'mixed', {"boundary" => "Somethingspecial"}
 
# body
Mail_Body = TMail:: Mail.new
Mail_Body.Set_Content_Type 'text', 'plain', {'charset'=>'UTF-8'}
Mail_Body.Body = 'Ge almost'
Mail.Parts.push(@ Mail_Body)
 
# Attachments
Attach = TMail:: Mail.new
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
Mail.Parts.push(Attach)
 
# send
Net:: SMTP.start("smtp.yourhost.com", 587, " smtp.yourhost.com ", "username", "password") do | SMTP | SMTP.sendmail(Mail.encoded, Mail.from, Mail.to)
end

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 <fred@bloggs.com>"
email.match(/<(.*?)>/)[1]            # => "fred@bloggs.com"
email[/<(.*?)>/, 1]                  # => "fred@bloggs.com"
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.