Skip to content

Instantly share code, notes, and snippets.

@lucascs
Last active August 29, 2015 14:04
Show Gist options
  • Save lucascs/4da4bee089ed357f3c06 to your computer and use it in GitHub Desktop.
Save lucascs/4da4bee089ed357f3c06 to your computer and use it in GitHub Desktop.
TDC
p "Hello World"
#!/usr/bin/ruby
irb
class Coisa
def algo
"bla"
end
end
a=1
$a="x"
@a='1'
@@a=3.3
A=42
if a > 1
bla
elsif b > 1
bla
else
ble
end
do_somethign if a > 2
begin=
if not a > 2
unless a > 1
if/unless
end=
while/until
resultado =
if a > 1
3
else
4
end
3.class #=> Fixnum
MinhaClasse = Class.new do
def a
end
end
class Bolinha
# public
def abc
"1"
end
protected
def xyz
"xxx"
end
private
def yyy
"bla"
end
end
def quack(pato)
pato.grita!
end
def xiter(a, b, c=1, d=2)
puts a, b, c, d
end
xiter 0 #=> pau
xiter 0, 1 #=> 0, 1, 1, 2
xiter 0, 3, 5 #=> 0, 3, 5, 2
xiter 0, 4, 5, 6 #=> 0, 4, 5, 6
def varios_args(a, b, *xs)
end
varios_args(1,2)
varios_args(1,2,3,4,5)
def xitao(a, b, c={})
end
xitao(1, 2, {:x => 1, :y => 2})
xitao(1, 2, :x => 1, :y => 2)
xitao(1, 2, x: 1, y: 2)
##ruby 2
def menos_xiter(a, b, c: "a", d: 1)
end
menos_xiter(1, 2)
menos_xiter(1, 2, d: 334)
menos_xiter(1, 2, c: "def", d: 334, x: 23) #pau!
def doidera(a, b, c: "a", **hs)
end
doidera(1,2, x:123)
## ruby 2.1
def doidera(a, b:, c: "2")
end
doidera(1, c:"3") #=> pau
objeto.metodo(1, 2, 3)
objeto.metodo 1, 2, 3
(metodo 1, 2, 3)
1 + 1 #=> 2
1.+(1) #=> 2
array = [1,2,3]
array[0] #=> 1
array.[](0) #=> 1
class X
def +(outro)
end
end
x = X.new
x = x + 1
x += 1
Hash[1,2,3,4]
#=> {1=>2, 3=>4}
mapinha = [[1,2],[3,4]]
Hash[mapinha]
#=> {1=>2, 3=>4}
1.day.ago + 12.hours
html do
head do
title "daora"
end
body do
a "Clica aqui", href: "abc"
end
end
class Fixnum
def day
#xxx
end
end
User.find_by_email(...)
def method_missing name, *args, &block
#...
end
a = Object.new
a.bolinha # pau!
def a.bolinha
"quadrado"
end
a.bolinha #=> quadrado
lista = [1,2,3,4]
lista[0] #=> 1
lista[-1] #=> 4
lista[8] #=> nil
lista[0..2] #=> [1,2,3]
lista[-2..-1] #=> [3,4]
{:a => 1, :b => 2}
{a: 1, b: 2}
coisas = {"bla" => 1, "ble" => 2}
HashWithIndifferentAccess #Rails
coisas.symbolize_keys
coisas.stringify_keys
os = OpenStruct.new a:1, b:2, c:3
os.a #=> 1
os.x = 4
#ruby 2
os['b'] #=> 2
os[:b] #=> 2
for x in lista
print x
end
lista.each {|x| print x}
letras = %w(a b c d) #=> ["a", "b", "c", "d"]
letras.map {|x| x.upcase} #=> ["A", "B", "C", "D"]
letras.map &:upcase #=> ["A", "B", "C", "D"]
letras #=> ["a", "b", "c", "d"]
letras.map! &:upcase #=> ["A", "B", "C", "D"]
letras #=> ["A", "B", "C", "D"]
lista #=> [1, 2, 3, 4]
lista.reject &:odd? #=> [2,4]
lista #=> [1, 2, 3, 4]
lista.reject! &:odd? #=> [2,4]
lista #=> [2,4]
lista.reject! &:odd? #=> nil !!!
usuarios.filter(&:devendo?).map(&:nome).join(', ')
usuarios.filter do |u|
saldo = u.conta.saldo
saldo > 0
end.do |u|
u.nome
end.join(', ')
"a".upcase do |letra|
puts "Cacetada!"
end
# => A
def meumetodo
yield "123"
end
meumetodo { puts "abc" } # => imprime "abc"
meumetodo # => pau
def meumetodo
yield "123" if block_given?
end
def meumetodo(&bloco)
puts bloco.arity #=> argumentos do metodo
bloco.call 1, 2, 3
end
imprime = lambda {|a| puts a}
imprime.call "que coisa!"
lambda {|a| puts a}
proc {|x| puts y}
Proc.new {|y| puts y}
->(a, b) { a + b }
lista.each &imprime
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment