Skip to content

Instantly share code, notes, and snippets.

@lucasrenan
Created September 15, 2011 01:16
Show Gist options
  • Save lucasrenan/1218288 to your computer and use it in GitHub Desktop.
Save lucasrenan/1218288 to your computer and use it in GitHub Desktop.
exemplos curso ruby fatec so
####################################################
# sintaxe
####################################################
var = "Conteudo"
puts var
puts var.downcase
arr = ["ruby", "rails", "javascript"]
puts arr
if arr.length > 1
puts "mais que uma tecnologia"
end
hash = {:languages => ["ruby", "python", "php"], :databases => ["couchdb", "mongodb", "mysql"]}
puts hash[:languages]
puts hash[:databases]
for i in 1..8 do
puts i
end
def my_method(param1, param2)
puts "esse eh o parametro 1 " + param1
puts "esse eh o parametro 2 #{param2}"
end
my_method("lucas", "o cara!")
####################################################
# classe, objeto
####################################################
class Product
attr_accessor :name
attr_accessor :price
def initialize(category)
@category = category
end
def get_category
@category
end
end
product = Product.new("book")
product.name = "programando ruby"
product.price = 10.50
puts product.get_category
puts product.name
puts product.price
####################################################
# modulo
####################################################
module Store
class Product
attr_accessor :name
end
end
product = Store::Product.new
product.name = "javascript - good parts"
puts product.name
####################################################
# herança
####################################################
class Publication
attr_accessor :name
attr_accessor :price
end
class Book < Publication
attr_accessor :isbn
end
book = Book.new
book.name = "my book"
book.isbn = 123456
puts "#{book.isbn} - #{book.name}"
####################################################
# metodos de classe e instancia
####################################################
class Customer
def initialize(name)
@name = name
end
def name
@name
end
def self.find_all
"voce poderia consultar o banco de dados por exemplo"
end
end
customer = Customer.new("matz")
puts customer.name
puts Customer.find_all
####################################################
# mixin
####################################################
module Authentication
def login
"loga o user"
end
end
class User
include Authentication
attr_accessor :name
end
user = User.new
puts user.login
####################################################
# bloco
####################################################
languages = ["ruby", "python", "c", "java", "php", "c#"]
languages.each do |language|
puts language
end
require "minitest/autorun"
# require "turn"
def sum_two_numbers(num1, num2)
num1 + num2
end
##############
class SumTest < MiniTest::Unit::TestCase
def test_should_sum_two_numbers
assert_equal 3, sum_two_numbers(1, 2)
end
end
require "minitest/autorun"
# require "turn"
class Post
def title=(title)
@title = title
end
def title
@title
end
end
##############
class PostTest < MiniTest::Unit::TestCase
def test_should_initialize_post
post = Post.new
assert post
end
def test_should_set_title
post = Post.new
assert post.title = "title"
end
def test_should_get_title
post = Post.new
post.title = "my title"
assert_equal "my title", post.title
end
end
- acrescentar exemplos de métodos com ! e ?
- unless
@tiagogodinho
Copy link

Ficaram bons os exemplos. Só faltou vc mostrar como sobreescrever um método. Acho isso muito legal no Ruby.

@guivinicius
Copy link

Põe uns exemplos de programas de uma linha em Ruby. É massa e anima o pessoal.

@kssia
Copy link

kssia commented Sep 15, 2011

vdd aquele como fazer um random e pagar numeros pra jogar na mega kkkkkkk

@lucasrenan
Copy link
Author

hash adicionado =)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment