KBD

Keith Devens .com

Saturday, July 5, 2008 Flag waving
And if you go too far up, abstraction-wise, you run out of oxygen. Sometimes smart thinkers just don't... – Joel Spolsky

Tag: Code

Daily link icon Wednesday, September 12, 2007

while(<>){...} in Ruby

Re: translate Perl diamond operator to Ruby.

Perl:

while(<>){
  ...
}

Ruby:

ARGF.each do |$_|
  ...
end

At this point I question whether I'd ever use Perl for anything again. Until now, Perl filled a niche where if the code I wanted to write would fit in 10 lines or so, and did a lot of string manipulation, I'd turn to Perl. Otherwise Python. Now I think I'll just use Ruby for everything Smiley

Daily link icon Friday, July 13, 2007

Javascript toggle functions

Some more handy Javascript (depends on Prototype):

function toggleByClass(id, className, callback){
    // shows the element with id 'id'
    // and hides all other elements with class 'className'
    $A($$('.'+className)).each(function(element){
        $(element).style.display = "none";
    })
    if(id) // call it with a false id to hide all
        $(id).style.display = "block";
    if(callback)
        callback(id, className);
}

function toggleById(id1, id2){
    var a = $(id1).style;
    var b = $(id2).style;
    if(a.display == 'none' || a.display == ''){
        a.display = 'block';
        b.display = 'none';
    }else{
        a.display = 'none';
        b.display = 'block';
    }
}

Daily link icon Thursday, July 12, 2007

The annoying way to .clone() a list in C#

List<Type> temp = list.ConvertAll<Type>(delegate(Type a){return a;});

You'd think a generic type like List would have a useful clone or copy method, but noo. (Type is just a placeholder... insert your type here)

  1. I dunno if I have a recent version of my blogging bookmarklet on my site anywhere... last time I tried to find it I think the version I found was outdated. So here it is: Blog.

       (0) Tags: [Bookmarklets, Code, This website]

Daily link icon Thursday, June 7, 2007

  1. Because I'm sure it'll come in handy later, here's a javascript object clone function:

    function clone(obj){
        if(obj == null || typeof(obj) != 'object')
            return obj;

        var temp = {};
        for(var key in obj)
            temp[key] = clone(obj[key]);
        return temp;
    }

    Update: From feedback in the comments, a better version is:

    function clone(obj){
        if(obj == null || typeof(obj) != 'object')
            return obj;

        var temp = new obj.constructor(); // changed (twice)
        for(var key in obj)
            temp[key] = clone(obj[key]);

        return temp;
    }
       (19) Tags: [Code, Javascript]

Daily link icon Wednesday, January 24, 2007

Obfuscated code of the day

results = commands.collect{|i| host.cmd(i)}
  .collect{ |i| i.collect{ |i| i.chomp }[1..-2] }

(reformatted slightly)

What this code does: takes a list of commands to run over a telnet session, runs them all, takes the output for each command, splits it into an array of lines (removing any stray newlines on each line), strips off the first and last lines of the output (since the first line is just an echo of the command and the last line is the command prompt again), and returns an array of lines of the output for each command in an array.

Daily link icon Wednesday, May 31, 2006

A couple of solutions...

def isPowerOf2(num):
  return num == 1 or num == 2 or (num%2 == 0 and isPowerOf2(num/2))
def isPowerOf2(num):
  count = 0
  while(num > 0):
    count += num%2
    num >>= 1
  return count == 1

Update: From David, in the comments:

def isPowerOf2(num):
  return (num & (num - 1) == 0)

I've never gotten to do enough bit twiddling to have let me think of that.

Daily link icon Friday, March 24, 2006

  1. Here's some quick Perl I used to unescape some form-encoded data:

    while(<>){
        s/\+/ /g;
        s/%([0-9A-Fa-f]{2})/chr(hex($1))/ge;
        print;
    }
       (0) Tags: [Code, Perl]
July 2008
SunMonTueWedThuFriSat
 12345
6789101112
13141516171819
20212223242526
2728293031 



RSS feed RSS feed for Keith's Weblog
Atom feed Atom feed for Keith's Weblog
Weblog archive
Recent comments
  on 5 posts

Recent comments XML

Girls, please don't get breast implants

> And no, you will not be receiving​a picture.

:-(...

Keith: Jul 2, 6:05am

Javascript clone function

This is a clever way to clone an​object if you are using YAHOO UI.​Same tec...

Antonio: Jul 1, 12:47pm

I hate Norton Antivirus

Oh just one other thing norton is​great at keeping people out of your​compu...

kevin.sands: Jul 1, 12:50am

Terminator 3 was awful

I think the biggest reason why T3​totally blew was because Edward​Furlong g...

76.167.172.64: Jun 29, 3:06am

Generated in about 0.131s.

(Used 10 db queries)

mobile phone