Want to stay on top? Ruby Weekly is a once-weekly e-mail newsletter covering the latest Ruby and Rails news.
     Feed Icon

By Peter Cooper / May 26, 2006

For the fun of it, I developed a quick Ruby obfuscation tool a few weeks ago. It’s not a proper obfuscater that changes variable names and such, but it’ll keep your code from being immediately understood. Here’s the library, px.rb:

class PX; def self.method_missing(m, *args); m = m.to_s; eval $c.join if m == ‘-@’; m.scan(/(\w)(\w)/).each { |t| ($c ||= []) << (((t[0][0] – 97) * 26) + (t[1][0] – 97)).chr }; end; end

And here’s a demonstration program, using the px.rb decoder, that displays some text on the screen:

require ‘px’ PX.bxbwbuemebefdxelbgdwehakeienemelbgbndjdxeedvehefdxbgemehbgdudtdwbgdeenduerbg PX.ehdudyeneldvdtemebehegbhbnakdxegdwak -PX

Pretty incomprehensible, right? Here’s a program that will help you create your own encoded / obfuscated Ruby scripts called pxcode.rb:

require ‘px’ PX.eienemelbgbiekdxejenebekdxbgbneieqbnbiakdueebgcjbgdndpakebbgcjbgbwakbkelemdw PX.ebegbudxdtdveabgetbgeueeebegdxeuakeeebegdxbuelendubhbobvdqdoelbrbvbsbgbnbnbp PX.akeeebegdxbudxdtdveadrdueremdxbgetbgeudueuakdubgcjbgdubuemehdrebakebbgbrcjbg PX.bxbgebdybgdueednebdpbgbmbmbgdueednebdpbueedxegdzemeabgckbgcdcbakdueednebdpbg PX.eueucjbgbnbnakdueednebdpbgbrcjbgbodubgbvbgbyccbgbrbgcfcdbpbudveaekbgbrbgbodu PX.bgblbgbyccbgbrbgcfcdbpbudveaekakevakevakdueebudxdtdveabgetbgeueeeuakeienemel PX.bgbidcdkbubjeteeevbiakevakeienemelbgbibtdcdkbi -PX

To use it, you’ll run.. Read More

By Peter Cooper / May 26, 2006

An anonymous commenter contributed a cute Ruby example on this post talking about Java’s verbosity. The original poster lamented on how much code you have to write to create some basic accessors on a Java class.

With Ruby you can simply use attr, attr_accessor, attr_reader and attr_writer to create various types of accessors with a single line of code. They lack logic though, and you need to write your own accessors from scratch if you want even basic logic included. Mr. Anonymous came up with a solution, which I’ve slightly changed to produce this:

class Class
def attr(name, &conditions)
varname = ‘@’ + name.to_s
conditions ||= lambda {true}
define_method(name) do
instance_variable_get(varname)
end

define_method(name.to_s + ‘=’) do |val|
raise ArgumentError, "#{name} can’t be #{val}" unless conditions.call(val)
instance_variable_set(varname, val)
end
end
end

class AClass
attr(:my_int) { |i| i >= 0 }
attr(:my_string)
end

With this code, ‘attr’ has been extended to support accepting a code block as a parameter. Read More

By Peter Cooper / May 26, 2006

New to Rails 3? Check out the Ruby on Rails 3 Tutorial book and screencast.

A book and screencast series showing you how to develop and deploy industrial-strength Rails apps in a direct, step by step way. The screencast series includes 12 lessons over more than 15 hours! Get the best “over the shoulder” experience of following what a top Rails 3 developer does when building an app today. Click here to learn more.

Please note that this post is over four years old – it’s from 2006! As such, these tips were relevant to Rails 1.2 and this content is woefully out of date. Read More

By Peter Cooper / May 25, 2006

While looking at NegaPosi, a crazy Ruby implementation of a micro language that only uses unary operators, I discovered a cute way to initialize instance variables. Usually you’d do this:

def initialize
@p = []
@b = []
end

But, how about doing it this way?

def initialize a=@p=[], b=@b=[]
end

Initialize doesn’t require the parameters, but it forces @p and @b to be initialized as arrays whenever an instance is created. Cute trick, although the readability suffers.

Technorati Tags:

Read More

By Peter Cooper / May 13, 2006

Welcome to Ruby Inside. Learn more about the blog. It’s early days yet, but let’s get cracking.. Read More

Recently Popular Posts