Skip to content

Instantly share code, notes, and snippets.

@Shamaoke
Shamaoke / dup_clone_diff.rb
Last active September 28, 2015 00:38
Difference between dup and clone in Ruby
# Difference between dup and clone in Ruby
class Example
attr_accessor :str, :arr
end
s1 = Example.new
s1.str = 'ok!'
s1.arr = [1, 2, 3]
@Shamaoke
Shamaoke / creating_users.feature
Created January 16, 2012 11:26
Scenario: Creating a user when another user with the same name exists
Scenario: Creating a user when another user with the same surname exists
Given a user with "name" = "Ross" and "surname" = "Geller" exists
When I create a user with "name" = "Monica" and "surname" = "Geller"
Then 2 users with "surname" = "Geller" should exist
And the only user with "name" = "Ross" should exist
And the only user with "name" = "Monica" should exist
@Shamaoke
Shamaoke / rails__authentication_example.rb
Created February 10, 2012 19:28
Rails' Authentication example
# module
require 'digest/sha2'
module Extensions
module Authentication
extend ActiveSupport::Concern
module ClassMethods
def find_and_authenticate(attributes)
@user.authenticate(attributes[:password]) if @user = find_by_email(attributes[:email])
@Shamaoke
Shamaoke / write_attributes.rb
Created February 11, 2012 09:59
Write Attributes
# Schema Information
#
# Table name: users
#
# id: integer
# name: string
# surname: string
class User < ActiveRecord::Base
def full_name=(full_name) # a virtual attribute
@Shamaoke
Shamaoke / private-public.rb
Created February 13, 2012 07:40
Call private, retry call public
# encoding: utf-8
class Example
private
def one
'ok!'
end
public
def two
'ok too!'
@Shamaoke
Shamaoke / module_require.js
Created February 19, 2012 11:42
Using module.require in Node.js
// ross.js
this.ross = {
name: 'Ross',
surname: 'Geller',
get_name: function() { return this.name },
get_surname: function() { return this.surname },
get_fullname: function() { return this.name + ' ' + this.surname }
}
this.module = module
@Shamaoke
Shamaoke / io_popen_pipe.rb
Last active September 30, 2015 21:17
Using IO.popen and IO.pipe
# ruby child | ruby parent
IO.popen('-') do |io|
if io # run in the current process
puts io.read.upcase
else # run in the child process
$>.puts 'ok!'
end
end
# ruby child | ruby parent
@Shamaoke
Shamaoke / set_interval.coffee
Created February 24, 2012 15:36
Using setInterval
class Example
constructor: (@counter, @final, @id = null) ->
prepare: (meth) -> @id = setInterval(meth, 1000); return
count: =>
console.log @counter
@counter += 1
clearInterval @id if @counter is @final
return
start: -> @prepare @count; return
@new: -> new @ 0, 10
@Shamaoke
Shamaoke / set_timeout.coffee
Created March 14, 2012 19:03
Using setTimeout
class Example
constructor: (@counter, @final) ->
count: ->
setTimeout =>
console.log @counter
@counter += 1
@count() unless @counter is @final
, 1000
@new: -> new @ 0, 10
@Shamaoke
Shamaoke / coffeescript_singleton.coffee
Created March 16, 2012 13:13
CoffeeScript Singleton
# class
class Singleton
constructor: ->
throw new Error 'Singleton error' if @.constructor is Singleton or @.constructor._instance
@.constructor._instance = @
@instance: (arg...) ->
unless @._instance
new @ arg...
else
@._instance