elegant

accessing third-party ruby library module in code? is this the right way on windows?

I needed a little script to read data out of windows-style .ini files. Searching my windows machine I found inifile.rb in this path: C:\ruby\lib\ruby\site_ruby\1.8\vr\contrib\ it seems unnatural to add this path directly to my rubylib path as this will mean any other special units I want will need to be added. so in my code I wrote...

Python - temporarily modify the current process's environment

I use the following code to temporarily modify environment variables. @contextmanager def _setenv(**mapping): """``with`` context to temporarily modify the environment variables""" backup_values = {} backup_remove = set() for key, value in mapping.items(): if key in os.environ: backup_values[key] = o...

doctrine regenerating models from yml only the base models?

I am wondering if there is a way to handle this more elegantly. After generating the "main" models and base models from yml files the first time I have to add at the very leased an include for the base model to the "main" model like so: include_once 'generated/BaseBlog.php'; At the moment before I regenerate the models I move my chan...

Is there a more elegant solution than an if-statement with no else clause?

In the following code, if Control (the element that trigers Toggle's first OL) is not Visible it should be set Visible and all other Controls (Controls[i]) so be Hidden. .js function Toggle(Control){ var Controls=document.getElementsByTagName("ol",document.getElementById("Quote_App")); var Control=Control.getElementsByTagName("...

Boost's "cstdint" Usage

Boost's C99 stdint implementation is awfully handy. One thing bugs me, though. They dump all of their typedefs into the boost namespace. This leaves me with three choices when using this facility: Use "using namespace boost" Use "using boost::[u]<type><width>_t" Explicitly refer to the target type with the boost:: prefix; e.g., boost::...

Ruby elegant way to deal card array to player objects

I'm having trouble finding a nice elegant ruby way to do this. I have a deck array with 52 Card objects, and I want to iterate over it and loop through an array of Player objects, dealing one card at a time into their hand array. Something like: deck = Card.deck.shuffle! deck.each do |card| @players.NEXTELEMENT.hand << card end wh...

Please recommend a smart, elegant way to write these repetitive 'if' statements in php

Hi PHP users, I just know i'm doing this in really bad way: $url = wp_get_referer(); $path_parts = pathinfo($url); $mycat = $path_parts['filename']; if ( $mycat == "animation" ) { $_SESSION["theCategory"] = $mycat;?> <a href="<?php bloginfo('home')?>/c...

Elegant Python?

I am trying to teach myself Python, and I have realized that the only way I really learn stuff is by reading the actual programs. Tutorials/manuals just cause me to feel deeply confused. It's just my learning style, and I'm like that with everything I've studied (including natural languages -- I've managed to teach myself three of them...

List comprehension for running total

I want to get a running total from a list of numbers. For demo purposes, I start with a sequential list of numbers using range a = range(20) runningTotal = [] for n in range(len(a)): new = runningTotal[n-1] + a[n] if n > 0 else a[n] runningTotal.append(new) # This one is a syntax error # runningTotal = [a[n] for n in range(le...

How to write an elegant collision handling mechanism?

I'm in a bit of a pickle: say I'm making a simple, 2D, Zelda-like game. When two Objects collide, each should have a resulting action. However, when the main character collides with something, his reaction depends solely on the type of the object with which he collided. If it's a monster, he should bounce back, if it's a wall, nothing sh...

Pass a value between views

I have a list of items of the same type (let's say guests) and i display them in a listview. By clicking on the line I would be able to edit the single element. All the elements have the same editing mask, so i would like to call the same page and pass a parameter. The only solution that i found it's: <a href="edit-page" onClick="setPar...