Archive for category Ruby

Tweet from the command line

Avoid using Twitter’s new god awful interface and tweet from the command line like a real man.

cmd_twitter.rb

Rubygems annoyances

Earlier today while working on Ruby CLI Mailer, I had a problem installing the pony rubygem. I was getting the following error when trying to install the pony via rubygems:

ERROR: Error installing pony:
invalid gem format for /home/tony/.rvm/gems/ruby-1.8.7-p352/cache/pony-1.3.gem
ERROR: Error installing pony:
invalid gem format for /home/tony/.rvm/gems/ruby-1.9.2-p290/cache/pony-1.3.gem

I soon came to notice that the problem was because the Barnes&Noble wifi session had expired and I had to reconnect to their network. After getting my laptop online, I tried to install the gem, but once again I was still getting the invalid gem error. This was even after I removed the gem from all Ruby installations (RVM) and tried to install them from scratch. Knowing that the issue was caused because the files were partially downloaded, I removed both cache gem files and voila, I was able to install the pony rubygem.

While it would be wonderful if every single rubygem package would be digitally signed of some sort, it would have been helpful if at least it had some sort of checksum verification after it downloads the package prior to installing any gems. This is certainly somewhat scary since rubygems is not able to detect/verify that it downloaded the right package without any sort of alterations.

First Git cloned repo – installing rvm

For quite a while now, I’ve heard good things about the Ruby Version Manager (if only I knew about it before I installed Ruby 1.9 from source and screwed up rubygems). So I decided to install this application, but to my surprise in reading the documentation, the official install is only made via cloning the latest rvm github repository.

sudo apt-get install git git-core git-doc git-svn
mkdir Git_Repos/rvm
cd Git_Repos/rvm
git config --global user.name "Alpha01"
git config --global user.email "root@rubyninja.org"
git config --global core.editor /usr/bin/vim
git init
bash < <(curl -s https://rvm.beginrescueend.com/install/rvm)

Congratz, I just installed rvm and cloned my very first git repository.

Tags: ,

What’s in my bookshelf?

As always I’m pretty late in writing my book reviews. This time it’s been well over 6 months since I finished reading the book “Beginning Ruby” (first edition) by Peter Cooper. I have to say is this by far the best Ruby book that I’ve read so far. (This is out of a 13 book collection that I own on Ruby programming.) I’ve always been a fan of Apress “Beginning” title’s, and this book is definitely not the exception. Although this book is technically considered for beginning/intermediate programmers wanting to dive to the wonderful world of Ruby, I feel this book is perfect for all levels of programming expertise. The authors does a wonderful job explain the concepts of each chapter and slowly building you to what will be the next chapter. Like Apress’ “Beginning Perl” book help me tremendously in learning the language, I have to say this book taught how to program in Ruby.

I’ll definitely come back to this book and use as a reference. Anyone new or already experienced using Ruby, will definitely benefit from reading this book. I hope this book (all editions) gets the high praise from the Ruby community, which it deserves, as in my eyes this is a classic programming book and I see this title as the Llama/Camel book for the Ruby World.

Chapter 1: Let’s Get it Started: Installing Ruby
Chapter 2: Programming == Joy: A Whistle-Stop Tour of Ruby and Object Orientation
Chapter 3: Ruby’s Building Blocks: Data, Expressions and Flow Control
Chapter 4: Developing a Basic Ruby Application
Chapter 5: The Ruby Ecosystem
Chapter 6: Classes, Objects, and Modules
Chapter 7: Projects and Libraries
Chapter 8: Documentation, Error Handling, Debugging, and Testing
Chapter 9: Files and Databases
Chapter 10: Deploying Ruby Applications and Libraries
Chapter 11: Advance Ruby Features
Chapter 12: Tying it Together: Developing a Larger Ruby Application
Chapter 13: Ruby on Rails: Ruby’s Killer App
Chapter 14: Ruby and the Internet
Chapter 15: Networking, Sockets, and Daemons
Chapter 16: Useful Ruby Libraries and Gems

Beginning Ruby (2nd Edition)
5/5

Tags: ,

Moving to Ruby 1.9

I’ve been using the Ruby 1.8.6 on my CentOS box and Ruby 1.8.7 on my Ubuntu and OS X computers, for a couple of years now; I’ve never payed much attention on continuing to hack Ruby code using these versions of Ruby.
However, in reading DHH’s tweet stating that Ruby 1.8X series is now considered legacy and should be avoided and in reading through some of the Metasploit Framework initial documentation, I’ve decided to migrate to the latest Ruby 1.9 build (Ruby 1.9.2 patch level 180, at the time of this writing).

Upgrading was really easy, essentially on both CentOS and on Ubuntu I installed Ruby 1.9 directly from source, while I was able to install Ruby 1.9 on the Mac using Mac Ports.

The only thing that problem that I noticed in doing this, is that my existing Rubygems installation crapped out, which really isn’t a problem since I was able to successfully install a new version of Rubygems. So far the only incompatibilty that I have noticed so far is the Crypt gem which is not yet been updated to work with Ruby 1.9. There is an unofficial port called Crypt19, buts it’s such horribly undocumented (giving the library implementation is not helpful) that I couldn’t get my existing scripts to work using this library.

Now hopefully I’ll find more features on Ruby 1.9 that are worth doing the upgrade.

Tags:

Ruby cli_mailer

After much nights of Ruby programming and many revisions, I finally manage to have an beta release of Ruby cli_mailer.

I originally planned on writing a full mail (Unix command line email client) like program in Ruby. Though I was able to have an almost identical console interface to mail’s reading, viewing, and deleting emails, using POP. I was having a really hard time, keeping the connection alive after a certain number of seconds after initiating the connection.

So instead of having the incoming POP email client like usage that mail provides, I instead wrote the application to only support the mail sending features almost similar to mailx.

I would think developers or anyone wanting to send email from a Linux/Unix server without the hassle of sendmail, postfix, exim, etc… may find this simple program useful. I know I do, since I always find myself wanting to easily send emails from the command line whenever I’m using or doing some sort of testing on a Virtual Machine.

If you’ve ever used mail on a Linux/Unix system to send email messages, Ruby cli_mailer is practically identical.

Basic Usage:
Will get prompt to write email message that gets sends to root@rubyninja.net and blinds copy ccemailaddress@rubyninja.org

rcli_mailer.rb -s 'Title of message' -c ccemailaddress@rubyninja.org root@rubyninja.net

Will email the contents of sendmail.cf to root@rubyninja.net

cat /etc/mail/sendmail.cf | rcli_mailer.rb -s 'God help me' root@rubyninja.net


rcli_mailer.rb -s "I'm so sorry" -c jrsysadmin@rubyninja.net root@rubyninja.net < /etc/postfix/main.cf

NOTE: Ctrl-D is NOT supported by Ruby cli_mailer at the moment, you'll need to use the '.' (dot) character
to send the email message.

I tried to make Ruby cli_mailer as moduler as possible, since I'm planning adding the project to github, and I already have in mind more features to add into it.

Enjoy :-)
rcli_mailer-01.tar.gz

