Skip to content

Instantly share code, notes, and snippets.

@nedzadarek
nedzadarek / strings_literal.rb
Created January 31, 2014 22:22
Closing and opening brackets - tests
# You should have the same amount of `{` as `}`
# 2
%Q{{hi}}
# => "{hi}"
# 4
%Q{{{{42}}}}
# => "{{{42}}}"
# `{`: 2; `}`:3
@nedzadarek
nedzadarek / fixnum_pry_to_s.rb
Last active August 29, 2015 14:02
Monkey patching `to_s` of Fixnum's class. Running it from the pry session as commands.
puts `ruby -e 'class Fixnum; def to_s; "WHAT"; end; end; puts 42.to_s; puts "#{3 * 4}";'`
# WHAT
# 12
# this one works only in the pry
# .ruby -e 'class Fixnum; def to_s; "WHAT"; end; end; puts 42.to_s; puts "#{3 * 4}";'
# WHAT
# 12
system(%q{ruby -e 'class Fixnum; def to_s; "WHAT"; end; end; puts 42.to_s; puts "#{3 * 4}";'})
@nedzadarek
nedzadarek / modules_extending.rb
Created June 19, 2014 09:26
`extend self` vs `extend A::B`
module A
# evil module:
module A
module B
def foo
:evil_foo
end
end
end
# good module:
@nedzadarek
nedzadarek / _id2ref.rb
Created July 24, 2014 12:36
Sending to the Redis
class RStruct
def initialize
# `@name` => just to show that code works
# you don't have to put it into `initialize`
@name = 'example'
@my_object_id = self.object_id
end
attr_reader :name, :my_object_id
def save_to_Redis
# some code
def correct? input
if 24 == input
puts "great job! you did it!"
true
else
puts "please try again"
false
end
end
@nedzadarek
nedzadarek / if_ternary.rb
Created December 17, 2014 18:08
if vs ternary operator(?)
# 1
# you have to use parens => `()`
# otherwise Ruby will threat it as `stuff if condition`
puts (if 1
then 2
else 3
end)
# 2
puts 1 \
@nedzadarek
nedzadarek / trying_snippet
Last active December 15, 2015 23:19
Nothing interesting
I wanted to try this.
So I added new line.
<!DOCTYPE html>
<html>
<head>
<title>HTML5 Video</title>
</head>
<body>
<!-- Write your video tag here: -->
<video controls="controls" src="">Your browser does not support the HTML5 video tag.</video>
</body>
</html>
@nedzadarek
nedzadarek / datalog_in_racket.rkt
Created April 13, 2013 14:27
How to use prolog in racket
#lang datalog
ancestor(A, B) :- parent(A, B).
ancestor(A, B) :-
parent(A, C), D = C, ancestor(D, B).
parent(john, douglas).
parent(bob, john).
ancestor(A, B)?
@nedzadarek
nedzadarek / compare2.c
Created April 13, 2013 14:41
Comparing 2 floats equality based on how different they are.
bezwgledna(float a) if (a<0) return (a-(2*a)); else return a;
if ( bezwgledna(liczba_float1 - liczba_float2) < 0.0001