idioms

ruby idioms for using command-line options

I'm trying to pick up ruby by porting a medium-sized (non-OO) perl program. One of my personal idioms is to set options like this: use Getopt::Std; our $opt_v; # be verbose getopts('v'); # and later ... $opt_v && print "something interesting\n"; In perl, I kind of grit my teeth and let $opt_v be (effectively) a global. In ruby,the ...

Is there a tutorial that teaches common Ruby programming idioms used by experienced programmers, but may not be obvious to newcomers?

I'm looking for a Ruby's equivalent of Code Like a Pythonista: Idiomatic Python Desirable features: easy to read single document which covers all topics: tips, tricks, guidelines, caveats, and pitfalls size less than a book idioms should work out of the box for the standard distribution (% sudo apt-get install ruby irb rdoc) Please,...

Practical Uses for the "Curiously Recurring Template Pattern"

What are some practical uses for the "Curiously Recurring Template Pattern"? The "counted class" example commonly shown just isn't a convincing example to me. ...

How do you write a (simple) variable "toggle"?

Given the following idioms: 1) variable = value1 if condition variable = value2 2) variable = value2 if not condition variable = value1 3) if condition variable = value2 else variable = value1 4) if not condition variable = value1 else variable = value2 Which do you prefer, and why? We assume the most common execut...

Hashes of Hashes Idiom in Ruby?

Creating hashes of hashes in Ruby allows for convenient two (or more) dimensional lookups. However, when inserting one must always check if the first index already exists in the hash. For example: h = Hash.new h['x'] = Hash.new if not h.key?('x') h['x']['y'] = value_to_insert It would be preferable to do the following where the new H...

Is there a downside to adding an anonymous empty delegate on event declaration?

I have seen a few mentions of this idiom (including on SO): // Deliberately empty subscriber public event EventHandler AskQuestion = delegate {}; The upside is clear - it avoids the need to check for null before raising the event. However, I am keen to understand if there are any downsides. For example, is it something that is in wi...

Best ruby idiom for "nil or zero"

I am looking for a concise way to check a value to see if it is nil or zero. Currently I am doing something like: if (!val || val == 0) # Is nil or zero end But this seems very clumsy. ...

Checking lists and running handlers

I find myself writing code that looks like this a lot: set<int> affected_items; while (string code = GetKeyCodeFromSomewhere()) { if (code == "some constant" || code == "some other constant") { affected_items.insert(some_constant_id); } else if (code == "yet another constant" || code == "the constant I didn't mention yet...

Has .NET removed the distinction between various languages?

We have always had languages that were preferable to be used in a particular scenario. For a quick prototype development, VB6 was an obvious choice. VB6 was chosen in projects that had a simple desktop user interface and standard and un-complicated database interaction requirements. If you wanted to develop a device driver using low-leve...

Script a ruby command-line app; best way to do this?

I have a command line Ruby app I'm developing and I want to allow a user of it to provide code that will run as a filter on part of the process. Basically, the application does this: read in some data If a filter is specified, use it to filter data process the data I want the filtering process (step 2) to be as flexible as possible....

Parallel Programming and C++

I've been writing a lot recently about Parallel computing and programming and I do notice that there are a lot of patterns that come up when it comes to parallel computing. Noting that Microsoft already has released a library along with the Microsoft Visual C++ 2010 Community Technical Preview (named Parallel Patterns Library) I'm wonder...

What are some C++ related idioms, misconceptions, and gotchas that you've learnt from experience?

What are some C++ related idioms, misconceptions, and gotchas that you've learnt from experience? An example: class A { public: char s[1024]; char *p; A::A() { p = s; } void changeS() const { p[0] = 'a'; } }; Even know changeS is a const member function, it is changing the value of the object. So a cons...

What is a programming idiom?

I see the phrase "programming idiom" thrown around as if it is commonly understood. Yet, in search results and stackoverflow I see everything... From micro: Incrementing a variable Representing an infinite loop Swapping variable values To medium: PIMPL RAII Format, comments, style... To macro: Programming paradigm or common li...

using a vector of column names, to generate a sql statement.

A problem that we need to solve regularly at my workplace is how to build sql statements based on user supplied table/column names. The issue I am trying to address is the commas between column names. One technique looks something like this. selectSql = "SELECT "; for (z = 0; z < columns.size(); z++) { selectSql += columns[z]....

An interesting C linked list idiom

I was at an interview for a C position in which they presented me with an idiom that I haven't previously encountered. This is a trick that simplifies implementation of various algorithms involving linked lists and I'm wondering if anybody else has encountered this. Say we have a linked list record defined so: typedef struct _record { ...

What is the "Execute Around" idiom?

What is this "Execute Around" idiom (or similar) I've been hearing about? Why might I use it, and why might I not want to use it? ...

Java String Parameters

I'm coming from a .net background and want to know the accepted way of creating a method that returns a boolean and modifies a string that was passed in via parameter. I understand Strings are immutable in Java so the below snippet will always produce an empty string. I am constrained to return boolean only. Exceptions can't be thrown. I...

Most pythonic form for mapping a series of statements?

This is something that has bugged me for some time. I learnt Haskell before I learnt Python, so I've always been fond of thinking of many computations as a mapping onto a list. This is beautifully expressed by a list comprehension (I'm giving the pythonic version here): result = [ f(x) for x in list ] In many cases though, we want to ...

Python idiom to return first item or None

I'm sure there's a simpler way of doing this that's just not occurring to me. I'm calling a bunch of methods that return a list. The list may be empty. If the list is non-empty, I want to return the first item; otherwise, I want to return None. This code works: list = get_list() if len(list) > 0: return list[0] return None It seem...

What's the most pythonic way of testing that inputs are well-formed numbers

I have a function that expects real numbers (either integers or floats) as its input, and I'm trying to validate this input before doing mathematical operations on it. My first instinct is to cast inputs as floats from within a try-except block. try: myinput = float(input) except: raise ValueError("input is not a well-formed numb...