Friday, June 27, 2008

Finish What You Start


As usual we will go straight to the point.

Three Ways to finish things:
  • Throw it away. Put an end to it. Example: Your sudoku game. Seriously is not that important.

  • Do it yourself. But really, do it.

  • Delegate it. Find someone to do it for you, your friend, or that developer from india. Make sure that you find someone who you can trust.


Why is it important?
Sense of accomplishment and tranquility. Yes, now you can get that out of your mind. As embarrassing or ridiculous you may think it is; it is now out of your head.

How is it relevant to programming?
Finish that project you started a couple of days ago and gave up because it turned too complicated. Spend an entire night finding the solution. It will be more rewarding than what you think and you will probably learn more than what you intended.

Crazy Googled Internet Quote
"The way to achieve inner peace is to finish all the things you've started."

Monday, June 23, 2008

PHP Segmentation Fault

I ran around this problem a couple of times, and recently i spent more than 20 mins trying to figure out why my php page was seg faulting. Well now I'm going to document it.

To make it simple, this code will give you a seg fault:

class A
{
function __construct()
{
new B;
}
}

class B extends A
{

}

new A;


Don't create new objects from classes that inherit from the class you are creating the object. Yes sounds confusing, and probably it makes no sense but i think the code above is enough to explain it.

Monday, June 9, 2008

New Company Website


My new website is up at : http://coaxialhost.com Handcoded HTML, JS and CSS. Fun to make but is it Google friendly? I hope it is.

Saturday, June 7, 2008

Validating (X)HTML using Ruby


This function returns validation messages from the w3c website (http://validator.w3.org)

require 'net/http'
require 'uri'

class XHTML

VALIDATOR_URL = 'http://validator.w3.org/check'
XHTML_STRICT = 'XHTML 1.0 Strict'
XHTML_TRANSITIONAL = 'XHTML 1.0 Transitional'
XHTML_FRAMESET = 'XHTML 1.0 Frameset'

# Returns validation messages
def XHTML.validate(what, xhtml_version=XHTML_STRICT)
post_data = {
'fragment' => what,
'doctype' => xhtml_version
}

r = Net::HTTP.post_form(URI.parse(VALIDATOR_URL), post_data)

return r.body
end

end



We could try to parse the results into an array... maybe in the future.

Friday, June 6, 2008

Creating Custom Ruby Accessors

Built In Ruby Accessors

attr_reader :a # Generates a reader/get function:
#def a
# @a
#end

attr_writer :a # Generates a writer/set function
#def a= (value)
# @a = value
#end

attr_accessor :a # Generates Both get/set functions



Creating Custom Accessors

We need to define a function in the main Module class:

class Module
def custom_accessor(*symbols)
symbols.each do |symbol|
class_eval "
# Here we would insert the code that we want to generate.
"
end
end
end


And this is it. Now we can use our custom accessor like this:

custom_accessor :accessor

Friday, May 30, 2008

Ruby Predefined Constants/Variables

Constants
ENV # Hash of environment variables
STDIN # IO stream for standard input
STDOUT # IO stream for standard output, most cases the console
STDERR # IO stream for standard error output
RUBY_VERSION # String containing current ruby version
RUBY_RELEASE_DATE # String containing the date in which ruby was compiled
RUBY_PLATFORM # String containing the platform and compiler used to build ruby
ARGV # Array of arguments used to run the ruby file. ARGV[0] is the first argument, not the name of the file.


Variables
$: # Ruby Include Path
$> # STDOUT
$. # Current Line
$! # Latest error message
$@ # location of error
$_ # string last read by gets
$. # line number last read by interpreter
$& # string last matched by regexp
$~ # the last regexp match, as an array of subexpressions
$n # the nth subexpression in the last match (same as $~[n])
$= # case-insensitivity flag
$/ # input record separator
$\ # output record separator
$0 # the name of the ruby script file
$* # the command line arguments
$$ # interpreter's process ID
$? # exit status of last executed child process
$SAFE # safe level (0-4)
$" # Loaded modules, extensions (also $LOADED_FEATURES)

Sunday, May 25, 2008

Mysql Ruby Extension

Installing in Windows

Download mysql dev files from : http://dev.mysql.com/downloads/mysql/5.0.html
Install them in your hard drive. We only need the include and lib files, not the server. Download the zip package.

Download last mysql-ruby extension from http://tmtm.org/downloads/mysql/ruby/ and extract it.

we run extconf.rb with the following parameters:

ruby extconf.rb --with-mysql-include=/path/to/mysql/include --with-mysql-lib=/path/to/libs

By default the mysql installer saves the lib files in the lib/opt.

To compile the extension:

nmake

Now we should have a valid mysql.so file. We need to get the libmysql.dll from the bin folder of the mysql distribution, you can grab it from the zip version.

To test it:

ruby test.rb [hostname [user [passwd [dbname [port [socket [flag]]]]]]]