Skip to content

Instantly share code, notes, and snippets.

View sergiomaia's full-sized avatar
💭
Focused

sergiomaia sergiomaia

💭
Focused
View GitHub Profile
# tenho a seguinte situação:
def update
ids = params["_json"].map { |hash| hash["id"] } # --> return [10, 20, 30, 40, 50]
positions = params["_json"].map { |hash| hash["position"] } # --> return [0, 1, 2, 3, 4]
query = MyQueryQuery.new
query.visible = true
query.active = true
{
"public": [
{
"thumb": "/uploads/user/avatar/thumb_6fd0915dcee8eb1a04bb215f68ecfa5a.png",
"name": "Lucky Luciano",
"email": "lif****@xample.com"
}
],
"privates": [
{
require 'rspec'
string_collection = [
"Web IconHTML & CSS100%",
"Command LineLearn the Command Line100%",
"Ruby IconRuby50%",
"Rails IconLearn Ruby on Rails100%",
"Git IconLearn Git100%",
"SassLearn Sass20%",
"JQuery IconjQuery1%",
@sergiomaia
sergiomaia / busca_linear.rb
Created September 27, 2017 02:40
Ruby Algorithm - Linear Search
def find_index(values, target)
values.each_with_index do |value, i|
return i if value == target
end
end
find_index([2, 4, 6, 8, 10, 12], 6)
# => 2
@sergiomaia
sergiomaia / magazine.html
Created January 30, 2017 15:46
Magazine Code
<iframe width="700px" height="425px" src="http://online.fliphtml5.com/cgpf/jhut/#p=1" frameborder="0" allowfullscreen allowtransparency></iframe>
# app/forms/registration.rb
class Registration
include ActiveModel::Model
attr_accessor(
:company_name,
:email,
:first_name,
:last_name,
:terms_of_service
# app/controllers/registration_controller.rb
class RegistrationsController < ApplicationController
respond_to :html
def new
@registration = Registration.new
end
def create
@registration = Registration.new(registration_params)
# config/routes.rb
resources :resgistration, only: [:new, :create]
1.upto(100) do |i|
if i % 3 == 0 && i % 5 == 0
puts "FizzBuzz"
elsif i % 3 == 0
puts "Fizz"
elsif i % 5 == 0
puts "Buzz"
else
puts i
end
@sergiomaia
sergiomaia / addhashtag.rb
Created October 4, 2016 23:24
Add Hashtag
# para esse exemplo pressupõe-se que você tenha um recurso post com o campo >> content:string
#Primeiro criamos um model Tag
rails g model Tag name:string
# >> crie uma tabela CreatePostsTags com post_id e tag_id (para nosso relacionamento has_and_belongs_to_many)
rails g migration CreatePostsTags post:references tag:references
# >> modifique a migration CreatePostsTags add :id => false, já que não precisaremos de id nesta tabela
class CreatePostsTags < ActiveRecord::Migration[5.0]