Because of numbers are doubles in Javascript, when yo do this:
> db.test.insert({name: ‘foo’, value: 1});
you get this:
>db.test.findOne({name: ’foo’});
{‘name’: ‘foo’, ‘value’: 1.0}
To avoid this you can use NumberInt or NumberLong:
> db.test.insert({name: ‘foo’, value: NumberLong(1)});
More than one year ago, I created a very simple script that enable Sublime Text 2 to automatically check the syntax of ruby files when they are saved
Three months ago I finally found the time to convert that script into my first Sublime Text 2 plugin: the RubyCheckOnSave plugin.
To remove all gems from a gemset you use the gemset empty command:
$ rvm gemset empty <my_gemset_name>
And to remove the gems from default gemset, you must supply an empty string as the gemset name:
$ rvm gemset empty ""
cUrl is a simple and powerful tool to test RESTful APIs, but sometimes the output could be messy, specially when the response is a long JSON.
To improve the output readability, I’m using cUrl with jsontool (a node.js script).
After you install it:
$ npm install -g jsontool
Then pipe the cUrl output to jsontool
$ curl -is http://search.twitter.com/search.json\?q\=foo | json -i
Below an example of the output
You need to remove the submodule and re-add the files into the main repo.
Edit the .gitmodules and .git/config files and remove the lines that specify the submodule, then;
git rm --cached path/to/submodule # delete reference to submodule HEAD (no trailing slash) rm -rf path/to/submodule/.git git add path/to/submodule # add files instead of commit reference git commit -m "remove submodule"
Today I tried to deploy to heroku a new version of an application (that not has been deployed in several months):
git push heroku master
but I got the following error:
Permission denied (publickey). fatal: The remote end hung up unexpectedly
The weird part is that I can deploy other 3 apps without problem, that means that my public SSH key is properly stored on Heroku.
If the Heroku api is experiencing downtime, you will get this error when you try to push. So, I checked https://status.heroku.com/ and everything seemed to be fine on Heroku’s end.
Then I checked my git remotes, and my problem was that the remote heroku repository was corrupted. I refreshed it as follows:
git remote -v
Then removed the heroku one that is wrong:
git remote rm heroku
Then added the new one
git remote add heroku git@heroku.com:myapp.git
I have an Enumarable of tokens, and I need to sort them descending by the last_access attribute, but for some objects this attribute could be nil. One OOP approach to do it, is defining the <=> method (Comparable) in the Token class:
class Token
include Comparable
define <=> other
...
end
end
Then I’ll only need to call tokens.sort. But I’m not ready to change the behavior of the Token class in that way, instead of that I prefer to follow an approach that don’t have side effects. So, an easy hack is to use an immediate-if like this:
tokens.sort{|a,b|
(a.last_access && b.last_access) ?
a.last_access <=> b.last_access :
(a.last_access ? 1 : -1)
}.reverse!
We have a db collection that is around 30 million documents, and I need to trim it down, to only keeping the documents created on the last month.
One approach would be use the remove command with a condition on the created_at field (the collection already have an index on this field):
db.my_collection.remove({created_at: {$lte: new Date("11/01/2012")}});
But this approach will be very slow, instead of that, a better way to do it is rename the current collection (for instance to “old_collection”) using renameCollection. Then performing a query-and-insert from the “old_collection” into “my_collection”:
db.my_collection.renameCollection("old_collection");
db.createCollection("my_collection");
db.my_collection.createIndex(...); // recreate the indexes for the collection
// copy docs from old collection into the new collection
db.old_collection.find(
{created_at: {$gte: new Date("11/01/2012")}
).sort({_id: -1}).forEach(
function(row) { db.my_collection.insert(row); }
);
// drop old collection
db.old_collection.drop();
This approach is typically faster than running a bunch of removes on your data
We live in a world where there is more and more information, and less and less meaning — Jean Baudrillard
First you set up your staging app following the Heroku’s guide for Managing Multiple Environments for an App
Then edit the config/environments/staging.rb:
#config/environments/staging.rb
MyApp::Application.configure do
# Basic authentication
config.middleware.insert_after(::Rack::Lock, "::Rack::Auth::Basic", "My App") do |u, p|
[u, p] == [ENV['HTTP_USER'], ENV['HTTP_PASSWORD']]
end
...
And finally set the config vars:
heroku config:add HTTP_USER='foo' HTTP_PASSWORD='bar' -a myapp-staging