Documentation
rdoc

Tags: ,

Phusion Passenger = good stuff

While mod_ruby is perfect to serve dynamic Ruby web pages, the project looks like its long dead; given that it hasn’t been updated for over 4 years now. Now the de facto way to deploy Ruby on Rails applications is Phusion Passenger. Compared to mod_ruby, configuring the Phusion Passenger (mod_rails) Apache module is ridiculously easy to do. Essentially all I had to do is download and install the gem.

The only quirk, which was entirely my fault for being lazy, was not reading the portion of the documentation where it mentions that the default environment is set to production. This gave me quite a headache as I couldn’t figure out why I had to restart Apache every time I modified any controller code (RoR noob).

My fix:
.htaccess
RailsEnv development

I’m amazed on Phusion’s work, the company responsible for the module. As they’ve take something that’s been historically difficult to deploy (at least I think) and made it really simple.
http://www.modrails.com/

RDoc: Documenting Ruby scripts

RDoc makes documenting Ruby scripts be really simple. RDoc is fairly simple to use, as it has a straight forward SimpleMarkup syntax which the RDoc utility uses to traverse the entire Ruby script and automatically generate pretty html documentation pages.

Now I have documentation for the dead simple Ruby script that I wrote to encode videos for my iphone.
iphone_encode.rb documentation

What’s in my bookshelf?


I finally finished reading my first book on Ruby programming. Ruby: Visual QuickStart Guide, by Larry Ullman has to be the perfect introduction book for anyone trying to learn the Ruby programming language, even though sadly it only has 3 five star reviews on Amazon. The author writing thoroughly explains the concepts in a friendly and easy to understand manner. This being the third book that I’ve read from this same author, the other titles being for MySQL and PHP (no reviews on these yet since I’m not fully finished reading them).

One thing I would’ve like changed the author to change was on the database chapter, as it was mainly focused on SQLite. It would have been better if the author used MySQL instead. Also it would have been better if the author removed the dedicated chapter to Rails, and instead extended the chapter to have more generic web related like Net::HTTP, given how powerful that single class is.

Chapter 1: Getting Started
Chapter 2: Simple Scripts
Chapter 3: Simple Types
Chapter 4: Array, Ranges, and Hashes
Chapter 5: Control structures
Chapter 6: Creating Methods
Chapter 7: Creating Classes
Chapter 8: Inheritance and More
Chapter 9: Modules and Includes
Chapter 10: Regular Expressions
Chapter 11: Debugging and Error Handling
Chapter 12: Rubygems
Chapter 13: Directories and Files
Chapter 14: Databases
Chapter 15: Networking
Chapter 16: Ruby on Rails
Chapter 17: Dynamic Programing

I would highly recommended this book to anybody starting or wanting to learn Ruby.
Ruby-Visual-QuickStart
4/5

Re-enabling mod_ruby on perlninja.pl

I finally managed to re-enable mod_ruby on my server once again. This time around, I had a hard time enabling eRuby to work in Apache.

New Apache configuration file:

AddType text/html .rhtml
LoadModule ruby_module modules/mod_ruby.so
#<IfModule mod_ruby.c>
RubyRequire apache/ruby-run
RubyRequire apache/eruby-run
<Files *.rb>
Options +ExecCGI
SetHandler ruby-object
RubyHandler Apache::RubyRun.instance
</Files>
<Files *.rhtml>
SetHandler ruby-object
RubyHandler Apache::ERubyRun.instance
</Files>
#</IfModule>

The Apache eRuby SetHandler (rhtml) was not able to render any html/ruby pages, while regular ruby scripts (.*rb) were able to run just fine.

Apache log error:
[Sun Aug 08 20:56:15 2010] [error] mod_ruby: (eval):45: (eval):45: uninitialized constant Apache::ERubyRun (NameError)

FIX:

Specify the absolute path to the eRuby Apache module in /usr/lib/ruby/1.8/apache/eruby-run.rb.

Change  line 45

require ‘eruby’

to

require “/usr/lib/ruby/1.8/i386-linux/eruby.so”

Restart Apache and eRuby should render html/ruby pages as expected.

I hope people find this useful (and save a headache) as I spent about 5 hours researching this problem and I wasn’t able to find this fix anywhere.