Skip to content

Instantly share code, notes, and snippets.

@gr33n7007h
gr33n7007h / fdf.rb
Created Jul 20, 2020
Fast Doubling Fibonacci
View fdf.rb
def fdf(n)
return 1 if n <= 2
k = n >> 1
a = fdf(k + 1)
b = fdf(k)
n % 2 == 1 ? (a * a + b * b) : (b * (2 * a - b))
end
View circle.rb
def circle(radius: 10)
0.step(to: radius << 1, by: 1) do |x|
0.step(to: radius << 1, by: 1) do |y|
dist = ((x - radius).abs2 + (y - radius).abs2) ** 0.5
dist.between?(radius - 0.5, radius + 0.5) ? $> << ?. : $> << ' '
end
$> << $/
end
end
View mirror_update.hook
[Trigger]
Operation = Upgrade
Type = Package
Target = pacman-mirrorlist
[Action]
Description = Updating pacman-mirrorlist with reflector and removing pacnew...
When = PostTransaction
Depends = reflector
Exec = /bin/sh -c "reflector --protocol https --country 'United Kingdom' --score 10 --sort rate --save /etc/pacman.d/mirrorlist; rm -f /etc/pacman.d/mirrorlist.pacnew"
View disasm
>> puts RubyVM::InstructionSequence.compile('-2 ** 2').disasm
== disasm: #<ISeq:<compiled>@<compiled>:1 (1,0)-(1,7)> (catch: FALSE)
0000 putobject 2 ( 1)[Li]
0002 putobject 2
0004 opt_send_without_block <calldata!mid:**, argc:1, ARGS_SIMPLE>
0006 opt_send_without_block <calldata!mid:-@, argc:0, ARGS_SIMPLE>
0008 leave
=> nil
>> puts RubyVM::InstructionSequence.compile('-2.itself ** 2').disasm
== disasm: #<ISeq:<compiled>@<compiled>:1 (1,0)-(1,14)> (catch: FALSE)
View socket_state.rb
require 'socket'
TCP_STATES = {
1 => :established,
2 => :syn_sent,
3 => :syn_recv,
4 => :fin_wait1,
5 => :fin_wait2,
6 => :time_wait,
7 => :close,
@gr33n7007h
gr33n7007h / download.rb
Created Feb 28, 2020
Memory efficient image download
View download.rb
require 'open-uri'
uri = URI("https://i.imgur.com/NYPjmMN.jpg")
File.open('/path/you/want/to/save/image/blah.jpeg', 'wb') do |dest|
URI.open(uri) do |source|
IO.copy_stream(source, dest)
end
end
@gr33n7007h
gr33n7007h / fizzbuzz.rb
Created Feb 7, 2020
FizzBuzz using Ruby 2.7 pattern matching.
View fizzbuzz.rb
#!/usr/bin/env ruby
fizzbuzz = -> (n) {
case [n % 3, n % 5, n]
in 0, 0, _; "FizzBuzz"
in 0, _, _; "Fizz"
in _, 0, _; "Buzz"
else n
end
}
View ghost.rb
require 'fiddle/import'
class Object
extend Fiddle::Importer
RUBY_FL_SINGLETON = 1 << 12
RBasic = struct ['uintptr_t flags', 'uintptr_t klass']
private_constant :RUBY_FL_SINGLETON, :RBasic
View xco.rb
require 'yaml'
t = %i(name address tel fax email website).freeze
c = []
File.read('data.txt').split("\n\n").each do |line|
c << t.zip(line.split("\n")).to_h
end
puts c.to_yaml
@gr33n7007h
gr33n7007h / test.lua
Created Jun 5, 2019
Run Ruby from LuaJIT
View test.lua
#!/usr/bin/env luajit
local ffi = require 'ffi'
local cast = ffi.cast
local cdef = ffi.cdef
local load = ffi.load
local libruby = load '/usr/lib/libruby.so'
cdef[[
You can’t perform that action at this time.