Skip to content

Instantly share code, notes, and snippets.

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

Ed de Almeida EdDeAlmeidaJr

🏠
Working from home
View GitHub Profile
@EdDeAlmeidaJr
EdDeAlmeidaJr / isprime02.rb
Created June 1, 2016 12:12
A better function to tell if a number is prime or not
def prime?(n)
res = true
limit = Math.sqrt(n)
(2..limit).each{ |d| res = res && (not ((n%d)==0)) }
return res
end
@EdDeAlmeidaJr
EdDeAlmeidaJr / example_of_link_wrapping_html_01.html
Last active June 20, 2016 06:21
Example of link wrapping html
@EdDeAlmeidaJr
EdDeAlmeidaJr / rails_link_wrapping_html.html.erb
Created June 20, 2016 07:01
Example of link wrapping HTML with Rails
@EdDeAlmeidaJr
EdDeAlmeidaJr / link_img_div.html.erb
Created June 20, 2016 07:07
Example of link containing an image and some other HTML elements with Rails link_to helper
@EdDeAlmeidaJr
EdDeAlmeidaJr / bad_type_conversion.rb
Created June 23, 2016 13:01
Example of bad implicit type conversion
def LongestWord(sen)
sen1 = sen.split("/\W+/")
grt = 0
sen1.each do |i|
if sen1[i].length > sen1[grt].length # Type conversion error
grt = i
end
end
sen1[grt]
end
@EdDeAlmeidaJr
EdDeAlmeidaJr / example0002.rb
Created June 23, 2016 13:21
Example fixing the previous gist
def longest_word(sen)
sen1 = sen.split(" ")
grt = 0
sen1.each_index do |i|
if sen1[i].length > sen1[grt].length
grt = i
end
end
sen1[grt]
end
@EdDeAlmeidaJr
EdDeAlmeidaJr / example0003.rb
Created June 23, 2016 13:30
Optimizing the code in example0002.rb
def longest_word(str)
str.split(" ").max_by{ |s| s.length }
end
@EdDeAlmeidaJr
EdDeAlmeidaJr / cohesion01.rb
Last active July 15, 2016 02:07
Example of perfect coupling among two methods
def some_method
(1..1000).each{ |i| puts i if sum_is_ten?(i) }
end
def sum_is_ten?(par)
sum = 0
par.to_s.each_char{ |c| sum += c.to_i }
sum == 10
end
@EdDeAlmeidaJr
EdDeAlmeidaJr / cohesion02.rb
Last active July 15, 2016 02:08
Example of cohesion.
def some_method
(1..1000).each{ |i| puts i if sum_is_ten?(i) }
end
def sum_is_ten?(par)
sum = 0
par.to_s.each_char{ |c| sum += c.to_i }
((sum == 10) && (par%4 == 0))
end
@EdDeAlmeidaJr
EdDeAlmeidaJr / cohesion03.rb
Last active July 15, 2016 02:09
Example of cohesion
def some_method
(1..1000).each{ |i| puts i if (sum_is_ten?(i) && (i%4 == 0)) }
end
def sum_is_ten?(par)
sum = 0
par.to_s.each_char{ |c| sum += c.to_i }
sum == 10
end