Last active
December 12, 2015 05:29
-
-
Save marioplumbarius/4722497 to your computer and use it in GitHub Desktop.
Anotações e informações básicas que agrupei sobre a linguagem de programação Ruby.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# if/else | |
if x > y | |
print "x é maior que y" | |
elsif x < z | |
print "x é menor que y" | |
else | |
print "valor padrão" | |
end | |
# unless | |
hungry = false | |
unless hungry = false | |
puts "I'm writing Ruby programs!" | |
else | |
puts "Time to eat!" | |
end | |
# operators | |
# comparisons | |
is_true = 2 != 3 | |
is_false = 2 == 3 | |
x = 2 > -1 | |
y = 3 >= 3 | |
z = 3 < 2 | |
# logical & boolean | |
im_true = true | |
im_not_true = false | |
you_and_me = 2 ** 2 == 4 && 3 ** 2 == 9 | |
you_or_me = 2 ** 2 == 4 || 3 ** 2 == 12 | |
not_true = !(2 ** 2 == 4) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# arrays | |
int_array = [1, 2, 3] | |
str_array = ["mario", "luan"] | |
multidimensional_array = [["name", "surname"], ["mario", "luan"]] | |
# hash arrays | |
# literal notation | |
person_data = {"name" => "mario", "age" => 22, "hungry?" => true} | |
# constructor like | |
person_data = Hash.new | |
person_data["name"] = "mario" | |
person_data["age"] = 22 | |
person_data["hungry?"] = true | |
# iterating | |
# array com apenas 1 dimensão | |
languages = ["HTML", "CSS", "JavaScript", "Python", "Ruby"] | |
languages.each do |x| | |
puts x | |
end | |
# arrays multidimensionais | |
languages = [["HTML", "CSS"], ["PHP", "Python"], ["Wordpress", "Blogger"]] | |
languages.each do |x| | |
x.each do |y| | |
puts y | |
end | |
end | |
# hashes | |
technologies = {"HTML" => "web development", | |
"PHP" => "server-side", | |
"JavaScript" => "client-side"} | |
technologies.each do |x, y| | |
puts "#{x}: #{y}" | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# características da linguagem | |
x = "interpretada" | |
y = "orientada a objetos" | |
z = "desenvolvimento web" | |
# tipos de dados | |
x = 1 | |
y = "one" | |
z = true | |
# operadores aritméticos | |
x = 3 ** 2 | |
x = 3 % 2 | |
x = 3 / 2 | |
x = 3 * 2 | |
x = 3 + 2 | |
x = 3 - 2 | |
# printing | |
print "Hello world" # imprime a string | |
puts "Hello world" # imprime a string e pula uma linha | |
# input & output | |
# gets: método que pega o input do usuário | |
print("What's your name?") | |
my_name_1 = gets # armazena o último input em uma variável e 'pula uma linha' | |
# chomp: remove 'pulos de linha' | |
print("What's your name?") | |
my_name_2 = gets.chomp | |
# string interpolation / printando uma variável | |
print("Your name is #{my_name_1} or my name is #{my_name_2}") | |
# Comments | |
# single line comment | |
=begin | |
mutiple | |
line | |
comment | |
=end | |
# Strings Methods | |
"mario".length | |
"mario".reverse | |
"mario".upcase | |
"MARIO".downcase | |
"mario".capitalize | |
# caracteres especiais | |
=begin | |
! = modifica a variável | |
? = retorna true ou false | |
1..10 = de 1 a 10 | |
1...10 = de 1 a 9 | |
=end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# while | |
i = 1 | |
while i < 5 | |
puts i += 1 | |
end | |
# until | |
i = 5 | |
until i < 1 | |
puts i -= 1 | |
end | |
# for | |
for x in 1..10 | |
puts x | |
end | |
# iterator methods | |
# loop | |
i = 20 | |
loop do | |
i -= 1 | |
next if i % 2 == 1 # 'next' é equivalente a usar 'continue' em php | |
print i | |
break if i <= 0 | |
end | |
# each | |
my_array = [1, 2, 3] | |
my_array.each do |x| # a variável x que está entre as barras, é apenas um placeholder para cada elemento do array | |
print x += 10 | |
end | |
# times | |
5.times do | |
print "wow" | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# variaveis | |
text = "essa é a letra x e esse é o y" | |
array = [["mario", 22], ["joao", 15], ["alberto", 40]] | |
number = 22 | |
# métodos prontos / built-in | |
text.include? "x" # procura por x | |
text.gsub!(/x/, "y") # substitui x por y | |
array = array.sort_by {|x, y| y} # ordena os dados x e y em ordem crescente em relação a y | |
array.reverse! # inverte a ordenação dos dados | |
number.to_s # converte o dado em string | |
# criando um método | |
# sem argumentos | |
def metodo_sem_argumentos | |
# do something | |
end | |
# com argumentos | |
def metodo_com_argumentos(arg1, arg2, ..., argn10) | |
# do something | |
end | |
# chamando | |
metodo_sem_argumentos | |
metodo_com_argumentos(1, 2, 3) | |
# splat arguments = utilizado para um número infinito de argumentos | |
def favourite_color(*color) | |
color.each do |x| | |
puts x | |
end | |
end | |
favourite_color("blue", "yellow", "red") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment