Skip to content

Instantly share code, notes, and snippets.

View filipechagas's full-sized avatar
🏖️
OOO

Filipe Chagas filipechagas

🏖️
OOO
View GitHub Profile
fonte: http://www.akitaonrails.com/2008/11/10/micro-tutorial-de-ruby-parte-ii
contents = File.open(aFile).readlines.inject("") do |buf, line|
buf += line
end
O método ‘readlines’ devolve um Array, onde cada elemento é uma linha do arquivo texto. O método “inject” é um Redutor: ele pega linha a linha do Array e repassa ao bloco, como primeiro parâmetro. O segundo parâmetro, ‘buf’, é um totalizador que é iniciado com o primeiro parâmetro que passamos no método ‘inject’, no caso a string vazia “". Ele repassa sempre esse objeto como segundo parâmetro do bloco. Dentro do bloco podemos fazer o que quiser, mas normalmente queremos que seja um totalizador por isso usamos o operador "+=” que significa “buf = buf + line”.
Em Ruby é muito comum utilizar essa maneira de pensar: em vez de pensar em “como vamos iterar elemento a elemento”, partimos do princípio que isso é trivial e daí pensamos “como queremos filtrar elemento a elemento”. Linhas como a seguinte são bastante comuns:
>> [1,2,3,4,5].map { |elem| elem *
@filipechagas
filipechagas / .bash_aliases
Created January 18, 2011 21:08
Solving the $PATH problem with sudo
#add this line to ~/.bash_aliases
alias sudo='sudo env PATH=$PATH'
@filipechagas
filipechagas / gist:1106772
Created July 26, 2011 13:35
Linux/Debian - List the installed softs
dpkg --get-selections > installed_soft.txt
@filipechagas
filipechagas / gist:1181293
Created August 30, 2011 16:26 — forked from viniciusteles/gist:556029
Sete Atitudes para Hackear a Indústria de Software
Sete Atitudes para Hackear a Indústria de Software
By Klaus Wuestefeld
1) Torne-se excelente.
Seja realmente bom em alguma coisa. Não fique só choramingando ou
querendo progredir às custas dos outros. Não pense q pq vc sentou 4
anos numa faculdade ouvindo um professor falar sobre software q vc
sabe alguma coisa. Jogador de futebol não aprende a jogar bola tendo
@filipechagas
filipechagas / active_admin_views_pages_base.rb
Created September 15, 2011 22:07 — forked from jocubeit/active_admin_views_pages_base.rb
Override footer content in Active Admin gem
# lib/active_admin_views_pages_base.rb
class ActiveAdmin::Views::Pages::Base < Arbre::HTML::Document
private
# Renders the content for the footer
def build_footer
div :id => "footer" do
para "Copyright &copy; #{Date.today.year.to_s} #{link_to('Example.com', 'http://example.com')}. Powered by #{link_to('Active Admin', 'http://www.activeadmin.info')} #{ActiveAdmin::VERSION}".html_safe
@filipechagas
filipechagas / command-t
Created January 12, 2012 22:45
Solve to command-T problem - command-t.vim could not load the C extension
#using ruby 1.8
cd ~/.vim/ruby/command-t
make clean
env ARCHFLAGS="-arch x86_64" ruby extconf.rb
env ARCHFLAGS="-arch x86_64" make
@filipechagas
filipechagas / gist:1659692
Created January 23, 2012 00:51
Fix "Invalid gemspec date format" problem
(Insinde the gemspecs directory - $RUBYHOME/lib/ruby/gems/1.8/specifications)
Finding the problematics gemspecs:
grep -i *.gemspec -e '.*s\.date.*=.*%q{\(....-..-..\) \(.*Z\)}
Fixing the files:
sed -i -e 's/\(.*\)s\.date.*=.*%q{\(....-..-..\) \(.*Z\)}/\1s.date = %q\{\2}/p' ./*.gemspec
@filipechagas
filipechagas / gist:1950201
Last active October 1, 2015 07:58
Install ruby-debug-base19 - rbenv - ruby 1.9.2 p290 - ubuntu 64
If you're using rvm:
$ rvm reinstall 1.9.3 --force-autoconf
$ gem install ruby-debug19 -- --with-ruby-include="${MY_RUBY_HOME/rubies/src}"
$ gem install debugger-linecache -- --with-ruby-include="${MY_RUBY_HOME/rubies/src}"
#Using rbenv and ruby 1.9.2 p290 on ubuntu 64
Download the following files from http://rubyforge.org/frs/?group_id=8883
@filipechagas
filipechagas / gist:2287465
Created April 2, 2012 21:47
Getting Ack to Work in Vim on Ubuntu
#source: http://seechrisblog.com/2010/05/18/getting-ack-to-work-in-vim-on-ubuntu/#comment-95
sudo ln -s /usr/bin/ack-grep /usr/local/bin/ack
#source: http://mislav.uniqpath.com/2010/07/git-tips/
Show branches, tags in git log
$ git log --oneline --decorate
7466000 (HEAD, mislav/master, mislav) fix test that fails if current dir is not "hub"
494a414 fix cherry-pick of a commit URL
4277848 (origin/master, origin/HEAD, master) whoops
d270fae bugfix: git init -g
9307af3 test deps