Skip to content

Instantly share code, notes, and snippets.

View NetguruGist's full-sized avatar

Netguru NetguruGist

View GitHub Profile
@NetguruGist
NetguruGist / serializers1.ruby
Created October 30, 2015 08:28
serializers1 ruby
class User < ActiveRecord::Base
has_many :notes
end
class UserSerializer < ActiveModel::Serializer
attributes :id, :name
has_many :notes
end
```
```
@NetguruGist
NetguruGist / fibonacci1.ruby
Created October 31, 2015 14:50
fibonacci1 ruby
def nth_fibonacci(number)
return 1 if [0, 1].include? number
nth_fibonacci(number - 2) + nth_fibonacci(number - 1)
end
@NetguruGist
NetguruGist / fibonacci2.ruby
Created October 31, 2015 14:50
fibonacci2 ruby
def gcd(x, y)
loop do
r = x % y
return y if r.zero?
x, y = y, r
end
end
@NetguruGist
NetguruGist / fibonacci3.ruby
Created October 31, 2015 14:50
fibonacci3 ruby
gcd(nth_fibonacci(n), nth_fibonacci(n+1))
@NetguruGist
NetguruGist / fibonacci4.ruby
Created October 31, 2015 14:50
fibonacci4 ruby
> gcd(nth_fibonacci(32), nth_fibonacci(33))
=> 1
@NetguruGist
NetguruGist / fibonacci5.ruby
Created October 31, 2015 14:50
fibonacci5 ruby
def array_of_fibonaccis(amount)
fibonacci_numbers = [1, 1]
return [1] if amount < 2
(amount - 2).times { (fibonacci_numbers << fibonacci_numbers[-2..-1].inject(&:+)) }
fibonacci_numbers
end
@NetguruGist
NetguruGist / fibonacci6.ruby
Created October 31, 2015 14:50
fibonacci6 ruby
def neighbour(n, m)
return unless (n - m).abs == 1 && n < m
array_of_fibonaccis(m + 1)[n..m]
end
@NetguruGist
NetguruGist / hiddengems1.swift
Created October 31, 2015 14:50
hiddengems1 swift
struct RegularExpression {
let pattern: String
init(pattern: String) {
self.pattern = pattern
}
}
@NetguruGist
NetguruGist / hiddengems2.swift
Created October 31, 2015 14:50
hiddengems2 swift
enum Bit: UInt8 {
case Zero = 0
case One = 1
}
let five: "\(Bit.One)\(Bit.Zero)\(Bit.One)" // "OneZeroOne"
@NetguruGist
NetguruGist / hiddengems3.swift
Created October 31, 2015 14:50
hiddengems3 swift
extension String {
init(stringInterpolationSegment bit: Bit) {
self = "\(bit.rawValue)"
}
}
let five: "\(Bit.One)\(Bit.Zero)\(Bit.One)" // "101"