George Opritescu

Developer from somewhere

scaloid android scala

A way to write Android apps in Scala: scaloid

Read More

command to delete a lot of unnecessary git branches

Assuming you have a lot of branches, and you only want to keep devel and master:

gb -a | grep -i origin | sed -E "s/^\s+remotes\/origin\///" | grep -vi master | grep -vi devel | while read br; do echo "deleting $br"; git push origin :$br; done
Read More
git

thread abort on exception

It might be useful to enable this while working with threads ( at least in early development phases ):

Thread.abort_on_exception = true
Read More

ruby pg documentation

Some good documentation for the PG driver, available here

Read More

recursively replace with ack-grep and a little help from ruby and xargs

ack-grep is a great tool for finding files containing some strings. It’s easy to combine it’s power, with the in-place editing capabilities of ruby. Here’s how we’d replace the string alert with # alert in all the files within a directory:

ack-grep -lia alert app | xargs ruby -pi -e '$_.gsub!("alert","#alert")'

What’s important to notice here, is that we’re using the l switch for ack, which means “give me only the name of the files containing that string”, and we’re piping each file to ruby’s in place editing. If you’d like to create a backup of the file, run the command like this:

ack-grep -lia alert app | xargs ruby -pi.bak -e '$_.gsub!("alert","#alert")'

This way, you’d also have the old file, in case your replace did not go so well.

Read More
ack

rails listen/notify with postgres

Example of using listen/notify functionality, taken from here

# Be sure to check out a connection, so we stay thread-safe.
ActiveRecord::Base.connection_pool.with_connection do |connection|
  # connection is the ActiveRecord::ConnectionAdapters::PostgreSQLAdapter object
  conn = connection.instance_variable_get(:@connection)
  # conn is the underlying PG::Connection object, and exposes #wait_for_notify

  begin
    conn.async_exec "LISTEN channel1"
    conn.async_exec "LISTEN channel2"

    # This will block until a NOTIFY is issued on one of these two channels.
    conn.wait_for_notify do |channel, pid, payload|
      puts "Received a NOTIFY on channel #{channel}"
      puts "from PG backend #{pid}"
      puts "saying #{payload}"
    end

    # Note that you'll need to call wait_for_notify again if you want to pick
    # up further notifications. This time, bail out if we don't get a
    # notification within half a second.
    conn.wait_for_notify(0.5) do |channel, pid, payload|
      puts "Received a second NOTIFY on channel #{channel}"
      puts "from PG backend #{pid}"
      puts "saying #{payload}"
    end
  ensure
    # Don't want the connection to still be listening once we return
    # it to the pool - could result in weird behavior for the next
    # thread to check it out.
    conn.async_exec "UNLISTEN *"
  end
end

Under ruby 1.9, async_exec is just an alias for exec. Example notify call:

ActiveRecord::Base.connection.execute "notify channel1,'some payload'"
Read More

javascript arguments array

It may be useful to know that each javascript function has access to a variable/property called arguments. It will hold all the arguments that the function was called with. An example where this may prove useful:

  • imagine you’re doing a variable number of ajax requests in the same time, and you need to do something with the results, after they’ve all finished. I’m using the Deferred and Promise objects from jQuery, which I’m resolving once I’ve parsed the response of the server. Typically, the then function would take as many parameters as there were requests … and to write .then (data) it would only pass the first result to the then function. And … this is where arguments comes into play.
urls   = ["url1","url2", ...] # it may have any number
deferreds = urls.map (e) -> 
  dfrd    = $.Deferred()
  $.get e, (server_response) ->
    your_val  = $("some_selector",server_response).html()
    dfrd.resolve(your_val)
  dfrd.promise()

$.when.apply(this, deferreds).then ->
  console.log "All AJAX requests finished"
  console.log arguments
Read More

get shredded split

Fitness posts may pop here from time to time.

150lbs ( ~68 kg )

35% protein = 113g
10-15% carbs = 38g
55-60% fat = 100g

Read More

doing a request with basic auth

1
2
3
4
5
6
7
8
require "net/http"

url = URI.parse('http://localhost:3000/users/1.json')
req = Net::HTTP::Get.new(url.path)
req.basic_auth 'user', 'pass'

resp = Net::HTTP.new(url.host, url.port).start {|http| http.request(req) }
puts resp
Read More

Using underscore js in an erb template

This allows for mustache style templates, interpolation done with and logic with

_.templateSettings = 
  interpolate: /\{\{\=(.+?)\}\}/g
  evaluate: /\{\{(.+?)\}\}/g
Read More