This file contains hidden or 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
# add gem 'pusher' to your Gemfile | |
require 'pusher-client' | |
PusherClient.logger = Logger.new(STDOUT) | |
options = {secret: 'my_secret'} | |
socket = PusherClient::Socket.new('my_key', options) | |
socket['private-foo'].bind('client-status') do |data| | |
puts "Got data: #{data}" |
This file contains hidden or 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
pusher = new Pusher("#{Settings.pusher_key}") | |
channel = pusher.subscribe("integrations-ui") | |
channel.bind('some-event', function(data) { | |
alert(data.message) | |
}) |
This file contains hidden or 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
# see Metaprogramming Ruby, p. 70 | |
v1 = 1 | |
class MyClass | |
puts "Scope gate: entering class" | |
v2 = 2 | |
puts " Local variables: #{local_variables}" | |
def my_method | |
puts "Scope gate: entering method" | |
v3 = 3 |
This file contains hidden or 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
# see Metaprogramming Ruby, p. 72 | |
my_var = "success" | |
MyClass = Class.new do | |
puts "#{my_var} in the class definition!" | |
define_method :my_method do | |
puts "#{my_var} in the method!" | |
end |
This file contains hidden or 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 my_method(x) | |
yield(x) if block_given? | |
end | |
y = 10 | |
my_method(6) { |x| puts "hello: #{x + y}" } #16 |
This file contains hidden or 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 my_method(x) | |
yield(x) if block_given? | |
end | |
my_method(6) { |x| puts "hello: #{x}" } |
This file contains hidden or 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
# see Metaprogramming Ruby, p. 56 | |
class String | |
def method_missing(method, *args) | |
method == :ghost_reverse ? reverse : super | |
end | |
end | |
require 'benchmark' | |
Benchmark.bm do |b| | |
b.report 'Normal method' do |
This file contains hidden or 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 DataSource | |
def get_display_info(x) | |
"GeForce 3000" | |
end | |
def get_display_price(x) | |
100 | |
end | |
def get_casing_info(x) |
This file contains hidden or 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 Roulette | |
def method_missing(name, *args) | |
person = name.to_s.capitalize | |
3.times do | |
number = rand(10) + 1 | |
puts "#{number}" | |
end | |
"#{person} got a #{number}" # Ruby gets confused here | |
end | |
end |
This file contains hidden or 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 DataSource | |
def get_video_info(x) | |
"GeForce 3000" | |
end | |
def get_video_price(x) | |
100 | |
end | |
def get_casing_info(x) |