How to create an infinitely nestable hash that always defaults to a new hash if a key does not map to a value.
You can use these step definitions:
Then /^I should not see an error$/ do (200 .. 399).should include(page.status_code) end Then /^I should see an error$/ do (400 .. 599).should include(page.status_code) end
Note that you need to tag the scenario with
@allow-rescue to test that an error is shown like this
@allow-rescue Scenario: Accessing the admin area requires a login When I go to the admin area Then I should see an error
Use the htmlentities gem.
Encoding works like this:
require 'htmlentities' coder = HTMLEntities.new string = "<élan>" coder.encode(string) # => "<élan>" coder.encode(string, :named) # => "<élan>" coder.encode(string, :decimal) # => "<élan>" coder.encode(string, :hexadecimal) # => "<élan>"
Decoding works like this:
require 'htmlentities' coder = HTMLEntities.new string = "élan" cod...
If you modified git's history and the change was already pushed, you will usually get a
! [rejected] my-branch -> my-branch (non-fast-forward)
error, when trying to push.
You can force the push, with
git push --force origin my-branch
- You might lose history.
- Unless your git is configured to push only the current branch, you must supply the remote branch name or you will force-push all your branches!
- Anyone else who has already pulled the changes will run into significant trouble.
You can get
YAML.load to instantiate any Ruby object by embedding the desired class name into the YAML code. E.g. the following will create a new
User object and set
@password to the given values:
--- !ruby/object:User email: email@example.com password: secret
Considering the security implications, you should never trust YAML from untrusted sources. If you are looking for a simple, secure and readable data transfer format, use
Gallery of fonts you can use without much hassle in LaTeX. The license of the fonts vary, but are all free. Note that the fonts not necessarily are free to distribute, and some fonts are available for non-commercial use only.
Ruby's net/http is setup to never verify SSL certificates by default. Most ruby libraries do the same. That means that you're not verifying the identity of the server you're communicating with and are therefore exposed to man in the middle attacks. This gem monkey-patches net/http to force certificate verification and make turning it off impossible.
If you want to find out whether a Class object is directly inheriting from another class, use
ActiveRecord::RecordNotFound.super_class == ActiveRecord::ActiveRecordError # => true
To check if another class is an ancestor (not necessarily the direct superclass, but e.g. the superclass of the superclass):
ActiveRecord::RecordNotFound.ancestors.include?(StandardError) # => true
ancestors also includes the receiving class itself as well as any included modules (which is quite…
Sometimes, you just need to shoot from the hip…or deploy your local changes without committing them. Put this snippet from Jesse Newland in ~/.caprc and now you can cap cowboy deploy.
To change the commit message of the latest (unpushed, unmerged) commit, you can use
git commit --amend
To change the commit message of an earlier (unpushed, unmerged) commit [COMMIT], you can do
git rebase -i COMMIT~
For a current version of git, you can simply mark the line with "reword", and git will ask you later for the new message.
For older versions:
- mark the line with
- save the file
- do a
git commit --amendwhen rebasing stops at the relevant commit
git rebase --continue
If your controller spec never reaches your controller code:
- Make sure you are signed in.
- Make sure you are actually triggering a request by saying
get :editor something siliar.
describe UsersController do describe '#edit' do it 'should work' do sign_in get :edit end end end # define something like this in your spec_helper.rb: def sign_in(user = User.new) controller.instance_variable_set('@current_user', user) end
To temporarily change the current working directory in Ruby, call
Dir.chdir with a block. The previous working directory will be restored when the block ends:
Dir.chdir('/usr/local') do # do stuff in /usr/local end
How to write to the db 27,000 times in 24 seconds instead of 9 minutes.
To test whether two arrays have the same elements regardless of order, RSpec gives you the
actual_array.should =~ expected_array
It's a little tricky to emulate this check in plain Ruby (outside of a spec). If you're OK with ignoring duplicate elements and your element classes implement
#hash correctly (e.g. Strings), you can say:
def same_elements?(array1, array2) array1.to_set == array2.to_set end
If duplicate elements matter (e.g. you consider
[1,1,2] not to ha…
These two models can be used to access the posts and associated comments of a WordPress database.
Lets say you need to make a change to a commit
OLD_COMMIT, but this is not the most recent. If you have neither pushed nor merged it, you can do this:
- Make a new commit now, with a message like "fix".
- Do a
git rebase -i OLD_COMMIT~
- In the editor window that opened, move the "fix" commit directly after the one you want to amend (so it should be the second from the top), and mark it as "fixup". Save the file.
- If there are conflicts, solve them, add them, and do
git rebase --continue
The first thing you need to understand is that the purpose of refinements in Ruby 2.0 is to make monkey-patching safer. Specifically, the goal is to make it possible to extend core classes, but to limit the effect of those extensions to a particular area of code. Since the purpose of this feature is make monkey-patching safer, let’s take a look at a dangerous case of monkey-patching and see how this new feature would improve the situation.