How random is random, Ruby on Rails UUID's 2

Posted by Joel Jensen Mon, 24 Dec 2007 20:08:21 GMT

I needed a good random number. Some opensource login generator I used had

They made a random number like this.
Digest::SHA1.hexdigest( Time.now.to_s.split(//).sort_by {rand}.join )

I don't know if thats really random. I think it is.

UUID's are unique by definition. I have a ruby uuid library. 

Ruby is slow for UUID's
Mysql is fast.

# a function that uses mysql > 4.1 to generate uuid's. Just stick it in your model.
def uuid()
   sql = "SELECT UUID()"   
   record = connection.select_one(sql)
   return record['UUID()']
end

Rails RJS page.replace_html "div_id_name", :text=> "text goes here" 4

Posted by Joel Jensen Mon, 24 Dec 2007 02:51:55 GMT

I constantly get burned on this one. You cannot render partial :text in an rjs template.

NO ::  page.replace_html "div_id_name", :text=> "text goes here"
YES :: page.replace_html "div_id_name", "text goes here"

My pain your gain.

Undelete a file in linux ext3 filesystem

Posted by Joel Jensen Wed, 19 Dec 2007 01:35:23 GMT

Yes I screwed up. And I needed that file too.

After alot of looking I found the solution

download sleuthkit unzip and compile. The program you need is in that-folder/bin

Basicly:
Open up a terminal session.
su root

# find the device that your hard drive is using
 mount | column -t

# open up the drive in debugfs, In my case the drive is at /dev/sda1
debugfs /dev/sda1

# cd to where the file was
cd /var/mail

# show the deleted file
ls -d

# the deleted files are in the brackets <somenumbers>
# note that number

q

# find the block group that contains that node
debugfs: imap <415926>
Inode 415926 is part of block group 25
    located at block 819426, offset 0x0a80

# find the Blocks per group for the drive
debugfs: stats
[...]
Blocks per group: 32768
[...]

# figure out the start block
# start-block= block-group * blocks-per-group

# figure out the end block
# end-block= start-block + blocks-per-group - 1

# Run sleuthkit and get the file
cd {sleuthkit directory}/bin

# for this demo case
# start-block=819200
# end-block=851867

# make sure that the file you write to ( > ) is on a different filesystem or you may overwrite your source file, I didn't do this and got lucky.
dls /dev/sda1 819200-851867 > /mnt/yourdata.dat

# your data MAY be at /mnt/yourdata.dat

django ImportError: No module named yoursitename 8

Posted by Joel Jensen Mon, 17 Dec 2007 00:47:43 GMT

This has been frustrating me on and off for quite a while. Here’s how to fix it.

add a file called __init__.py to your folder. Done.

The error message, if you can call it that, is vague.

Basically, all python modules require this file.

Django doesn’t add one when you create a site ( an oversight in my opinion ), so you have to remember to add one. I have been doing PYTHONPATH funkiness and other hacks to get the site working without this. Now I can just relax, code and drink tea.

Computer accessories Signal - 2 - Noise

Posted by Joel Jensen Thu, 08 Nov 2007 16:01:28 GMT

I just bought a most important computer accessory, eyeglasses. My prescription is very light, but was having trouble with monitors. I asked the eye doctor about this. I had been using my reading glasses. Doctor said for computer use go down .25 from your reading glasses, this is because of the different focal target area, computers are further away than books. So 1.00 becomes 0.75. I can concentrate much easier. More Signal, less noise

Scratch, a cool programming language for kids

Posted by Joel Jensen Fri, 12 Oct 2007 15:47:18 GMT

I was at a Chris and Lisas’ house last night. Their son (10) showed me some video games he had just created using a really innovative smalltalk IDE for children. It’s called “scratch” http://scratch.mit.edu. The various controll structures in programming are shaped like lego/ puzzle pieces. Certain actions only fit with in certain data structures. But piecing them together you can easily make and learn programming. I think this really really would have ‘fit my head’ when I was young. After watching him for about 20 minutes, I could easily see him writing much more difficult things in only a few years.

The IDE generates smalltalk squeak in the background, creating objects, actions, amimations, images and passing messages around between objects. There’s even a smalltalk debugger, but it’s hidden enough that you really really have to want to get at it. I assume this is to prevent people from accidenly going there and getting confused.

You make the datastructures, then associate them with images or animation. Their son made a pretty respectable game in 2 days. When done, you can share it on the scratch site, people can play it, download the code and extend it themselves. They call this ‘remixing’. Its a really cool way of getting kids into open source programming.

Scratch

How much is $450 billion? 1

Posted by Joel Jensen Sun, 09 Sep 2007 09:33:00 GMT

I was wondering today, if we took all the money spent in Iraq, as dollar bills, would it blanket the entire country?

Dollar bill = 6.6294cm * 15.5956 = 103.3894 square cm per dollar bill

square cm per square meter = 10,000

square m per square km = 1,000,000

cm per km = 10,000,000,000

dollar bills per km = 96,721,714

cost of Iraq war so far $450,000,000,000

km of paper coverage = 4,653 square kilometers ( the state of Deleware is 5,133 square km )

area of Iraq = 437,072 SQ KM

We have a way to go, we’ve only gotten a little over 1% coverage so far.

If it were pennies:

$1 = 156.3175 cm of pennies,arranged in a hexagonal fasion 6 pennies touching every penny

7,035 square km. Probabally enough to pave all the roads in copper.

https for rails 2

Posted by Joel Jensen Tue, 28 Aug 2007 05:27:00 GMT

Heres some tips for rails https.

First – use the plugin SslRequirement. But REMEMBER this ONLY works on controllers that are listed in the routes.rb file as RESOURCES

map.resources :blahblah

IE they only work on resources. Period. For non resource controllers, you will have to stick it in the links.

<%= button_to "Checkout", {:action => :checkout,:controller=>"catalog",:protocol=>"https://",:only_path=>false },{}%>

Here is how to use the ssl plugin

ruby script/plugin install ssl_requirement


class ApplicationController < ActiveRecord::Base
    include SslRequirement
end


class AccountController < ApplicationController
    ssl_required :signup, :payment
    ssl_allowed :index

    def signup
      # Non-SSL access will be redirected to SSL
    end

    def payment
      # Non-SSL access will be redirected to SSL
    end

    def index
      # This action will work either with or without SSL
    end

    def other
      # SSL access will be redirected to non-SSL
    end
end

Next Here is a working apache / mongrel config for a https ssl virtualhost. REMEMBER enable proxy, rewrite, and headers in the apache modules stanza

<IfDefine SSL>
<IfDefine !NOSSL>
<VirtualHost 192.168.1.42:443>
    DocumentRoot /srv/www/site-name/current/public
    ServerName www.site-name.com:443
    ServerAdmin webmaster@site-name.com
    ErrorLog /var/log/apache2/site-name.com-error_log
    CustomLog  /var/log/apache2/site-name.com-access_log combined
    HostnameLookups Off
    UseCanonicalName Off
    ServerSignature Off

    SSLEngine on
    SSLCipherSuite ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP:+eNULL 
    SSLCertificateFile    /etc/apache2/site-name-ssl/www.site-name.com.crt
    SSLCertificateKeyFile /etc/apache2/site-name-ssl/site-name.key
    SSLCertificateChainFile  /etc/apache2/site-name-ssl/gd_intermediate_bundle.crt
    SetEnvIf User-Agent ".*MSIE.*" nokeepalive ssl-unclean-shutdown downgrade-1.0 force-response-1.0

    <Directory "/srv/www/site-name/current/public">
        Options FollowSymLinks
        AllowOverride None
        Order allow,deny
        Allow from all
    </Directory>

    RewriteEngine On


    # Don't do forward proxying
    ProxyRequests Off

    # Enable reverse proxying
    <Proxy *>
        Order deny,allow
        Allow from all
    </Proxy>


    RequestHeader set X_FORWARDED_PROTO 'https'

    RewriteCond %{DOCUMENT_ROOT}/system/maintenance.html -f
    RewriteCond %{SCRIPT_FILENAME} !maintenance.html
    RewriteRule ^.*$ /system/maintenance.html [L]

    RewriteRule ^/$ /index.html [QSA] 
    RewriteRule ^([^.]+)$ $1.html [QSA]


    # Redirect all non-static requests to cluster
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
    RewriteRule .* http://127.0.0.1:3000%{REQUEST_URI} [L,P,QSA]

    # Deflate
    AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml application/xml application/xhtml+xml text/javascript 
    BrowserMatch ^Mozilla/4 gzip-only-text/html
    BrowserMatch ^Mozilla/4.0[678] no-gzip
    BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

</VirtualHost>                                  

</IfDefine>
</IfDefine>

My pain is your gain.

SVN externals 1

Posted by Joel Jensen Thu, 23 Aug 2007 08:20:00 GMT

Here is how to set a directory to fetch as an svn:externals directory. I keep forgetting this. Now you can remember it too.

#make sure your editor variable is set, find the editor
# or which vi or whatever
which emacs  

#no spaces in EDITOR="
export EDITOR="/usr/bin/emacs"     

cd THE-DIRECTORY-YOU-WANT-EXTERNAL-TO-BE-IN

#propset the svn externals on the current directory, this pops up the editor
svn propedit svn:externals .     

# in emacs --- in emacs -- in emacs one directory per line.
apache-ant http://svn.repository.my/apache-ant/trunk/

# you saved the file, back in the shell
#download the repo as the new directory directory-name, in this case apache-ant
svn update

DONE

Nested Java classes from jruby

Posted by Joel Jensen Thu, 02 Aug 2007 17:06:00 GMT

I needed to get at a nested java class from jruby.

It’s tricky. Here’s How I did it

Java class ‘Fonzie’ has a nested public class ‘Hair’

include_class 'com.als.Fonzie'
Hishair = Fonzie::Hair
folicle = Hishair.new(nil)   # if the constructor takes no arguments you must include nil here.

# view that it's really a nested class

folicle.java_class 

# returns com.als.Fonzie$Hair

Older posts: 1 2 3 4 5 6 ... 10