Skip to content

Instantly share code, notes, and snippets.

View lucasdavila's full-sized avatar
🌈

Lucas D'Avila lucasdavila

🌈
  • SC, Brasil
View GitHub Profile
@lucasdavila
lucasdavila / merge_itself_with_simbolized_keys.rb
Created October 15, 2012 13:01
Merge itself with simbolized keys in rails
hash = {'a' => 1, 'b' => 2}
# => {"a"=>1, "b"=>2}
hash.merge hash.symbolize_keys
# => {"a"=>1, "b"=>2, :a=>1, :b=>2}
hash.to_json
# => "{\"a\":1,\"b\":2}"
hash[:a] = 3
@lucasdavila
lucasdavila / 1_ubuntu_terminal_command
Last active February 21, 2024 16:26
Installing Source Code Pro fonts in ubuntu
# to execute this gist, run the line bellow in terminal
\curl -L https://raw.github.com/gist/3875946/install_source_code_pro.sh | sh
@lucasdavila
lucasdavila / nested_attributes_pt-BR.yml
Created September 19, 2012 21:46
Rails I18n for nested attributes
'pt-BR':
activerecord:
attributes:
student:
name: 'Nome'
student_users: 'Usuários'
# ...
student/student_users:
@lucasdavila
lucasdavila / configuring_ubuntu_audio.sh
Created September 19, 2012 18:21
Configuring ubuntu audio / Restarting pulseaudio when flash player crashes.
sudo apt-get install pavucontrol; pavucontrol &;
echo "Go to configuration tab and select the correct profile"
@lucasdavila
lucasdavila / simple_pg_query_for_multitenant_dbs.sh
Created September 13, 2012 20:54
Simple script to run a PostgreSQL query in multitenancy dbs
sql="select 1"
for tenant_name in tenant_1 tenant_2 tenant_3 tenant_4
do
psql -d $tenant_name -c "$sql"
done
@lucasdavila
lucasdavila / dynamic_form_for_hstore_attributes_in_rails.html.erb
Created September 13, 2012 15:54
Dynamic form for hstore attributes in rails
<%= simple_form_for @product do |f| %>
<%= f.simple_fields_for :attributes do |d| %>
<% f.object.attributes.try(:each) do |key, value| %>
<%= d.input key, :input_html => {:value => value } %>
<% end %>
<% end %>
<% end %>
@lucasdavila
lucasdavila / 1_parent.rb
Created September 12, 2012 02:51
Calling a jruby script from a ruby script (via shell)
$jruby_bin = '/path/to/jruby/bin/jruby'
$children_script = File.join File.expand_path('.'), 'children.rb'
def call_children command = ''
full_command = "#{$jruby_bin} -r#{$children_script} -e '#{command}'"
puts "Executing command: #{full_command}"
system full_command
end
puts "Hello from parent!"
@lucasdavila
lucasdavila / 1_without_factory_pattern.java
Created August 31, 2012 19:12
Factory pattern or "convention over ifs"?
// example from book "Head First Design Patterns".
// without factory pattern
public class PizzaStore {
public Pizza orderPizza(String type) {
Pizza pizza;
// bad! we have a lot of ifs here...
if (type.equals("cheese")) {
@lucasdavila
lucasdavila / run_sandbox.sh
Created August 27, 2012 00:18
Simple file system sandbox to jRuby with Java SecurityManager.
# enable security manager checks
export VERIFY_JRUBY="yes"
jruby sandbox.rb
@lucasdavila
lucasdavila / sort_array_by_key.php
Created August 15, 2012 00:40
Sort array by key in php
<?php
// util function to be used by sortArrayByKey function
function _arraySorterByKey($key, $array, $otherArray) {
$a = $array[$key];
$b = $otherArray[$key];
if ($a == $b)
return 0;