View hashing_example.rb
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
require 'zlib' | |
six_items = ["item_1", "item_2", "item_3", "item_4", "item_5", "item_6"] | |
ten_items = ["item_1", "item_2", "item_3", "item_4", "item_5", "item_6", "item_7", "item_8", "item_9", "item_10"] | |
chunks = 3 # should be set once and stay constant to preserve item<=>feed stickiness | |
six_items.each { |item_id| puts "#{item_id} goes to feed ##{Zlib.crc32(item_id) % chunks}" } | |
ten_items.each { |item_id| puts "#{item_id} goes to feed ##{Zlib.crc32(item_id) % chunks}" } | |
ten_items.reverse.each { |item_id| puts "#{item_id} goes to feed ##{Zlib.crc32(item_id) % chunks}" } |
View implicit_block_capture.rb
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
def new(url = nil, options = nil) | |
block = block_given? ? Proc.new : nil | |
options = options ? default_connection_options.merge(options) : default_connection_options | |
Faraday::Connection.new(url, options, &block) | |
end | |
# And it should be used like this: | |
conn = Faraday.new(:url => 'https://www.smartly.io/developer') do |faraday| | |
faraday.request :url_encoded # form-encode POST params |
View infinitely_nested_hash.rb
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
hash = Hash.new {|h, k| h[k] = Hash.new(&h.default_proc) } | |
hash[:this][:is][:really] = 'Amazing!' | |
hash | |
=> {:this=>{:is=>{:really=>"Amazing!"}}} |
View nested_hash.rb
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
h = Hash.new {|h, k| h[k] = Hash.new } |
View infinite_lambda_self_invocation.rb
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
go_deeper.call.call.call.call.call.call.call.call | |
# You can try to implement it yourself. Your code will look like this: | |
(go_deeper = -> { puts "deeper"; go_deeper }).call.call.call.call.call |
View return_multiple_values_example.rb
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
return [:ok, response.body] | |
# or | |
[:ok, response.body] |
View return_multiple_values.rb
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
status, reason = send def authorize | |
response = do_request | |
if response.success? | |
return :ok, response.body | |
else | |
return :error, response.error | |
end | |
end |
View iffe_plus_finally_plus_return.rb
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
send def escape | |
return "I want to return" | |
ensure | |
return "I want to finally get out" | |
end | |
=> "I want to finally get out" |
View iffe_plus_finally.rb
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
send def escape | |
return "I want to return" | |
ensure | |
puts "print this text first" | |
end | |
# Output looks like this: | |
print this text first | |
=> "I want to return" |
View iffe4.rb
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 RailsController | |
helper_method def current_user | |
User.current | |
end | |
end |
NewerOlder