Note: This website has been deprecated and is no longer maintained.

Posts tagged with tuesday tricks

Tuesday Tricks - JavaScript Timer Differentials

Timers in JavaScript can be either very simple or become very difficult. In most cases, timers are very simple, such as polling a resource to update content on a SPA (Single-Page Application). In this case, you don't really care when you get your updates from the server, as long as you get them within, say, 1 second of when you expect to receive a response.

But how about when you need a timer to execute at an exact moment in time (ie: a game)? Well, this isn't possible. The best JavaScript can do is guarantee that your function will be run. It doesn't guarantee that it will be run at a specific time. This is because there could be other processes running when you request your timeout, using setTimeout.

When you call setTimeout with a 5000 millisecond delay, it may run in exactly 5000 milliseconds and it may not, depending on what is being processed and what is in the queue. If your setTimeout call gets placed in the queue for 50 milliseconds, your function will be run at 5050 milliseconds. The delay is unpredictable. It's a fact of cyber-life.

Timer Differentials

You can't tell how long your setTimeout call will remain in the queue (and offset your timing), but you can tell how long it remained in the queue after it has been executed. Be careful, it's only supported by Firefox.

setTimeout(function(diff) {
  if( diff > 0 ) {
// function was in the queue
} else if( diff < 0 ) {
// function was called early
} else {
//function was called on time
}, 5000);

This Tuesday's Trick

You probably never develop applications that only run in Firefox, but it's helpful to know that you can make your timing more accurate in the instance the user's browser is Firefox. setTimeout differential will help you do just that.

Posted by Mike Pack on 05/17/2011 at 12:23PM

Tags: tuesday tricks, javascript, timers

Tuesday Tricks - Splatting

In Ruby, the * (asterisk) token is often referred to as the "splat operator". It's purpose is to turn a group of arguments into an array. This can be useful if you want to accept an enumeration to your method but don't care how it's formed. For example:

def note_tasks(*tasks)
  puts "[ ] #{tasks.join(' and ')}"

note_tasks('mow the lawn') #=> [ ] mow the lawn
note_tasks('take out the trash', 'walk the dog') #=> [ ] mow the lawn and walk the dog
note_tasks(['feed yourself', 'get some sleep']) #=> [ ] feed yourself and get some sleep

Splatting in Ruby 1.9

In Ruby 1.8 you were constricted to using the splat operator on the last argument in a method signature. In Ruby 1.9, you can splat anywhere.

def note_task(name, *options, stream)
  $stdout = stream
  puts "#{options.first.to_s}#{name}#{options.last.to_s}"

note_task('mow the lawn', '[ ] ', 'ignored', '!!', $stdout)
#=> [ ] mow the lawn!!

The above method shouldn't normally be defined in such a way. It would make much more sense to define it with stream as the second argument and allow options to be a trailing hash.

def note_task(name, stream, options = {})
  $stdout = stream
  puts "#{options[:before].to_s}Make sure you #{name}#{options[:after].to_s}"

note_task('mow the lawn','/dev/null', 'w'), :before => '[ ] ',
:ignore => 'this',
:after => '!!')
#=> [ ] mow the lawn!! (to /dev/null)
note_task('mow the lawn','/dev/null', 'w'))
#=> mow the lawn (to /dev/null)

Ruby will automatically convert the trailing parameters into a hash. Thanks Ruby.

This Tuesday's Trick

Splatting is fun and useful, but careful, it can sometimes decrease the integrity of your method signature. Mainly use them when you have a trailing enumerable set that can be passed as a list of arguments.

Posted by Mike Pack on 05/10/2011 at 03:55PM

Tags: tuesday tricks, splat, ruby

Tuesday Tricks - Named Regex Groups

New in Ruby 1.9 is the ability to name capture groups so you don't have to use $1, $2...$n. First a demonstration:

Ruby Pre-1.9

regex = /(\w+),(\w+),(\w+)/
"Mike,Pack,Ruby".match regex
"First Name: #$1"
"Last Name: #$2"
"Favorite Language: #$3"

Ruby 1.9.2

regex = /(?<first_name>\w+),(?<last_name>\w+),(?<favorite_language>\w+)/
m = "Mike,Pack,Ruby".match regex
"First Name: #{m[:first_name]}"
"Last Name: #{m[:last_name]}"
"Favorite Language: #{m[:favorite_language]}"

Note: If you use named groups, Ruby won't process unnammed groups. So the following won't work:

regex = /(?<first_name>\w+),(?<last_name>\w+),(?<favorite_language>\w+),(\w+)/
m = "Mike,Pack,Ruby,Colorado".match regex
"First Name: #{m[:first_name]}"
"Last Name: #{m[:last_name]}"
"Favorite Language: #{m[:favorite_language]}"
"Location: #$4"

Note: Even though Ruby won't populate $4, it will still populate $1, $2 and $3.

This Tuesday's Trick

Perl had named regex groups, now Ruby has them. Naming your regex groups can be extremely helpful, especially when the regex becomes complex. Use 'em.

Posted by Mike Pack on 05/03/2011 at 12:58PM

Tags: tuesday tricks, regex, ruby