Skip to content

Instantly share code, notes, and snippets.

View gr33n7007h's full-sized avatar
🌴
On vacation

Li gr33n7007h

🌴
On vacation
View GitHub Profile
@gr33n7007h
gr33n7007h / rstring.rb
Created November 16, 2020 21:38
Testing nested struct/union Fiddle version 1.0.1
require 'fiddle/import'
include Fiddle::Importer
# Fiddle v:1.0.1
dlload
typealias 'VALUE', 'unsigned long long'
RBasic = struct [
'VALUE flags',
@gr33n7007h
gr33n7007h / fdf.rb
Created July 20, 2020 03:46
Fast Doubling Fibonacci
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
@gr33n7007h
gr33n7007h / circle.rb
Created July 11, 2020 09:12
ascii circles.
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
>> 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)
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 February 28, 2020 20:16
Memory efficient image download
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 / 10-monitor.conf
Last active February 18, 2020 13:42
10-monitor.conf
# create 10-monitor.conf in /etc/X11/xorg.conf.d/
# add these lines;
Section "Monitor"
Identifier "VGA1"
Modeline "1440x900_75.00" 136.49 1440 1536 1688 1936 900 901 904 940 -HSync +Vsync
Option "PreferredMode" "1440x900_75.00"
EndSection
Section "Screen"
@gr33n7007h
gr33n7007h / fizzbuzz.rb
Created February 7, 2020 01:01
FizzBuzz using Ruby 2.7 pattern matching.
#!/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
}
@gr33n7007h
gr33n7007h / tp.rb
Last active November 6, 2019 16:51
TracePoint
#!/usr/bin/env ruby
require 'open3'
TracePoint.trace(:call) do |tp|
if tp.method_id == :capture3
m = tp.self.method(tp.method_id)
m.parameters.each do |_, param|
puts "#{tp.method_id} called with #{tp.binding.local_variable_get(param)}"
end
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