This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Schema Information | |
# | |
# Table name: users | |
# | |
# id: integer | |
# name: string | |
# surname: string | |
class User < ActiveRecord::Base | |
def full_name=(full_name) # a virtual attribute |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding: utf-8 | |
class Example | |
private | |
def one | |
'ok!' | |
end | |
public | |
def two | |
'ok too!' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Example | |
constructor: (@counter, @final) -> | |
count: -> | |
setTimeout => | |
console.log @counter | |
@counter += 1 | |
@count() unless @counter is @final | |
, 1000 | |
@new: -> new @ 0, 10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
OlderNewer