Skip to content

Instantly share code, notes, and snippets.

View carolvisoto's full-sized avatar

Caroline Visoto Nunes carolvisoto

View GitHub Profile
require 'soundcloud'
class StoriesController < ApplicationController
def index
@stories = Story.all
end
def new
get_user
@carolvisoto
carolvisoto / blocks.rb
Last active August 29, 2015 14:15 — forked from jkischkel/blocks.rb
# a simple block:
p [1,2,3].map { |i| i.to_s }
# a block with do/end
p [1,2,3].map do |i|
i.to_s
end
# even fancier, with a function reference
p [1,2,3].map(&:to_s)
# Iterator methods without blocks are called Enumerators
[1,2,3].each_index # => #<Enumerator: [1, 2, 3]:each_index>
0.upto(10) # => #<Enumerator: 0:upto(10)>
# You can call any Enumerable method on Enumerators
[1,2,3].each_index.map {|i| i + 10 } # => [10, 11, 12]
0.upto(10).map {|i| i + 10 } # => [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
# See documentation about Enumerables
# Returns all permutations of string s
def permutations(s)
# For single characters just return one solution, the character
if s.size <= 1
[s]
else
# calculate all permutations without the first character
# and then iterate over each
permutations(s[1..-1]).flat_map do |x|
# for each position in permutation x