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 Person < ActiveRecord::Base | |
# If I18n.t is evaluated when the model is loaded, which in production happens only once | |
# the translation will always use the locale which was set at that moment. | |
# | |
# (Instead just use the activerecord.errors.person.attributes.email.invalid key in | |
# the translation files) | |
# | |
# This does NOT work properly: | |
validates_format_of :email, :with => REGEXP, :message => I18n.t(:email_invalid) |
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 Hash | |
# Compares two hashes bases on only their keys. | |
# Recursively matches nested hashes too. | |
# | |
# (Used for comparing translations hashes in test.) | |
def =~(other, _raise = false) | |
if _raise | |
raise "Unmatched key: #{self.keys.inspect} / #{other.keys.inspect}" unless self.keys == other.keys | |
end | |
(self.keys == other.keys) && all?{ |k, v| !v.is_a?(Hash) || other[k].=~(v, _raise) } |
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
# test/test_helper.rb | |
# Make sure the tests fail if it encounters a missing translation: | |
module I18n | |
def self.just_raise(*args) | |
raise args.first | |
end | |
end | |
I18n.exception_handler = :just_raise |
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 Object | |
def metaclass | |
class << self; self; end | |
end | |
end | |
class Tea | |
@@liters = 5 | |
class << Tea | |
def spill_all |
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
Processing UsersController#create (for 195.232.229.139 at 2009-06-18 14:50:29) [POST] | |
Parameters: {"user"=>{"password_confirmation"=>"[FILTERED]", "password"=>"[FILTERED]", "login"=>"XXX"}, "profile"=>{"first_name"=>"XXX", "last_name"=>"XXX"}, "action"=>"create", "authenticity_token"=>"XXX", "controller"=>"users"} | |
Sent mail to XXX | |
========= | |
Unable to send e-mail! 501 Syntax: HELO hostname | |
/usr/local/lib/ruby/1.8/net/smtp.rb:679:in `check_response' |
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
# HTTP Basic Auth | |
class User < ActiveRecord::Base | |
has_remote :site => "http://example.com", :user => "foo", :password => "bar" do |remote| | |
#... | |
end | |
end | |
# API key | |
class User < ActiveRecord::Base | |
has_remote :site => "http://example.com?api_key=12x34x5" do |remote| |
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 User < ActiveRecord::Base | |
def email | |
# Make REST call, parse response, extract email address and return it. | |
# Probably not the most concise and expressive code. | |
end | |
end | |
# Is more or less equal to: | |
class RemoteUser < ActiveResource::Base |
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 Object | |
def metaclass | |
class << self; self; end | |
end | |
end | |
# - Normal cases - | |
class Foo | |
@@bar = 0 |
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 Object | |
def metaclass | |
class << self; return self; end | |
end | |
end | |
class Foo | |
# 1. def within class definition | |
def self.set_a |
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 ConstantGardener | |
def self.create(*booleans) | |
Class.new do | |
def initialize(bitmask) | |
@bitmask = bitmask | |
end | |
booleans.each_with_index do |b, i| | |
const_set b.to_s.upcase, 2**i |
OlderNewer