Skip to content

Instantly share code, notes, and snippets.

View joelibaceta's full-sized avatar
🏠
Working from home

Joel Ibaceta joelibaceta

🏠
Working from home
View GitHub Profile
def get_major_middle_min *numbers
(0..numbers.count-2).each |i| {numbers[i] > numbers[i+1] ? p i : } ...
# TODO
end
@joelibaceta
joelibaceta / Sudoku_validator.rb
Last active February 22, 2016 22:09
Fragmentos
#Valida soluciones de sudoku con la forma
validSolution([[5, 3, 4, 6, 7, 8, 9, 1, 2],
[6, 7, 2, 1, 9, 5, 3, 4, 8],
[1, 9, 8, 3, 4, 2, 5, 6, 7],
[8, 5, 9, 7, 6, 1, 4, 2, 3],
[4, 2, 6, 8, 5, 3, 7, 9, 1],
[7, 1, 3, 9, 2, 4, 8, 5, 6],
[9, 6, 1, 5, 3, 7, 2, 8, 4],
[2, 8, 7, 4, 1, 9, 6, 3, 5],
@joelibaceta
joelibaceta / after.rb
Last active May 9, 2016 17:29
Code Refactorization Example
class MovieReviewsApp
attr_accessor :tweet
@tweet = Hash.new()
def self.call(*args)
args.map{|a| JSON.parse(a).map{|i| append(i['title'], i)}}
return build_tweet(@tweet)
end
def self.append(title, hash)
codigo = " p 'hello world' "
pp Ripper.lex(codigo) # Paso 1, Tokenizacion
# [[[1, 0], :on_sp, " "],
# [[1, 1], :on_ident, "p"],
# [[1, 2], :on_sp, " "],
# [[1, 3], :on_tstring_beg, "'"],
# [[1, 4], :on_tstring_content, "hello world"],
# [[1, 15], :on_tstring_end, "'"],
@joelibaceta
joelibaceta / DSL_test.rb
Last active May 12, 2016 18:20
A Demo about a custom DSL
class Preference
# Not allow dynamic attributes
not_allow_dinamic_attributes
has_rest_method create: '/checkout/preferences'
has_rest_method read: '/checkout/preferences/:id'
has_rest_method update: '/checkout/preferences/:id'
# Setting the relations between objects
@joelibaceta
joelibaceta / developer1.rb
Last active May 14, 2016 02:57
Kind of Ruby Developers
# The Java Driven Developer
class Person
@name = ""
def get_name
return @name
end
def set_name
return @name
@joelibaceta
joelibaceta / Fastest
Last active June 28, 2020 03:14
For, ForEach ForOf Benchmark
Fastest is for_caching#test
@joelibaceta
joelibaceta / a_+_=_1.rb
Last active May 22, 2016 04:57
Difference between += and = +
Ripper.tokenize("a += 1")
# => ["a", " ", "+=", " ", "1"]
pp Ripper.sexp "a += 1"
#[:program,
# [[:opassign,
# [:var_field, [:@ident, "a", [1, 0]]],
# [:@op, "+=", [1, 2]],
# [:@int, "1", [1, 5]]]]]
ar = Array.new
(0..1000000).map{|i| ar[i] = i} # Fill the Array
ar[rand(1000000)]=ar[rand(1000000)] # Randomise the repetition
beginning_time = Time.now
repeated = ar.each_with_index.map{|v, i| ar[(ar[i]).abs] < 0 ? ((puts "#{ar[i].abs}"); break;) : ((ar[(ar[i]).abs]= - ar[(ar[i]).abs]))}
end_time = Time.now
puts "Time elapsed #{(end_time - beginning_time)*1000} milliseconds"