Archive for the 'Code Snippets' Category



Your favourite bit of Ruby code?

Friday 9 February 2007 @ 11:34 am

An interesting thread at Ruby_talk is ‘Your favorite bit of Ruby code.’


Setting aside time for learning HTML is beneficial to any web host.


John Carter
from New Zealand has this interesting snippet of code:

# Ruby is Objects all the way down and open for extension...
class Integer
  def factorial
    return 1 if self <= 1
    self * (self-1).factorial
  end
end

puts 6.factorial

Technorati Tags:

Posted by Satish Talim



Convert bytes to megabytes

Tuesday 6 February 2007 @ 11:26 am

In one of my projects, I need to find the file-size in megabytes again and again. This simple method helps me to convert the size of a file in bytes to megabytes. This is useful for very large files.

MEGABYTE = 1024.0 * 1024.0
def bytesToMeg bytes
  bytes /  MEGABYTE
end

# big file
len = File.size("Dreamweaver8-en.exe")
puts len.to_s + ' bytes'  # displays 62651176 bytes
puts bytesToMeg(len).to_s + ' MB'  # displays 59.7488174438477 MB

One of the uses for PDF conversion is that by going through the process of converting PDF to Word you’ll have a more easily editable document than if you didn’t do PDF conversion and tried to edit a PDF file.

The program code is pretty obvious and as you can see the method uses division. However, it is best to wrap this functionality in a method.

Technorati Tags: ,

Posted by Satish Talim



Next Posts »»

RUBYGALORE.COM