Pages

Tuesday, June 21, 2011

Ruby Modules: A perfect name space resolver?

Coming from java background, I was looking for package equivalent in Ruby and read in Programming Ruby 1.9 book that Ruby Module serves two purposes, 1) Namespace conflict resolver 2) Mixin Though, mixin is applauded one, now the one that bothers me is Namespace conflict resolver. lemme idealize it in to a simple example
module A

def test
  puts "I am in module A"
end

end

module B

def test
  puts "I am in module B"
end

end

Class Payment

include A
include B

end

p = Payment.new

p.test #prints - I am in module A

Now how to invoke test method in module B? But the same if I rewrite the those module methods as a class level methods, this works
module A

def self.test
  puts "I am in module A"
end

end

module B

def self.test
  puts "I am in module B"
end

end

Class Payment

include A
include B

def test_modules
  A.test
  B.test
end

end

p = Payment.new

p.test_modules #prints fine - I am in module A, I am in module B

when I do,
p.A.test #bangs - `
': undefined method `A' for # (NoMethodError)
What is this actually? I couldn't understand the concept, Is someone who could help me on this? Is Ruby a perfect namespace conflict resolver?

P.S - I got to know clear about this by watching this video MetaProgramming for Fun by Dave Thomas

Wednesday, March 2, 2011

Ruby Floating floats like a Dead Fish

I was so serious in attempting to solve a problem that Thoughtworks usually gives to its new recruits to test their Object Orientation skills. In that there is a Sales Tax problem which involves lot of math library, floating point calculations rather than using our brain memory in it.

While when I do the rounding of a sales tax calculation to the nearest 0.05, I faced the problem in my ruby
                                               
Though I agree that this is my first time where I am doing intense floating point calculations and round off, could someone explain me why this inconsistency happens with ruby?

I need at least two decimal places right in my calculations, rather than implementing a work around to round off a 8 decimal float number .


Also it should be noted that round(n) as written in Float class in ruby, which rounds to 'n' decimal places doesn't work with Jruby. Any clue on this???

Sunday, December 26, 2010

How to run PHP files inside public_html directory in Ubuntu 10.10 a.k.a Maverick Meerkat

By default, php files doesnt gets interpreted when placed inside public_html in Ubuntu 10.10. ie in Maverick Meerkat.

When you install php5 in ubuntu 10.10 or 10.04, planning to place your php files inside public_html directory in your user area, here is the solution for you.

Create public_html in your user area


First of all check whether enable-modules under your apache server
for that
goto /etc/apache2/mods-enabled/ folder and check userdir.conf, userdir.load modules are enabled by simply typing ls command

if not then goto /etc/apache2/mods-enabled and create symbolic link as follow


sudo ln -s ../mods-available/userdir.load

sudo ln -s ../mods-available/userdir.conf


or you can enable using the following command

sudo a2enmod userdir



Finally create public_html folder under your home directory an give full access permission to it.

Restart your apache server (sudo /etc/init.d/apache2 restart or service apache2 force-reload)

Access it through following link.

http://localhost/~[your_home_directory_name]/ (for me it's http://localhost/~hariharank12/)


To run PHP files inside public_html in Ubuntu 10.10

In the new php.conf file which is present in /etc/apache2/mods-available/php5.conf, the php engine inside user directory is turned off as like below.



 
 php_admin_value engine Off
 
 


Comment those above lines.

Finally php5.conf will look like below.


    
        SetHandler application/x-httpd-php
    
    
        SetHandler application/x-httpd-php-source
    
    # To re-enable php in user directories comment the following lines
    # (from  to .) Do NOT set it to On as it
    # prevents .htaccess files from disabling it.
#    
#        
#            php_admin_value engine Off
#        
#    
#


Again restart the apache web server.


Test with some php files placing inside public_html folder and hitting URL with localhost/~username/php_file_name.

It should work now! :)

For more detailed description why php files running inside public_html is disabled, see the following link