Skip to content

Instantly share code, notes, and snippets.

@naohaq
naohaq / ptr.c
Created November 7, 2012 03:09
A compilation result of cast "const int *" into "int *"
#include <stdio.h>
void
f(int * p)
{
*p = 2;
}
int
main(int argc, char * argv[])
@naohaq
naohaq / block_if.rb
Created July 19, 2012 05:03
Represent then/else clauses as blocks.
#!/usr/bin/ruby1.9.1 -Ku
# -*- mode: ruby; coding: utf-8-unix -*-
class Conditional
class Else
def initialize(p,v)
@p = p
@v = v
end
@naohaq
naohaq / func_if.rb
Created July 19, 2012 04:44
Use "if" as a name of a method.
#!/usr/bin/ruby1.9.1 -Ku
# -*- mode: ruby; coding: utf-8-unix -*-
class Foo
def if(p,t,f)
if p.call then
t.call
else
f.call
end
@naohaq
naohaq / local.rb
Created June 22, 2012 08:15
Scoping of local variables in Ruby
colinux> ruby local.rb
foo(true):
a is not defined.
a is defined.
a = 1
foo(false):
a is not defined.
a is defined.
a = nil
@naohaq
naohaq / del0.c
Created June 20, 2012 01:15
Delete characters on heads of lines.
#include <stdio.h>
int
main( )
{
int c;
int newl = 0;
while((c = getchar( )) != EOF) {
if (c == '\r' || c == '\n') {
newl = 1;
@naohaq
naohaq / bar.rb
Created May 29, 2012 02:41
Top level local variable in Ruby
load "foo.rb"
puts a # Error!