Skip to content

Instantly share code, notes, and snippets.

class CaseTest
def initialize(number)
@number = number
end
def test
case @number
when it_is_eight?
'eight'
when it_is_five?
@seban
seban / pattern_matching_numbers.rb
Created April 18, 2019 12:41
First try of Ruby 2.7 pattern matching
def fizzbuzz(number)
case [number % 3, number % 5]
in [0, 0] then 'FizzBuzz'
in [0, _] then 'Fizz'
in [_, 0] then 'Buzz'
end
end
puts fizzbuzz(15) # => 'FizzBuzz'
puts fizzbuzz(3) # => 'Fizz'
;; To run this one you need to have Leiningen installed.
;; cat reviewers.clj | lein repl
(def team
["Andrzej" "Artur" "Jarek" "Junior" "Oskar" "Piotr" "Paweł" "Sebastian"])
(def me "Sebastian")
(->> team
(remove #(= %1 me))
(shuffle)
require 'benchmark'
def test_exception
raise "test exception" unless true
2 + 2
rescue
4
end
def test_unless
class TravelDocumentPolicy
def create?
user.agent? || user.superagent?
end
def update?
create?
end
class Profile < ActiveRecord::Base
validates :facebook_profile, url: { allow_blank: true }
end
# failing spec
specify "www.facebook.com/username should be valid facebook profile url" do
profile.facebook_profile = "www.facebook.com/username"
# controller code
current_merchant.payments.find(params[:id])
# test code
stub(Merchant).find(@merchant_id) { @merchant }
stub(@merchant).payments.stub!.find(@payment.id.to_s) { @payment }
login_merchant(@merchant)
@seban
seban / message_test.rb
Created December 24, 2013 11:16
Running this spec raises an ArgumentError: wrong number of arguments (2 for 0) at line 9.
require "rubygems"
require "minitest/autorun"
describe "#let" do
describe "problematic names" do
let(:message) { "It is my message" }
it "is not accessible" do
message.must_equal "It is my message"
end
# Class validates if passed record's attribute value has proper Liquid template syntax.
class LiquidSyntaxValidator < ActiveModel::EachValidator
def validate_each(record, attribute, template)
Liquid::Template.parse(template)
rescue Liquid::SyntaxError
record.errors.add(attribute, :liquid_syntax_error)
end
end
Offer
.with_nokaut_category_id(10745)
.and({
"$or" => [
{ "is_trash"=>false }, { "is_trash" => { "$exists" => false } }
]
},
{
"$or" => [
{ "xml_category" => /ultrad.wi.kowe/ },