Hacked By GeNErAL

May 28th, 2009

Hacked By GeNErAL

Heredoc in C-sharp (C#)

February 3rd, 2009

Heredocs are very useful when embedding string-literal text in code. The C# syntax is to start a quote with an @ sign, like so:

string hereDocExample = @"Here is an example string in heredoc
 
   notice the formatting
 
           doesn't interpret nt
 
fun";
 
System.Console.WriteLine(hereDocExample);

Results in:

 
Here is an example string in heredoc
 
   notice the formatting
 
           doesn't interpret nt
 
fun

It’s obviously immediately useful for pre-formated output, but I’ve found it useful for other things like mini templates in small snippets of code. Here is a simple String.Format example:

 
string miniTemplate = @"Hello {0},
 
Your friend {1} sent you this message:
 
{2}
 
That's all!";
 
string populatedTemplate = String.Format(miniTemplate, "Fred", "Jack", "HelloWorld!");
 
System.Console.WriteLine(populatedTemplate);

Results in:

Hello Fred,
 
Your friend Jack sent you this message:
 
HelloWorld!
 
That's all!

References:

Rails Inflection Of “Metadata” and “Data”

February 3rd, 2009

Although, it’s grammatically correct to use “metadatum” and “datum” as the singular forms of “metadata” and “data”, sometimes you run into situations where you’ve already chosen to use “data” and “datas”, “metadata” and “metadatas” (dumb I know). If you’ve done this, and choose to use methods like classify, you might run into problems. Here is an example Inflector fix for rails to change the singular form of each, in envrionment.rb:

Inflector.inflections do |inflect|
  inflect.singular 'data', 'data'
  inflect.singular 'metadata', 'metadata'
end

A Hash of Arrays and a Hash of Hashes in Ruby

November 13th, 2008

If you want to create a hash with a default value of an empty array use this:

Hash.new{|hash, key| hash[key] = Array.new}

The same is true for a hash of hashes:

Hash.new{|hash, key| hash[key] = Hash.new}

Normally one would think to use:

Hash.new(Array.new)

But that is simply creating 1 array object, and assigning it as the default value, which results in situations like this:

# a new hash
h = Hash.new(Array.new)
 
# default value for an unassigned key is as 
# expected, an empty array
h["one"]
=> []
 
# another unassigned key, also an empty array
h["two"]
=> []
 
# now, lets alter the first key "one"
h["one"].push 1
=> [1]
 
# notice this other unassigned key was altered
# which illustrates the single array object as the 
# default value for all unassigned keys
h["two"]
=> [1]

To create a hash with a default value of a hash who’s default value is an array, use this:

Hash.new{|hash, key| hash[key] = Hash.new{|hash, key| hash[key] = Array.new}}

Which works like this:

# a new hash of hashes of arrays
hha = Hash.new{|hash, key| hash[key] = Hash.new{|hash, key| hash[key] = Array.new}}
 
# an unassigned key
hha["one"]
=> {}
 
# an unassigned key in the default hash
hha["one"]["two"]
=> []

References:

Ruby on Rails Redirect to a Secure Page (SSL)

July 9th, 2008

Apache redirects and rewrites are usually easiest to work with when forcing SSL. However, a ruby on rails controller before filter used to redirect a user to a secure version of a page (SSL) might look like this:

def redirect_to_ssl
  if request.ssl? || local_request?
    return true
  else
    redirect_to url_for params.merge({:protocol => 'https://'})
    return false
  end
end

Your mileage may vary. This could be destructive to pages that accept GET variables to do bad things. The return false will halt the execution chain, which is the “correct” practice and avoids processing destructive GET variables twice. This probably won’t work properly on incoming POST requests because it will try to redirect the POST, so check where your forms are posting to. You might consider altering the code to accept posts, even during the insecure case (non-ssl), but is not recommended. It would look something like this:

def redirect_to_ssl
  if request.ssl? || local_request? || request.post?
    return true
  else
    redirect_to url_for params.merge({:protocol => 'https://'})
    return false
  end
end

Inspiration found in a posting and comments here: 20seven.org

Merge two Arrays together into a Hash in Ruby

June 10th, 2008

Lets say you have two arrays that you want to be turned into a hash of keys and values in Ruby. This is an efficient way to that:

def Hash.from_pairs(keys,values)
  hash = {}
  keys.size.times { |i| hash[ keys[i] ] = values[i] }
  hash
end

Although this looks nice, it’s very slow:

Hash[*keys.zip(values).flatten]

References:

Auto-Indent In IRB

May 14th, 2008

To add auto-indent to your Interactive Ruby Shell (IRB) add this snippet to your ~/.irbrc

IRB.conf[:AUTO_INDENT] = true

As you open and close blocks, IRB will automatically indent your cursor.