Skip to content

Instantly share code, notes, and snippets.

@squarism
Last active May 23, 2016 22:20
Show Gist options
  • Save squarism/1706126 to your computer and use it in GitHub Desktop.
Save squarism/1706126 to your computer and use it in GitHub Desktop.
WTF Computers

All my wtf momements, Ruby or otherwise.

# having a wat moment here in ruby
0 <=> 1
# => -1  (left is less, or another way to think of it, 
# the argument 1 is less than 0)

"0" <=> "1"
# => -1

0 <=> "1"
# => nil

# WAT
# So now if checks will be weird because of nil.  
# This also screws up Comparable <=> methods.  Gah.


# @tpitale:
# Using nil has a hash key {nil => 'value'} #lolwutruby
h = { nil => "wtf" }
# => {nil=>"wtf"}

h.keys.first.nil?
# => true

>> h[nil]
# => "wtf"

# How come Array.collect with a multi-line block returns an Enumerator
# but a single line block returns an array?
puts [1, 2, 3].collect do |number|; number; end
#<Enumerator:0x007fe7d2194fd0>
puts [1, 2, 3].collect {|number| number}.join(",")
1,2,3

# this is just order of operations problems.
puts([1, 2, 3].collect do |number|; number; end.join(","))
1,2,3


# Rubygems can't install all dev dependencies.  Gets in a loop.
# You have to download a gem to hack on and get its dependencies through Bundler.

# $ gem install --include-dependencies --dev bundler -V
# Unable to resolve dependencies: em-spec requires rspec (> 2.6.0), test-unit (>= 0); 
# sdoc-helpers requires sdoc (~> 0.2); railties requires rdoc (~> 3.4); mail requires i18n
# (>= 0.4.0); fuubar requires rspec (~> 2.0); capybara requires rack-test (>= 0.5.4); 
# jeweler requires rdoc (>= 0); uglifier requires execjs (>= 0.3.0); aruba requires rspec 
# (>= 2.7.0); webrat requires rack-test (>= 0.5.3); test-unit-rr requires test-unit (>= 
# 2.1.2); test-unit-notify requires test-unit (>= 2.1.2); mini_shoulda requires minitest (> 
# 2.1.0); actionpack requires i18n (~> 0.5.0), rack (~> 1.2.5), rack-test (~> 0.5.7), erubis 
# (~> 2.6.6); activemodel requires i18n (~> 0.5.0); bourne requires mocha (= 0.10.5); pry 
# requires coderay (~> 1.0.5); eco requires eco-source (>= 0), execjs (>= 0); hoe-highline 
# requires hoe (~> 2.8); coffee-script requires execjs (>= 0); hoe-gemspec requires hoe (>= 
# 2.2.0); hoe-git requires hoe (>= 2.2.0); hoe-bundler requires hoe (>= 2.2.0); gem_hadar 
# requires sdoc (~> 0.2.20)

# if your locale is en_US.UTF-8 (it is)
# and you are using bash and not using zsh 
# you will delete files by accident
# If you are using an older version of bash?  This doesn't seem to happen on bash-3.2 now.
touch resume.txt Trash.me
rm [A-Z]*  # delete all files with a capital letter
ls         # your precious face is deleted


# puts ignores the first newline
puts "this newline is ignored.\n"
puts "two newlines turn into one.\n\n"


# Exceptions have weird corner case
# Raise exception and rescue doesn't work but raise exception and rescue exception does. Wtf.
begin
  raise Exception
rescue
  puts "saved!"  # doesn't fire
end
# => Exception: Exception

begin
  raise Exception
rescue Exception
  puts "this works."
end
# => this works

# even though Exception is at the top of the object hierarchy
# so rescue with no class type should work
begin
  raise StandardError
rescue
  puts "this works."
end

# but did not work with Exception above.


# Ruby base64 generates a hash with a newline which is not allowed in the spec.
# It will be thrown away when you decode it so it's harmless.
# It's harmless unless you are taking the output and using as a hash, index or a search string.
require 'base64'
Base64.encode64("hello")
# => "aGVsbG8=\n"
# The solution is to use strict_encode64
Base64.strict_encode64("hello")
# => "aGVsbG8="
perl -e 'use MIME::Base64; print decode_base64("aGVsbG8=\n") . "\n";'
# => hello
# The new line is thrown away.


# Not ruby but headers in a rails controller isn't a shortcut for request.headers
headers
=> {"X-Frame-Options"=>"SAMEORIGIN", "X-XSS-Protection"=>"1; mode=block", "X-Content-Type-Options"=>"nosniff"}
headers.class
=> Hash

request.headers.class
# => ActionDispatch::Http::Headers

request.headers
#<ActionDispatch::Http::Headers:0x007fe4a5b82e00
 @env=
  {"CONTENT_LENGTH"=>"106",
   "CONTENT_TYPE"=>"application/x-www-form-urlencoded",
   "GATEWAY_INTERFACE"=>"CGI/1.1",
   "PATH_INFO"=>"/bacon",
   "QUERY_STRING"=>"",
   "REMOTE_ADDR"=>"::1",
   "REMOTE_HOST"=>"::1",
   "REQUEST_METHOD"=>"POST",
   ...


# to_i can't be used for number coercion
"5".to_i
# => 5
"golden rings".to_i
# => 0
"5 golden rings".to_i
# => 5

# use Integer(string) instead.

# YAML is terrible.
irb> YAML.load("no: true\nfalse: yes")
# => {false=>true}

Everything Else

What the hell. Using zsh on mac.

$ cd
$ cd sandbox
$ pwd
/var/empty
# What.

Mysql uses sockets if host is localhost. If you say 127.0.0.1, that's different. Because that makes sense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment