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
require 'csv' | |
require 'activerecord-import' | |
file_path = 'caminho/para/seu/arquivo.csv' | |
users_data = [] | |
CSV.foreach(file_path, headers: true) do |row| | |
users_data << User.new(name: row['name'], email: row['email']) | |
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
file_path = 'caminho/para/seu/arquivo.csv' | |
users_data = [] | |
CSV.foreach(file_path, headers: true) do |row| | |
users_data << {name: row['name'], email: row['email']} | |
end | |
User.insert_all(users_data) |
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
# Supondo que o modelo seja User com os campos name e email | |
require 'csv' | |
file_path = 'caminho/para/seu/arquivo.csv' | |
CSV.foreach(file_path, headers: true) do |row| | |
User.create(name: row['name'], email: row['email']) | |
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
def with_logging | |
puts "Iniciando operação…" | |
yield | |
puts "Operação concluída." | |
end | |
with_logging do | |
puts "Realizando a operação…" |
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
require 'forwardable' | |
class Logger | |
extend Forwardable | |
def_delegator :@logger, :log | |
def initialize | |
@logger = Rails.logger | |
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
class Person | |
def method_missing(method_name, *args) | |
puts "Método '#{method_name}' não encontrado." | |
end | |
end | |
person = Person.new | |
person.unknown_method | |
# Retorno: "Método 'unknown_method' não encontrado." |
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
def greet(*people) | |
people.each do |person| | |
puts "Olá, #{person}!" | |
end | |
end | |
greet("Alice", "Bob", "Charlie") | |
# Retorno: | |
# Olá, Alice! |
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
name = "Sávio" | |
age = 54 | |
def greet(name, age) | |
name = "Sr. #{name}" | |
age = "#{age} anos de idade!" | |
puts "Olá, #{name}! Você tem #{age}" | |
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
class Person | |
attr_accessor: name,: age | |
def initialize(name, age) | |
@name = name | |
@age = age | |
end | |
end | |
def greet(person) |
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
def greet(options) | |
name = options[:name] | |
age = options[:age] | |
puts "Olá, #{name}! Você tem #{age} anos." | |
end | |
greet({ name: "Ana", age: 35 }) | |
# Retorno: "Olá, Ana! Você tem 35 anos." |
NewerOlder