Skip to content

Instantly share code, notes, and snippets.

@prodis
Created Jul 18, 2011
Embed
What would you like to do?
Ruby Fundamental - Hash em Ruby (parte I)
even_numbers = {} # Cria um objeto hash vazio
even_numbers["zero"] = 0 # Atribui 0 como valor para a chave "zero"
even_numbers["two"] = 2
even_numbers["four"] = 4
even_numbers["two"] # Recupera 2 como valor
even_numbers = {}
even_numbers[:zero] = 0
even_numbers[:two] = 2
even_numbers[:four] = 4
even_numbers = { :zero => 0, :two => 2, :four => 4 }
even_numbers = { zero: 0, two: 2, four: 4 }
misc_hash = {
:string => "Some text",
:number => 456,
:boolean => false,
:array => [0, 1, 2]
}
even_numbers = { :zero => 0, :two => 2, :four => 4 }
even_number.each do |key, value|
print "|#{key}: #{value}| "
end
# Imprime "|zero: 0| |two: 2| |four: 4| "
even_numbers = { :zero => 0, :two => 2, :four => 4 }
even_number.each_key { |key| print "#{key} " }
# Imprime "zero two four "
even_number.each_value { |value| print "#{value} " }
# Imprime "0 2 4 "
class OrdersController < ApplicationController
def payment
@order = Order.find_by_number params[:id]
@payment_method = PaymentMethod.find params[:payment_method_id]
# Alguma implementação
end
# Mais ações
end
class ShippingCalculator
def calculate(weight, height, width, depth)
# Alguma implementação
end
end
class ShippingCalculator
def calculate(weight, dimensions = {})
# Alguma implementação
end
end
dimensions = { :height => 0.75, :width => 1.00, :depth => 0.10 }
calc = ShippingCalculator.new
calc.calculate 1.50, dimensions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment