Skip to content

Instantly share code, notes, and snippets.

@tdshipley
tdshipley / bang_method_example_1.rb
Last active August 29, 2015 14:24
Ruby Bang Method Example
x = hEllO
x.downcase # returns "hello" x == hEllO
x.downcase! #=> x == "hello"
@tdshipley
tdshipley / ruby_string_interpolation_example_1.rb
Last active August 29, 2015 14:24
Ruby String Interpolation Example 1
puts "#{getFilename(filenumber)}.jpeg"
@tdshipley
tdshipley / ruby_isjpeg_method.rb
Last active August 29, 2015 14:24
Ruby isJPEG? method
# @param [Object] block A 512 byte block to check if it contains a jpeg header in first 4 bytes
def isJPEG?(block)
# Encodings are diff between reading an image file and strings so block[0] == 0xFF wont work
# http://stackoverflow.com/questions/16815308/ruby-comparing-hex-value-to-string
jpeg_header_part = ['FFD8FF'].pack('H*')
return block[0,3] == jpeg_header_part && (block[3] == 0xe0.chr || block[3] == 0x0e1.chr)
end
@tdshipley
tdshipley / question_mark_ruby_example.rb
Created July 13, 2015 21:01
Ruby Question Mark Method Example
until file.eof?
@tdshipley
tdshipley / go_json_struct.go
Created August 31, 2015 21:51
Go Struct for JSON
type Person struct {
Name string `json:"name"`
Age int `json:"age"`
}
@tdshipley
tdshipley / go_yaml_import.go
Created August 31, 2015 22:05
Import statement for go-ymal
package main
import (
"gopkg.in/yaml.v2"
)
@tdshipley
tdshipley / go_yaml_struct.go
Created August 31, 2015 22:09
A YAML struct for Go
type Person struct {
Name string `yaml:"name"`
Age int `yaml:"age"`
}
@tdshipley
tdshipley / marshal_unmarshal_ymal_go.go
Last active August 31, 2015 22:23
An example of marshling and unmarshling YAML in GO
package main
import (
"gopkg.in/yaml.v2"
)
var data = `
- name: 'Thomas'
- age: 25
`
@tdshipley
tdshipley / ruby_precentage_sudo_code.rb
Created September 1, 2015 19:54
Sudo code for a ruby percentage method that would be nice to have
10.percentage_of 100 # => 10
# Taken from http://stackoverflow.com/questions/3668345/calculate-percentage-in-ruby
class Numeric
def percent_of(n)
self.to_f / n.to_f * 100.0
end
end
1.percent_of 10 # => 10.0 (%)