One of Ruby's major influences was Perl, leaving well equipped to replace bash, sed, and awk. Lets take a look at how to do that.
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
# app/controllers/users_controller.rb | |
class UsersController < ApplicationController | |
def index | |
render text: "Your IP is #{request.remote_ip}" | |
end | |
end | |
# test/controllers/users_controller_test.rb | |
class UsersControllerTest < ActionController::TestCase | |
test "should get index" do |
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
/* touch -- change modification and access times of files | |
Copyright (C) 1987, 1989-1991, 1995-2005, 2007-2011 Free Software | |
Foundation, Inc. | |
This program is free software: you can redistribute it and/or modify | |
it under the terms of the GNU General Public License as published by | |
the Free Software Foundation, either version 3 of the License, or | |
(at your option) any later version. | |
This program is distributed in the hope that it will be useful, |
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
# Read initial standard input from the data segment at the end of the file | |
# You can also see what is ultimately output down there | |
pos = DATA.pos # => 1617 | |
$stdin.reopen __FILE__ # => #<IO:/var/folders/7g/mbft22555w3_2nqs_h1kbglw0000gn/T/seeing_is_believing_temp_dir20161210-36802-czciw7/program.rb> | |
$stdin.seek pos, :SET # => 0 | |
# The commands that will be piped together | |
commands = [ | |
%w[cat], | |
%w[tr a 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
# these are reconstructed from a shell session without runnig them, so make | |
# sure you check that it's a sane thing to do before running it, I make no | |
# guarantees of fitness, and accept no liability. Run at your own risk. | |
sudo launchctl remove com.snap.SnapCameraRemover | |
rm -r ~/Library/Caches/Snap/ | |
rm -r ~/Library/Caches/com.snap.SnapCamera/ | |
rm -r ~/Library/Preferences/Snap/ | |
rm ~/Library/Preferences/com.snap.SnapCamera.plist | |
rm ~/Library/Preferences/com.snap.Snap\ Camera.plist | |
sudo rm -rf /Applications/Snap\ Camera.app/ |
- specs go in the "spec" directory
- specs end in "_spec.rb"
- toplevel begins with
describe
- inside of a
describe
you have anit
example
is an alias forit
- inside of
it
blocks, you write your code and assertions - run with
$ rspec
or$ rspec spec/some_spec.rb
- if you need common setup (e.g. add another dir to the load path), put into "spec/spec_helper.rb" and then `require "spec_helper" at the top of the spec
- using
let
will allow you to name a value that you want to call from your spec,
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
# I submitted a bug report https://github.com/rails/rails/issues/34244 | |
# b/c Rails was not honouring my `rescue_from` block which was causing my API to | |
# be inconsistent. I was told this is expected behaviour. I think it's probably | |
# fine for an HTML app where you control the form inputs. But for an API app, | |
# the API is public facing and very important, so Rails shouldn't have its own | |
# special errors that bypass my app's configuration and make my API inconsistent. | |
# | |
# Decided it shouldn't be too difficult to handle this myself. So, here is my | |
# solution. It contains most of the important lessons I've learned about how to | |
# get a Rails API app setup. It removes Rails' params parsing and adds its own. |
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
# Helpers to make the code below easier to read | |
class Array | |
def keywords?() = Hash.ruby2_keywords_hash?(self[-1]) | |
def to_keywords!() = (self[-1] = Hash.ruby2_keywords_hash(self[-1])) | |
end | |
# Code to facilitate the examples below | |
def record_args(*args) = args | |
def call_this(val:) = val |
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
puts Solution.new('1. Fizz Buzz', <<SOLUTION) | |
def fizzbuzz(n)(n%15==0?'FizzBuzz':n%3==0?'Fizz':n%5==0?'Buzz':n).to_s end | |
SOLUTION | |
.test { fizzbuzz 3 }.expect { |result| result == "Fizz" } | |
.test { fizzbuzz 10 }.expect { |result| result == "Buzz" } | |
.test { fizzbuzz 45 }.expect { |result| result == "FizzBuzz" } | |
.test { fizzbuzz 31 }.expect { |result| result == "31" } |
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 'pg' | |
db = PG.connect dbname: 'josh_testing' | |
db.exec 'begin' | |
def db.exec(*) | |
super.to_a | |
rescue | |
$!.set_backtrace caller.drop(1) | |
raise | |
end |
NewerOlder