# Anchor
click_link 'Save'
# Button
click_button 'awesome'
# Both above| hash = { 'foo' => 'bar' } | |
| # Version 1 | |
| hash = Hash[hash.map { |k, v| [k.to_sym, v] }] | |
| # Version 2 | |
| hash = hash.reduce({}) do |memo, (k, v)| | |
| memo.tap { |m| m[k.to_sym] = v } | |
| end |
| #Don’t Repeat Yourself (DRY) in Ruby on Rails | |
| #DRY (Don’t Repeat Yourself) is a principle of Software Development to reducing repetition of information or codes. We can #apply DRY quite broadly to database schema, test plan, system, even documentation. And in this post, we will take example of DRY #in Ruby on Rails development. | |
| #In particular case, if you find some methods whose definitions are more or less similar, only different by the method name, it #may use meta programming to simplify the things to make your model more clean and DRY. Consider this simple example where we #have an article with three states. | |
| #Before | |
| class Article < ActiveRecord::Base |
| def paginate(scope, default_per_page = 20) | |
| collection = scope.page(params[:page]).per((params[:per_page] || default_per_page).to_i) | |
| current, total, per_page = collection.current_page, collection.num_pages, collection.limit_value | |
| return [{ | |
| pagination: { | |
| current: current, | |
| previous: (current > 1 ? (current - 1) : nil), | |
| next: (current == total ? nil : (current + 1)), |
| module ControllerMacros | |
| def login_user | |
| before(:each) do | |
| @request.env["devise.mapping"] = Devise.mappings[:user] | |
| user = FactoryGirl.create(:user) | |
| user.confirm! | |
| sign_in user | |
| end | |
| end | |
| end |
| 2.hours.ago # => Fri, 02 Mar 2012 20:04:47 JST +09:00 | |
| 1.day.from_now # => Fri, 03 Mar 2012 22:04:47 JST +09:00 | |
| Date.today.to_time_in_current_zone # => Fri, 02 Mar 2012 22:04:47 JST +09:00 | |
| Date.current # => Fri, 02 Mar | |
| Time.zone.parse("2012-03-02 16:05:37") # => Fri, 02 Mar 2012 16:05:37 JST +09:00 | |
| Time.zone.now # => Fri, 02 Mar 2012 22:04:47 JST +09:00 | |
| Time.current # Same thing but shorter. (Thank you Lukas Sarnacki pointing this out.) | |
| Time.zone.today # If you really can't have a Time or DateTime for some reason | |
| Time.zone.now.utc.iso8601 # When supliyng an API (you can actually skip .zone here, but I find it better to always use it, than miss it when it's needed) | |
| Time.strptime(time_string, '%Y-%m-%dT%H:%M:%S%z').in_time_zone(Time.zone) # If you can't use Time#parse |
A primeira coisa a saber sobre a tipagem do PHP é que ela não é parecida com Java, JavaScript, Python, Ruby, C, C++, C# ou qualquer linguagem que tenha uma tipagem baseada em alguma dessas citadas. A tipagem do PHP é incomparável, e assim como tudo que não pode ser comparado é difícil de ser explicada.
De qualquer forma, a tipagem do PHP é extremamente simples se você apenas confiar na sua intuição. A primeira coisa que você tem que saber sobre a tipagem do PHP é que ela faz malabarismos. É exatamente essa a palavra: malabarismo. E o PHP é um ótimo malabarista, exceto por alguns poucos deslizes fáceis de decorar. O type juggling, traduzido para "malabarismo com tipos" é a habilidade que o PHP tem de tomar decisões intuitivas sobre conversões de tipos. Em termos grosseiros, o PHP decide toda e qualquer tipagem de variáveis em tempo de execução, não compilação (pros pedante aí que tão lendo).
Note bem: tipagem de variáveis. O PHP é multi-paradigma e, ao m
| // MIT license (c) andelf 2013 | |
| import ( | |
| "net/smtp" | |
| "errors" | |
| ) | |
| type loginAuth struct { | |
| username, password string |
É um design pattern que o Rails implementa a partir da gem ActiveRecord.
Serve para conectar a camada Model da aplicação com tabelas do database, para assim criar um modelo de domínio persistível, onde a lógica (Model) e dados (BD) são apresentados em uma única solução.
Já persiste no BD:
obj.create
| #!/usr/bin/env ruby | |
| require 'rubygems' | |
| @dir = ARGV[0] | |
| @i = 0 | |
| Dir.glob("#{@dir}/*.mp3").each do |mp3| | |
| ogg = mp3.gsub('mp3', 'ogg') | |
| system "ffmpeg -i \"#{mp3}\" -acodec libvorbis -ac 2 \"#{ogg}\"" | |
| @i += 1 | |
| end |