Hash[*User.all.collect{|u|[u.id,u.email]}.flatten]
# {1=>"foo@gmail.com", 2=>"bar@gmail.com", 3=>"baz@inmunited.com"}
Reveal More
http://twitter.com/codemariner
Hash[*User.all.collect{|u|[u.id,u.email]}.flatten]
# {1=>"foo@gmail.com", 2=>"bar@gmail.com", 3=>"baz@inmunited.com"}
curl --header "X-Forwarded-For: 24.235.32.1" http://www.cambio.com > index.html
module MyModule
module ClassMethods
def string(name)
module_eval <<-RUBY , __FILE__, __LINE__ + 1
def #{name}
raise "#{name}" #line number 6
end
RUBY
end
end
def self.included(base)
base.extend(ClassMethods)
end
end
class MyClass
include MyModule
string :foo
end
m = MyClass.new
m.foo
# results in
# $ ruby ./test.rb
#./test.rb:6:in `foo': foo (RuntimeError)
# from ./test.rb:25
#!/usr/bin/env ruby
require 'rubygems'
require 'net/ssh'
hosts = {:diginode2 => {:host => "64.93.11.111", :forwarding => [
{:local_port => 3002, :local_host => "localhost", :remote_port => 3000}, # admin webapp
{:local_port => 1936, :local_host => "localhost", :remote_port => 1935}, # fms rtmp
{:local_port => 1112, :local_host => "localhost", :remote_port => 1111} # fms admin
]
},
:diginode1 => {:host => "64.93.11.112", :forwarding => [
{:local_port => 3000, :local_host => "192.168.19.50", :remote_port => 3000}, # admin app
{:local_port => 3001, :local_host => "localhost", :remote_port => 3001}, # admin app
{:local_port => 1935, :local_host => "localhost", :remote_port => 1935}, # fms rtmp
{:local_port => 1111, :local_host => "localhost", :remote_port => 1111} # fms admin
]
},
:diginode3 => {:host => "64.93.11.113", :forwarding => [
{:local_port => 1937, :local_host => "localhost", :remote_port => 1935}, # fms rtmp
{:local_port => 1113, :local_host => "localhost", :remote_port => 1111} # fms admin
]
},
:record1 => {:host => "64.93.11.211", :forwarding => [
{:local_port => 9001, :local_host => "localhost", :remote_port => 8000} # json rpc
]
},
:record2 => {:host => "64.93.11.212", :forwarding => [
{:local_port => 9002, :local_host => "localhost", :remote_port => 8000} # json rpc
]
},
:record14 => {:host => "64.93.11.224", :forwarding => [
{:local_port => 9014, :local_host => "localhost", :remote_port => 8000} # json rpc
]
}
}
hosts.each do |name, config|
Thread.new {
puts "connecting to #{config[:host]}"
Net::SSH.start(config[:host], "diginode", :password => "fc8arca#0ne") do |ssh|
forwards = config[:forwarding]
forwards.each do |f|
puts "forwarding #{f[:local_port]}, #{f[:local_host]}, #{f[:remote_port]} from #{config[:host]}"
ssh.forward.local(f[:local_port], f[:local_host], f[:remote_port])
end
ssh.loop {true}
end
}
end
while (true)
sleep(0.5)
end
curl -s checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//'
curl -s http://whatismyip.org/
# something that represents a rake task
class Task
attr_accessor :name
def initialize(name)
@name = name
end
end
# something like a rake 'task' method
def task(name, &block)
yield Task.new(name)
end
# a method that will execute a block defined inside of a rake task
# the idea is that this method can trap and report on exceptions
# I want to get the task name to include in the report in as transparent
# of a way as possible. Unfortunately, it seems eval is the only way to
# go about getting this.
def handler(&block)
begin
yield
rescue Exception => e
# ideally, this would test for t first
name = block.binding.eval('t.name')
puts "error in task '#{name}': e.message"
end
end
task "publish" do |t|
handler do
# stuff here
# handler will pick up t
raise "something bad happened"
end
end
# this results in
# error in task 'publish': e.message
module Stalky
# config object that will be globally accessible
# as Stalky::Config
class Config
# same ol attr_accessor but applied to the Config class itself
class << self; attr_accessor :qwerly_api_key, :rapleaf_api_key; end
def initialize(&block)
# pass the Config class into the given block
yield Config if block_given?
end
end
end
# put this in something like config/initializers/stalky.rb
Stalky::Config.new do |config|
config.qwerly_api_key = "foo"
config.rapleaf_api_key = "bar"
end
# displays 'foo'
puts Stalky::Config.qwerly_api_key
# displays 'bar'
puts Stalky::Config.rapleaf_api_key
# stick this in some kind of initializer code like config/environment.rb
APP_CONFIG = YAML.load_file("#{Rails.root}/config/app_config.yml")
# to make it environment specific
APP_CONFIG = YAML.load_file("#{Rails.root}/config/app_config.yml")[Rails.env]
# sample yaml :
<<-YAML # put this in config/app_config.yml
development:
api_key: abcdefg
production:
api_key: 1234567
YAML
# then you can access like this:
api_key = APP_CONFIG['api_key']
# stick this in some kind of initializer code like config/environment.rb
APP_CONFIG = YAML.load_file("#{Rails.root}/config/app_config.yml")
# to make it environment specific
APP_CONFIG = YAML.load_file("#{Rails.root}/config/app_config.yml")[Rails.env]
# sample yaml :
<<-YAML # put this in config/app_config.yml
development:
api_key: abcdefg
production:
api_key: 1234567
YAML
# then you can access like this:
api_key = APP_CONFIG['api_key']
class Foo
def self.do_it
"bar"
end
class << self; extend ActiveSupport::Memoizable; self; end.memoize :do_it
end
# truncates a string based on word boundaries rather than
# actual length
def truncate_words(text, num_words = 30, end_string = '...')
return unless text
words = text.split()
words[0..(num_words-1)].join(' ') + (words.size > num_words ? end_string : '')
end
require 'rubygems'
require 'mongrel'
server = Mongrel::HttpServer.new("0.0.0.0", "8888")
server.register("/", Mongrel::DirHandler.new("."))
server.run.join