Tarring up the oldstuff
This commit is contained in:
parent
aa1345b6ed
commit
da7a8f5c14
BIN
oldstuff.tar
Normal file
BIN
oldstuff.tar
Normal file
Binary file not shown.
2
oldstuff/PracticingAlgorithms/.gitignore
vendored
2
oldstuff/PracticingAlgorithms/.gitignore
vendored
@ -1,2 +0,0 @@
|
||||
bin/*
|
||||
*.o
|
@ -1,14 +0,0 @@
|
||||
BINDIR := $(PWD)/bin
|
||||
CFLAGS := -std=c99 -Wall -g
|
||||
|
||||
export BINDIR CFLAGS
|
||||
|
||||
|
||||
all:
|
||||
cd src && $(MAKE)
|
||||
|
||||
clean:
|
||||
rm -f bin/*
|
||||
|
||||
|
||||
.PHONY: all clean
|
@ -1,10 +0,0 @@
|
||||
# Practicing Algorithms
|
||||
|
||||
I've been burned enough times when in a room with a bunch of CS Majors...
|
||||
(grumble.)
|
||||
|
||||
## MIT Open Courseware stuff
|
||||
|
||||
It's just like bookmarks, but on github!
|
||||
|
||||
- <a href="http://videolectures.net/mit6046jf05_leiserson_lec01/">Intro. to Algorithms lecture 01</a>
|
@ -1,10 +0,0 @@
|
||||
#!/bin/bash
|
||||
|
||||
SETUP="import sorting"
|
||||
LIST="$(python -c "import random;l = list(range(0, ${1-5000}));random.shuffle(l);print(l)")"
|
||||
|
||||
echo -n "insertion sort: "
|
||||
python -m timeit -s "$SETUP" "sorting.insertion_sort($LIST)"
|
||||
|
||||
echo -n "merge sort: "
|
||||
python -m timeit -s "$SETUP" "sorting.merge_sort($LIST)"
|
@ -1,49 +0,0 @@
|
||||
# vim:fileencoding=utf-8
|
||||
import math
|
||||
|
||||
|
||||
def insertion_sort(a):
|
||||
""" Θ(n^2) """
|
||||
for j in range(1, len(a)):
|
||||
key = a[j]
|
||||
i = j - 1
|
||||
while i >= 0 and a[i] > key:
|
||||
a[i + 1] = a[i]
|
||||
i = i - 1
|
||||
a[i + 1] = key
|
||||
|
||||
|
||||
def merge_sort(m):
|
||||
n = len(m)
|
||||
if n <= 1:
|
||||
return m
|
||||
|
||||
middle = math.ceil(n / 2)
|
||||
|
||||
left = m[:middle]
|
||||
right = m[middle:]
|
||||
|
||||
left = merge_sort(left)
|
||||
right = merge_sort(right)
|
||||
|
||||
return merge(left, right)
|
||||
|
||||
|
||||
def merge(left, right):
|
||||
result = []
|
||||
|
||||
while len(left) > 0 or len(right) > 0:
|
||||
if len(left) > 0 and len(right) > 0:
|
||||
if left[0] <= right[0]:
|
||||
result.append(left.pop(0))
|
||||
else:
|
||||
result.append(right.pop(0))
|
||||
|
||||
elif len(left) > 0:
|
||||
result.append(left.pop(0))
|
||||
|
||||
elif len(right) > 0:
|
||||
result.append(right.pop(0))
|
||||
|
||||
return result
|
||||
|
@ -1,12 +0,0 @@
|
||||
def insertion_sort(a)
|
||||
(0..(a.length - 1)).each do |j|
|
||||
key = a[j]
|
||||
i = j - 1
|
||||
while (i >= 0) && (a[i] > key)
|
||||
a[i + 1] = a[i]
|
||||
i = i - 1
|
||||
end
|
||||
a[i + 1] = key
|
||||
end
|
||||
end
|
||||
|
@ -1,8 +0,0 @@
|
||||
all: $(BINDIR)/insertion-sort
|
||||
|
||||
|
||||
$(BINDIR)/insertion-sort: insertion-sort.c
|
||||
gcc -o $@ $(CFLAGS) $^
|
||||
|
||||
|
||||
.PHONY: all
|
@ -1,38 +0,0 @@
|
||||
#include <stdio.h>
|
||||
|
||||
void insertion_sort(int * a, int len_a);
|
||||
|
||||
|
||||
int
|
||||
main(int argc, char ** argv)
|
||||
{
|
||||
int i;
|
||||
int l[] = {1, 4, 8, 9, 2, 3};
|
||||
|
||||
insertion_sort(l, 6);
|
||||
|
||||
for (i = 0; i < 6; i++) {
|
||||
printf("%d\n", l[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
insertion_sort(int * a, int len_a)
|
||||
{
|
||||
int i, j, key;
|
||||
|
||||
for (j = 1; j < len_a; j++) {
|
||||
key = a[j];
|
||||
i = j - 1;
|
||||
while ((i >= 0) && (a[i] > key)) {
|
||||
a[i + 1] = a[i];
|
||||
i = i - 1;
|
||||
}
|
||||
a[i + 1] = key;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* vim:filetype=c
|
||||
*/
|
@ -1,90 +0,0 @@
|
||||
import random
|
||||
import unittest
|
||||
|
||||
import sorting
|
||||
|
||||
|
||||
class TestInsertionSort(unittest.TestCase):
|
||||
|
||||
def test_example(self):
|
||||
tmpl = [8, 2, 4, 9, 3, 6]
|
||||
a = tmpl[:]
|
||||
expected = [2, 3, 4, 6, 8, 9]
|
||||
|
||||
sorting.insertion_sort(a)
|
||||
|
||||
self.assertEqual(expected, a)
|
||||
|
||||
def test_100_example(self):
|
||||
tmpl = list(range(0, 1000))
|
||||
a = tmpl[:]
|
||||
random.shuffle(a)
|
||||
expected = tmpl[:]
|
||||
|
||||
sorting.insertion_sort(a)
|
||||
|
||||
self.assertEqual(expected, a)
|
||||
|
||||
def test_reversed_100_example(self):
|
||||
tmpl = list(range(0, 1000))
|
||||
a = list(reversed(tmpl[:]))
|
||||
expected = tmpl[:]
|
||||
|
||||
sorting.insertion_sort(a)
|
||||
|
||||
self.assertEqual(expected, a)
|
||||
|
||||
def test_1000_example(self):
|
||||
tmpl = list(range(0, 1000))
|
||||
a = tmpl[:]
|
||||
random.shuffle(a)
|
||||
expected = tmpl[:]
|
||||
|
||||
sorting.insertion_sort(a)
|
||||
|
||||
self.assertEqual(expected, a)
|
||||
|
||||
|
||||
class TestMergeSort(unittest.TestCase):
|
||||
|
||||
def test_example(self):
|
||||
tmpl = [8, 2, 4, 9, 3, 6]
|
||||
a = tmpl[:]
|
||||
expected = [2, 3, 4, 6, 8, 9]
|
||||
|
||||
actual = sorting.merge_sort(a)
|
||||
|
||||
self.assertEqual(expected, actual)
|
||||
|
||||
def test_100_example(self):
|
||||
tmpl = list(range(0, 1000))
|
||||
a = tmpl[:]
|
||||
random.shuffle(a)
|
||||
expected = tmpl[:]
|
||||
|
||||
actual = sorting.merge_sort(a)
|
||||
|
||||
self.assertEqual(expected, actual)
|
||||
|
||||
def test_reversed_100_example(self):
|
||||
tmpl = list(range(0, 1000))
|
||||
a = list(reversed(tmpl[:]))
|
||||
expected = tmpl[:]
|
||||
|
||||
actual = sorting.merge_sort(a)
|
||||
|
||||
self.assertEqual(expected, actual)
|
||||
|
||||
def test_1000_example(self):
|
||||
tmpl = list(range(0, 1000))
|
||||
a = tmpl[:]
|
||||
random.shuffle(a)
|
||||
expected = tmpl[:]
|
||||
|
||||
actual = sorting.merge_sort(a)
|
||||
|
||||
self.assertEqual(expected, actual)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
@ -1,14 +0,0 @@
|
||||
require 'test/unit'
|
||||
require 'sorting'
|
||||
|
||||
|
||||
class TestSorting < Test::Unit::TestCase
|
||||
def test_insertion_sort
|
||||
tmpl = [8, 2, 4, 9, 3, 6]
|
||||
a = Array.new(tmpl)
|
||||
expected = [2, 3, 4, 6, 8, 9]
|
||||
|
||||
insertion_sort(a)
|
||||
assert_equal(expected, a)
|
||||
end
|
||||
end
|
@ -1,2 +0,0 @@
|
||||
export ANDROID_HOME="${ANDROID_HOME-"$HOME/opt/android-sdk-linux"}"
|
||||
export PATH="$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools:$PATH"
|
@ -1,2 +0,0 @@
|
||||
bin
|
||||
gen
|
@ -1,15 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="com.mbh"
|
||||
android:versionCode="1"
|
||||
android:versionName="1.0">
|
||||
<application android:label="@string/app_name" >
|
||||
<activity android:name="HelloAndroid"
|
||||
android:label="@string/app_name">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
</application>
|
||||
</manifest>
|
@ -1,17 +0,0 @@
|
||||
# This file is used to override default values used by the Ant build system.
|
||||
#
|
||||
# This file must be checked in Version Control Systems, as it is
|
||||
# integral to the build system of your project.
|
||||
|
||||
# This file is only used by the Ant script.
|
||||
|
||||
# You can use this to override default values such as
|
||||
# 'source.dir' for the location of your java source folder and
|
||||
# 'out.dir' for the location of your output folder.
|
||||
|
||||
# You can also use it define how the release builds are signed by declaring
|
||||
# the following properties:
|
||||
# 'key.store' for the location of your keystore and
|
||||
# 'key.alias' for the name of the key to use.
|
||||
# The password will be asked during the build when you use the 'release' target.
|
||||
|
@ -1,85 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project name="HelloAndroid" default="help">
|
||||
|
||||
<!-- The local.properties file is created and updated by the 'android' tool.
|
||||
It contains the path to the SDK. It should *NOT* be checked into
|
||||
Version Control Systems. -->
|
||||
<property file="local.properties" />
|
||||
|
||||
<!-- The ant.properties file can be created by you. It is only edited by the
|
||||
'android' tool to add properties to it.
|
||||
This is the place to change some Ant specific build properties.
|
||||
Here are some properties you may want to change/update:
|
||||
|
||||
source.dir
|
||||
The name of the source directory. Default is 'src'.
|
||||
out.dir
|
||||
The name of the output directory. Default is 'bin'.
|
||||
|
||||
For other overridable properties, look at the beginning of the rules
|
||||
files in the SDK, at tools/ant/build.xml
|
||||
|
||||
Properties related to the SDK location or the project target should
|
||||
be updated using the 'android' tool with the 'update' action.
|
||||
|
||||
This file is an integral part of the build system for your
|
||||
application and should be checked into Version Control Systems.
|
||||
|
||||
-->
|
||||
<property file="ant.properties" />
|
||||
|
||||
<!-- The project.properties file is created and updated by the 'android'
|
||||
tool, as well as ADT.
|
||||
|
||||
This contains project specific properties such as project target, and library
|
||||
dependencies. Lower level build properties are stored in ant.properties
|
||||
(or in .classpath for Eclipse projects).
|
||||
|
||||
This file is an integral part of the build system for your
|
||||
application and should be checked into Version Control Systems. -->
|
||||
<loadproperties srcFile="project.properties" />
|
||||
|
||||
<!-- quick check on sdk.dir -->
|
||||
<fail
|
||||
message="sdk.dir is missing. Make sure to generate local.properties using 'android update project' or to inject it through an env var"
|
||||
unless="sdk.dir"
|
||||
/>
|
||||
|
||||
|
||||
<!-- extension targets. Uncomment the ones where you want to do custom work
|
||||
in between standard targets -->
|
||||
<!--
|
||||
<target name="-pre-build">
|
||||
</target>
|
||||
<target name="-pre-compile">
|
||||
</target>
|
||||
|
||||
/* This is typically used for code obfuscation.
|
||||
Compiled code location: ${out.classes.absolute.dir}
|
||||
If this is not done in place, override ${out.dex.input.absolute.dir} */
|
||||
<target name="-post-compile">
|
||||
</target>
|
||||
-->
|
||||
|
||||
<!-- Import the actual build file.
|
||||
|
||||
To customize existing targets, there are two options:
|
||||
- Customize only one target:
|
||||
- copy/paste the target into this file, *before* the
|
||||
<import> task.
|
||||
- customize it to your needs.
|
||||
- Customize the whole content of build.xml
|
||||
- copy/paste the content of the rules files (minus the top node)
|
||||
into this file, replacing the <import> task.
|
||||
- customize to your needs.
|
||||
|
||||
***********************
|
||||
****** IMPORTANT ******
|
||||
***********************
|
||||
In all cases you must update the value of version-tag below to read 'custom' instead of an integer,
|
||||
in order to avoid having your file be overridden by tools such as "android update project"
|
||||
-->
|
||||
<!-- version-tag: 1 -->
|
||||
<import file="${sdk.dir}/tools/ant/build.xml" />
|
||||
|
||||
</project>
|
@ -1,10 +0,0 @@
|
||||
# This file is automatically generated by Android Tools.
|
||||
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
|
||||
#
|
||||
# This file must *NOT* be checked in Version Control Systems,
|
||||
# as it contains information specific to your local configuration.
|
||||
|
||||
# location of the SDK. This is only used by Ant
|
||||
# For customization when using a Version Control System, please read the
|
||||
# header note.
|
||||
sdk.dir=/home/me/opt/android-sdk-linux
|
@ -1,40 +0,0 @@
|
||||
-optimizationpasses 5
|
||||
-dontusemixedcaseclassnames
|
||||
-dontskipnonpubliclibraryclasses
|
||||
-dontpreverify
|
||||
-verbose
|
||||
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
|
||||
|
||||
-keep public class * extends android.app.Activity
|
||||
-keep public class * extends android.app.Application
|
||||
-keep public class * extends android.app.Service
|
||||
-keep public class * extends android.content.BroadcastReceiver
|
||||
-keep public class * extends android.content.ContentProvider
|
||||
-keep public class * extends android.app.backup.BackupAgentHelper
|
||||
-keep public class * extends android.preference.Preference
|
||||
-keep public class com.android.vending.licensing.ILicensingService
|
||||
|
||||
-keepclasseswithmembernames class * {
|
||||
native <methods>;
|
||||
}
|
||||
|
||||
-keepclasseswithmembers class * {
|
||||
public <init>(android.content.Context, android.util.AttributeSet);
|
||||
}
|
||||
|
||||
-keepclasseswithmembers class * {
|
||||
public <init>(android.content.Context, android.util.AttributeSet, int);
|
||||
}
|
||||
|
||||
-keepclassmembers class * extends android.app.Activity {
|
||||
public void *(android.view.View);
|
||||
}
|
||||
|
||||
-keepclassmembers enum * {
|
||||
public static **[] values();
|
||||
public static ** valueOf(java.lang.String);
|
||||
}
|
||||
|
||||
-keep class * implements android.os.Parcelable {
|
||||
public static final android.os.Parcelable$Creator *;
|
||||
}
|
@ -1,11 +0,0 @@
|
||||
# This file is automatically generated by Android Tools.
|
||||
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
|
||||
#
|
||||
# This file must be checked in Version Control Systems.
|
||||
#
|
||||
# To customize properties used by the Ant build system use,
|
||||
# "ant.properties", and override values to adapt the script to your
|
||||
# project structure.
|
||||
|
||||
# Project target.
|
||||
target=android-10
|
@ -1,13 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:orientation="vertical"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="fill_parent"
|
||||
>
|
||||
<TextView
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Hello World, HelloAndroid"
|
||||
/>
|
||||
</LinearLayout>
|
||||
|
@ -1,4 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<string name="app_name">HelloAndroid</string>
|
||||
</resources>
|
@ -1,16 +0,0 @@
|
||||
package com.mbh;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.os.Bundle;
|
||||
import android.widget.TextView;
|
||||
|
||||
public class HelloAndroid extends Activity {
|
||||
/** Called when the activity is first created. */
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
TextView tv = new TextView(this);
|
||||
tv.setText("Hello, Android");
|
||||
setContentView(tv);
|
||||
}
|
||||
}
|
6
oldstuff/PracticingC/.gitignore
vendored
6
oldstuff/PracticingC/.gitignore
vendored
@ -1,6 +0,0 @@
|
||||
*
|
||||
!gowrikumar/bin
|
||||
!gdbtut/bin
|
||||
!*.i
|
||||
!*.s
|
||||
!*.c
|
@ -1,21 +0,0 @@
|
||||
CD = cd
|
||||
RM = rm -v
|
||||
|
||||
CC := gcc
|
||||
CFLAGS := -std=c99 -Wall -g
|
||||
|
||||
ALL_TARGETS := $(patsubst %.c,%,$(wildcard *.c))
|
||||
|
||||
export CD RM CFLAGS
|
||||
|
||||
|
||||
all: $(ALL_TARGETS)
|
||||
$(CD) gowrikumar && $(MAKE)
|
||||
$(CD) gdbtut && $(MAKE)
|
||||
|
||||
clean:
|
||||
$(CD) gowrikumar && $(MAKE) clean
|
||||
$(CD) gdbtut && $(MAKE) clean
|
||||
|
||||
|
||||
.PHONY: all clean
|
@ -1,3 +0,0 @@
|
||||
I don't get paid to write C for a living, so I have to sneak in practice here
|
||||
and there. Maybe once the kiddo goes to college I can become a kernel hacker
|
||||
:-P (assuming there's still such a thing as a kernel.)
|
@ -1,14 +0,0 @@
|
||||
# tutorial exercises from http://www.unknownroad.com/rtfm/gdbtut/
|
||||
|
||||
BINDIR := $(PWD)/bin
|
||||
export BINDIR
|
||||
|
||||
|
||||
all:
|
||||
$(CD) src && $(MAKE)
|
||||
|
||||
clean:
|
||||
$(RM) $(BINDIR)/*
|
||||
|
||||
|
||||
.PHONY: all clean
|
@ -1,12 +0,0 @@
|
||||
# tutorial exercises from http://www.unknownroad.com/rtfm/gdbtut/
|
||||
|
||||
ALL_BIN := $(patsubst %.c,$(BINDIR)/%,$(wildcard *.c))
|
||||
|
||||
|
||||
$(BINDIR)/%: %.c
|
||||
$(CC) $(CFLAGS) -o $@ $<
|
||||
|
||||
all: $(ALL_BIN)
|
||||
|
||||
|
||||
.PHONY: all
|
@ -1,28 +0,0 @@
|
||||
/**
|
||||
* :author: Dan Buch (daniel.buch@gmail.com)
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
char *buf;
|
||||
long long huge = 8000000000000000000;
|
||||
/* Okay, so this is *not* going to segfault because
|
||||
* the way memory is allocated has changed since
|
||||
* the tutorial was written. The segfault is supposed
|
||||
* to happen when more memory is allocated than is
|
||||
* available on the machine. So much for that exercise.
|
||||
*/
|
||||
|
||||
buf = malloc(huge);
|
||||
|
||||
fgets(buf, 1024, stdin);
|
||||
printf("%s\n", buf);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* vim:filetype=c:fileencoding=utf-8
|
||||
*/
|
@ -1,14 +0,0 @@
|
||||
# puzzles from http://www.gowrikumar.com/c/
|
||||
|
||||
BINDIR := $(PWD)/bin
|
||||
export BINDIR
|
||||
|
||||
|
||||
all:
|
||||
$(CD) src && $(MAKE)
|
||||
|
||||
clean:
|
||||
$(RM) $(BINDIR)/*
|
||||
|
||||
|
||||
.PHONY: all clean
|
@ -1,23 +0,0 @@
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
int array[] = {23, 34, 12, 17, 204, 99, 16};
|
||||
#define TOTAL_ELEMENTS sizeof(array) / sizeof(array[0])
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
int d;
|
||||
|
||||
#ifdef DEBUG
|
||||
printf("sizeof(array) = %d\n", TOTAL_ELEMENTS);
|
||||
#endif
|
||||
|
||||
for (d = 0; d < TOTAL_ELEMENTS ; d++)
|
||||
printf("%d\n", array[d]);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* vim:filetype=c:fileencoding=utf-8
|
||||
*/
|
@ -1,20 +0,0 @@
|
||||
/**
|
||||
* :author: Dan Buch (daniel.buch@gmail.com)
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int i = 1;
|
||||
do
|
||||
{
|
||||
printf("%d\n", i);
|
||||
} while(++i < 15);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* vim:filetype=c:fileencoding=utf-8
|
||||
*/
|
@ -1,22 +0,0 @@
|
||||
/**
|
||||
* :author: Dan Buch (daniel.buch@gmail.com)
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
printf("(Hit ^C to stop the madness.)\n");
|
||||
while(1)
|
||||
{
|
||||
fprintf(stdout, "hello-out ");
|
||||
fprintf(stderr, "hello-err ");
|
||||
sleep(1);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/* vim:filetype=c:fileencoding=utf-8
|
||||
*/
|
@ -1,23 +0,0 @@
|
||||
/**
|
||||
* :author: Dan Buch (daniel.buch@gmail.com)
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
printf("This time we'll include \\n!\n");
|
||||
printf("(Hit ^C to stop the madness.)\n");
|
||||
while(1)
|
||||
{
|
||||
fprintf(stdout, "hello-out\n");
|
||||
fprintf(stderr, "hello-err\n");
|
||||
sleep(1);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/* vim:filetype=c:fileencoding=utf-8
|
||||
*/
|
@ -1,24 +0,0 @@
|
||||
/**
|
||||
* :author: Dan Buch (daniel.buch@gmail.com)
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
printf("This time we'll flush stdout!\n");
|
||||
printf("(Hit ^C to stop the madness.)\n");
|
||||
while(1)
|
||||
{
|
||||
fprintf(stdout, "hello-out ");
|
||||
fprintf(stderr, "hello-err ");
|
||||
fflush(stdout);
|
||||
sleep(1);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/* vim:filetype=c:fileencoding=utf-8
|
||||
*/
|
@ -1,19 +0,0 @@
|
||||
/**
|
||||
* :author: Dan Buch (daniel.buch@gmail.com)
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#define f(a, b) a##b
|
||||
#define g(a) #a
|
||||
#define h(a) g(a)
|
||||
|
||||
int main()
|
||||
{
|
||||
printf("%s\n", h(f(1, 2)));
|
||||
printf("%s\n", g(f(1, 2)));
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* vim:filetype=c:fileencoding=utf-8
|
||||
*/
|
@ -1,42 +0,0 @@
|
||||
/**
|
||||
* :author: Dan Buch (daniel.buch@gmail.com)
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#define f(a, b) a##b
|
||||
#define g(a) #a
|
||||
#define h(a) g(a)
|
||||
#define border(c) \
|
||||
for (int i = 0; i < 60; i++) { \
|
||||
printf(c); \
|
||||
} \
|
||||
printf("\n");
|
||||
|
||||
int main()
|
||||
{
|
||||
printf("%s\n", h(f(1, 2)));
|
||||
printf("%s\n", g(f(1, 2)));
|
||||
|
||||
printf("%s\n", g(printf("dogs rule cats drool\n")));
|
||||
|
||||
char * hambones = "Cats! " g(ham);
|
||||
char * bonesham = "Meats! " g(bones);
|
||||
char * tmp;
|
||||
|
||||
border("-");
|
||||
printf("bonesham = %s\n", bonesham);
|
||||
printf("hambones = %s\n", hambones);
|
||||
|
||||
border("-");
|
||||
tmp = f(ham, bones);
|
||||
f(ham, bones) = f(bones, ham);
|
||||
f(bones, ham) = tmp;
|
||||
|
||||
printf("bonesham = %s\n", bonesham);
|
||||
printf("hambones = %s\n", hambones);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* vim:filetype=c:fileencoding=utf-8
|
||||
*/
|
@ -1,24 +0,0 @@
|
||||
/**
|
||||
* :author: Dan Buch (daniel.buch@gmail.com)
|
||||
*/
|
||||
|
||||
#include<stdio.h>
|
||||
int main()
|
||||
{
|
||||
int a=10;
|
||||
switch(a)
|
||||
{
|
||||
case '1':
|
||||
printf("ONE\n");
|
||||
break;
|
||||
case '2':
|
||||
printf("TWO\n");
|
||||
break;
|
||||
defa1ut:
|
||||
printf("NONE\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* vim:filetype=c:fileencoding=utf-8
|
||||
*/
|
@ -1,32 +0,0 @@
|
||||
/**
|
||||
* :author: Dan Buch (daniel.buch@gmail.com)
|
||||
*/
|
||||
|
||||
#define MAGIC_NUMBER 10
|
||||
|
||||
#include<stdio.h>
|
||||
int main()
|
||||
{
|
||||
int a = MAGIC_NUMBER;
|
||||
switch(a)
|
||||
{
|
||||
case '1':
|
||||
printf("ONE\n");
|
||||
break;
|
||||
case '2':
|
||||
printf("TWO\n");
|
||||
break;
|
||||
defalut:
|
||||
printf("NO CAN SPELL\n");
|
||||
break;
|
||||
defau1t:
|
||||
printf("SO CLOSE, YET SO FAR\n");
|
||||
break;
|
||||
default:
|
||||
printf("NONE\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* vim:filetype=c:fileencoding=utf-8
|
||||
*/
|
@ -1,16 +0,0 @@
|
||||
/**
|
||||
* :author: Dan Buch (daniel.buch@gmail.com)
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int* p;
|
||||
p = (int*)malloc(sizeof(int));
|
||||
*p = 10;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* vim:filetype=c:fileencoding=utf-8
|
||||
*/
|
@ -1,29 +0,0 @@
|
||||
/**
|
||||
* :author: Dan Buch (daniel.buch@gmail.com)
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int* p;
|
||||
|
||||
size_t size_of_int = sizeof(int);
|
||||
printf("sizeof(int) = %d\n", size_of_int);
|
||||
|
||||
p = (int*)malloc(sizeof(int));
|
||||
|
||||
size_t size_of_p = sizeof(p);
|
||||
printf("sizeof(p) = %d\n", size_of_p);
|
||||
|
||||
*p = 10;
|
||||
|
||||
size_t size_of_ptr_p = sizeof(*p);
|
||||
printf("sizeof(*p) = %d\n", size_of_ptr_p);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* vim:filetype=c:fileencoding=utf-8
|
||||
*/
|
@ -1,40 +0,0 @@
|
||||
/**
|
||||
* :author: Dan Buch (daniel.buch@gmail.com)
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
static unsigned int mask[] = {
|
||||
0x55555555,
|
||||
0x33333333,
|
||||
0x0F0F0F0F,
|
||||
0x00FF00FF,
|
||||
0x0000FFFF
|
||||
};
|
||||
|
||||
|
||||
int get_bit_count(unsigned int x)
|
||||
{
|
||||
|
||||
int i;
|
||||
int shift; /* Number of positions to shift to right*/
|
||||
for (i = 0, shift = 1; i < 5; i++, shift *= 2) {
|
||||
x = (x & mask[i]) + ((x >> shift) & mask[i]);
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
int to_test[] = {0, 5, 7};
|
||||
int current;
|
||||
|
||||
for (int i = 0; i < 3 ; i++) {
|
||||
current = to_test[i];
|
||||
printf("get_bit_count(%d) = %d\n", current, get_bit_count(current));
|
||||
}
|
||||
}
|
||||
|
||||
/* vim:filetype=c:fileencoding=utf-8
|
||||
*/
|
@ -1,67 +0,0 @@
|
||||
/**
|
||||
* :author: Dan Buch (daniel.buch@gmail.com)
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
static unsigned int mask[] = {
|
||||
0x55555555,
|
||||
0x33333333,
|
||||
0x0F0F0F0F,
|
||||
0x00FF00FF,
|
||||
0x0000FFFF
|
||||
};
|
||||
|
||||
|
||||
int get_bit_count(unsigned int x)
|
||||
{
|
||||
printf("get_bit_count(%d)\n", x);
|
||||
|
||||
int i;
|
||||
int x_and_mask;
|
||||
int x_shifted;
|
||||
int x_shifted_and_mask;
|
||||
int shift; /* Number of positions to shift to right*/
|
||||
for (i = 0, shift = 1; i < 5; i++, shift *= 2) {
|
||||
printf(" START loop\n");
|
||||
printf(" i = %d, x = %d, shift = %d\n", i, x, shift);
|
||||
|
||||
x_and_mask = x & mask[i];
|
||||
printf(" x & mask[i] = %d\n", x_and_mask);
|
||||
|
||||
x_shifted = x >> shift;
|
||||
printf(" x >> shift = %d\n", x_shifted);
|
||||
|
||||
x_shifted_and_mask = x_shifted & mask[i];
|
||||
printf(" (x >> shift) & mask[i] = %d\n", x_shifted_and_mask);
|
||||
|
||||
x = x_and_mask + x_shifted_and_mask;
|
||||
printf(" x = %d\n", x);
|
||||
printf(" END loop\n");
|
||||
}
|
||||
|
||||
return x;
|
||||
}
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
int to_test[] = {0, 5, 7};
|
||||
int current;
|
||||
|
||||
/*
|
||||
size_t mask_len = sizeof(mask) / sizeof(unsigned int);
|
||||
|
||||
for (int i = 0; i < mask_len; i++) {
|
||||
printf("mask[%d] = %d\n", i, mask[i]);
|
||||
}
|
||||
*/
|
||||
|
||||
for (int i = 0; i < 3 ; i++) {
|
||||
current = to_test[i];
|
||||
printf("get_bit_count(%d) -> %d\n", current, get_bit_count(current));
|
||||
}
|
||||
}
|
||||
|
||||
/* vim:filetype=c:fileencoding=utf-8
|
||||
*/
|
@ -1,26 +0,0 @@
|
||||
/**
|
||||
* :author: Dan Buch (daniel.buch@gmail.com)
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
float f = 0.0f;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < 10; i++) {
|
||||
f = f + 0.1f;
|
||||
}
|
||||
|
||||
if (f == 1.0f) {
|
||||
printf("f is 1.0 \n");
|
||||
} else {
|
||||
printf("f is NOT 1.0\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* vim:filetype=c:fileencoding=utf-8
|
||||
*/
|
@ -1,43 +0,0 @@
|
||||
/**
|
||||
* :author: Dan Buch (daniel.buch@gmail.com)
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
float f = 0.0f;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < 10; i++) {
|
||||
f = f + 0.1f;
|
||||
}
|
||||
|
||||
if (f == 1.0) {
|
||||
printf("f is 1.0 \n");
|
||||
/* nope! */
|
||||
}
|
||||
|
||||
if (f == 1.000000) {
|
||||
printf("f is perhaps 1.000000?\n");
|
||||
/* nope again */
|
||||
}
|
||||
|
||||
printf("f is neither 1.0 nor 1.000000\n");
|
||||
printf("(f is really %.24f)\n", f);
|
||||
printf("OH NOES!\n");
|
||||
printf("But...\n");
|
||||
|
||||
double difference = fabs(f - 1.000000);
|
||||
|
||||
if (difference < 0.00001) {
|
||||
printf("f is close enough to 1.000000 (off by %.24f)\n", difference);
|
||||
/* yes? */
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* vim:filetype=c:fileencoding=utf-8
|
||||
*/
|
@ -1,20 +0,0 @@
|
||||
/**
|
||||
* :author: Dan Buch (daniel.buch@gmail.com)
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
/* FIXME: the comma after "a = 1" makes the compiler angry. Either the
|
||||
* ", 2" should be removed or another assignment should be added.
|
||||
int a = 1, 2;
|
||||
*/
|
||||
int a = 1, b = 2;
|
||||
printf("a: %d\n", a);
|
||||
printf("b: %d\n", b);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* vim:filetype=c:fileencoding=utf-8
|
||||
*/
|
@ -1,15 +0,0 @@
|
||||
/**
|
||||
* :author: Dan Buch (daniel.buch@gmail.com)
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int i = 43;
|
||||
printf("%d\n", printf("%d", printf("%d", i)));
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* vim:filetype=c:fileencoding=utf-8
|
||||
*/
|
@ -1,17 +0,0 @@
|
||||
/**
|
||||
* :author: Dan Buch (daniel.buch@gmail.com)
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int i = 43, nprinted;
|
||||
nprinted = printf("%d\n", printf("%d", printf("%d", i)));
|
||||
nprinted = printf("(the last one was %d characters)\n", nprinted);
|
||||
nprinted = printf("(and that was %d characters)\n", nprinted);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* vim:filetype=c:fileencoding=utf-8
|
||||
*/
|
@ -1,37 +0,0 @@
|
||||
/**
|
||||
* :author: Dan Buch (daniel.buch@gmail.com)
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
void duff(register char *to, register char *from, register int count)
|
||||
{
|
||||
register int n = (count + 7) / 8;
|
||||
switch (count % 8) {
|
||||
case 0: do { *to++ = *from++;
|
||||
case 7: *to++ = *from++;
|
||||
case 6: *to++ = *from++;
|
||||
case 5: *to++ = *from++;
|
||||
case 4: *to++ = *from++;
|
||||
case 3: *to++ = *from++;
|
||||
case 2: *to++ = *from++;
|
||||
case 1: *to++ = *from++;
|
||||
} while (--n > 0);
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
char * to = "dogs cats babies";
|
||||
char * from = "monkey hat pants";
|
||||
int i = 16;
|
||||
printf("to = %s\n", to);
|
||||
printf("from = %s\n", from);
|
||||
/* And here comes the segfault... */
|
||||
duff(to, from, i);
|
||||
printf("to = %s\n", to);
|
||||
printf("from = %s\n", from);
|
||||
}
|
||||
|
||||
/* vim:filetype=c:fileencoding=utf-8
|
||||
*/
|
@ -1,40 +0,0 @@
|
||||
/**
|
||||
* :author: Dan Buch (daniel.buch@gmail.com)
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
void duff(register char *to, register char *from, register int count)
|
||||
{
|
||||
register int n;
|
||||
|
||||
int remainder = count % 8;
|
||||
if (remainder == 0) {
|
||||
n = (count + 7) / 8;
|
||||
do {
|
||||
*to++ = *from++;
|
||||
} while (--n > 0);
|
||||
} else if (remainder <= 7 && remainder > 0) {
|
||||
int i;
|
||||
int c;
|
||||
for (i = 0; (c = from[i] && c != EOF); i++) {
|
||||
to[i] = c;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
char * to = "dogs cats babies";
|
||||
char * from = "monkey hat pants";
|
||||
int i = 11;
|
||||
printf("to = %s\n", to);
|
||||
printf("from = %s\n", from);
|
||||
/* And here comes the segfault... */
|
||||
duff(to, from, i);
|
||||
printf("to = %s\n", to);
|
||||
printf("from = %s\n", from);
|
||||
}
|
||||
|
||||
/* vim:filetype=c:fileencoding=utf-8
|
||||
*/
|
@ -1,32 +0,0 @@
|
||||
/**
|
||||
* :author: Dan Buch (daniel.buch@gmail.com)
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
int get_bit_count(unsigned int x)
|
||||
{
|
||||
int count=0;
|
||||
while(x)
|
||||
{
|
||||
count++;
|
||||
x = x&(x-1);
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
int to_test[] = {0, 5, 7};
|
||||
int current;
|
||||
|
||||
for (int i = 0; i < 3 ; i++) {
|
||||
current = to_test[i];
|
||||
printf("get_bit_count(%d) = %d\n", current, get_bit_count(current));
|
||||
}
|
||||
}
|
||||
|
||||
/* vim:filetype=c:fileencoding=utf-8
|
||||
*/
|
@ -1,12 +0,0 @@
|
||||
# puzzles from http://www.gowrikumar.com/c/
|
||||
|
||||
ALL_BIN := $(patsubst %.c,$(BINDIR)/%,$(wildcard *.c))
|
||||
|
||||
|
||||
$(BINDIR)/%: %.c
|
||||
$(CC) $(CFLAGS) -o $@ $<
|
||||
|
||||
all: $(ALL_BIN)
|
||||
|
||||
|
||||
.PHONY: all
|
@ -1,48 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/un.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
|
||||
const char *SOCKNAME = "/tmp/socky";
|
||||
int sfd;
|
||||
struct sockaddr_un addr;
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
sfd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
if (sfd == -1) {
|
||||
fprintf(stderr, "FAILED TO ALLOCATE SOCKET\n");
|
||||
goto error;
|
||||
}
|
||||
|
||||
memset(&addr, 0, sizeof(struct sockaddr_un));
|
||||
addr.sun_family = AF_UNIX;
|
||||
strncpy(addr.sun_path, SOCKNAME, sizeof(addr.sun_path) - 1);
|
||||
|
||||
if (bind(sfd, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)) == -1) {
|
||||
fprintf(stderr, "FAILED TO BIND SOCKET TO ADDRESS\n");
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (listen(sfd, 128) == -1) {
|
||||
fprintf(stderr, "FAILED TO LISTEN ON ADDRESS\n");
|
||||
goto error;
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
fprintf(stdout, "ATTEMPTING TO WAIT FOR REQUEST ON %s\n", SOCKNAME);
|
||||
if (accept(sfd, NULL, NULL) == -1) {
|
||||
fprintf(stderr, "FAILED TO ACCEPT REQUEST ON ADDRESS\n");
|
||||
goto error;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
error:
|
||||
return 1;
|
||||
|
||||
}
|
5
oldstuff/RubyFun/.gitignore
vendored
5
oldstuff/RubyFun/.gitignore
vendored
@ -1,5 +0,0 @@
|
||||
*.log
|
||||
koans/.path_progress
|
||||
*.sqlite3
|
||||
.rbenv-version
|
||||
*.rbc
|
@ -1,28 +0,0 @@
|
||||
# WRONG
|
||||
# aBlock = { |x| puts x }
|
||||
|
||||
# RIGHT
|
||||
aBlock = lambda { |x| puts x }
|
||||
|
||||
aBlock.call "Hello World!"
|
||||
|
||||
|
||||
def my_lambda(&aBlock)
|
||||
aBlock
|
||||
end
|
||||
|
||||
b = my_lambda { puts "Hello World My Way!" }
|
||||
b.call
|
||||
|
||||
aBlock = Proc.new { |x| puts x }
|
||||
aBlock = proc { |x| puts x }
|
||||
aBlock = lambda { |x| puts x }
|
||||
|
||||
add_lambda = lambda { |x,y| x + y }
|
||||
# add_lambda.call(4)
|
||||
# add_lambda.call(4,5,6)
|
||||
puts add_lambda.call(4,2)
|
||||
|
||||
add_procnew = Proc.new { |x,y| x + y }
|
||||
# add_procnew.call(4)
|
||||
puts add_procnew.call(4,5,6)
|
@ -1,30 +0,0 @@
|
||||
require 'pp'
|
||||
|
||||
def call_twice
|
||||
puts "Calling your block."
|
||||
ret1 = yield("very first")
|
||||
puts "The value of your block: #{ret1}"
|
||||
|
||||
puts "Calling your block again."
|
||||
ret2 = yield("second")
|
||||
puts "The value of your block: #{ret2}"
|
||||
end
|
||||
|
||||
call_twice do |which_time|
|
||||
puts "I'm a code block, called for the #{which_time} time."
|
||||
which_time == "very first" ? 1 : 2
|
||||
end
|
||||
|
||||
|
||||
class Hash
|
||||
def find_all
|
||||
new_hash = Hash.new
|
||||
each { |k,v| new_hash[k] = v if yield(k, v) }
|
||||
new_hash
|
||||
end
|
||||
end
|
||||
|
||||
squares = {0=>0, 1=>1, 2=>4, 3=>9}
|
||||
print "squares = "
|
||||
pp squares
|
||||
(squares.find_all { |key, value| key > 1 }).each { |k,v| puts "#{k}: #{v}" }
|
@ -1,39 +0,0 @@
|
||||
[1, 2, 3].each { |i| puts i }
|
||||
|
||||
[1, 2, 3].each do |i|
|
||||
if i % 2 == 0
|
||||
puts "#{i} is even."
|
||||
else
|
||||
puts "#{i} is odd."
|
||||
end
|
||||
end
|
||||
|
||||
1.upto 3 do |x|
|
||||
puts x
|
||||
end
|
||||
|
||||
1.upto(3) { |x| puts x }
|
||||
|
||||
hello = lambda { "Hello" }
|
||||
hello.call
|
||||
|
||||
log = lambda { |str| puts "[LOG] #{str}" }
|
||||
log.call("A test log message.")
|
||||
|
||||
{1=>2, 2=>4}.each { |k,v| puts "Key #{k}, value #{v}" }
|
||||
|
||||
def times_n(n)
|
||||
lambda { |x| x * n }
|
||||
end
|
||||
|
||||
times_ten = times_n(10)
|
||||
puts times_ten.call(5)
|
||||
puts times_ten.call(1.25)
|
||||
|
||||
circumference = times_n(2*Math::PI)
|
||||
puts circumference.call(10)
|
||||
puts circumference.call(3)
|
||||
puts [1, 2, 3].collect(&circumference)
|
||||
|
||||
ceiling = 50
|
||||
puts [1, 10, 49, 50.1, 200].select { |x| x < ceiling }
|
@ -1,44 +0,0 @@
|
||||
require 'open-uri'
|
||||
puts 'using "open-uri"'
|
||||
puts open('http://oreilly.com/').read(200)
|
||||
|
||||
# ------------------------------------------------------------
|
||||
require 'net/http'
|
||||
puts 'using "net/http"'
|
||||
response = Net::HTTP.get_response('oreilly.com', '/about/')
|
||||
puts 'response.code=' + response.code.to_s
|
||||
puts 'response.body.size=' + response.body.size.to_s
|
||||
puts 'response[\'Content-type\']=' + response['Content-type']
|
||||
puts 'response[0, 200]' + response.body[0, 200]
|
||||
|
||||
|
||||
# ------------------------------------------------------------
|
||||
require 'uri'
|
||||
puts 'using "net/http" with a URI'
|
||||
puts 'request object:' + \
|
||||
Net::HTTP.get(URI.parse("http://oreilly.com")).to_s
|
||||
response = Net::HTTP.get_response(URI.parse("http://oreilly.com/about/"))
|
||||
|
||||
# .........
|
||||
puts "Success!" if response.is_a? Net::HTTPOK
|
||||
|
||||
puts case response.code[0]
|
||||
when ?1 then "Status code indicates an HTTP informational response."
|
||||
when ?2 then "Status code indicates success."
|
||||
when ?3 then "Status code indicates redirection."
|
||||
when ?4 then "Status code indicates client error."
|
||||
when ?5 then "Status code indicates server error."
|
||||
else "Non-standard status code."
|
||||
end
|
||||
|
||||
puts 'Server=' + response['Server']
|
||||
puts 'SERVER=' + response['SERVER']
|
||||
|
||||
puts 'all keys:'
|
||||
response.each_key { |key| puts " #{key}" }
|
||||
|
||||
Net::HTTP.get_response('oreilly.com', '/about/') do |response|
|
||||
response.read_body do |segment|
|
||||
puts "Received segment of #{segment.size} byte(s)!"
|
||||
end
|
||||
end
|
@ -1,28 +0,0 @@
|
||||
require 'net/http'
|
||||
require 'uri'
|
||||
|
||||
uri = URI.parse("https://www.donotcall.gov/")
|
||||
puts 'uri=' + uri.to_s
|
||||
|
||||
request = Net::HTTP.new(uri.host, uri.port)
|
||||
# response = request.get('/')
|
||||
# ^--- would have resulted in an error
|
||||
|
||||
require 'net/https'
|
||||
puts 'making a request with use_ssl=true and verify_mode=VERIFY_NONE'
|
||||
request.use_ssl = true
|
||||
request.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
||||
response = request.get('/')
|
||||
|
||||
puts 'response.body.size=' + response.body.size.to_s
|
||||
|
||||
|
||||
request = Net::HTTP.new(uri.host, uri.port)
|
||||
puts 'making a request with use_ssl=true, ' + \
|
||||
'ca_path=/etc/ssl/certs and verify_mode=VERIFY_PEER'
|
||||
request.use_ssl = true
|
||||
request.ca_path = "/etc/ssl/certs/"
|
||||
request.verify_mode = OpenSSL::SSL::VERIFY_PEER
|
||||
response = request.get('/')
|
||||
|
||||
puts 'response.body.size=' + response.body.size.to_s
|
@ -1,64 +0,0 @@
|
||||
require 'net/http'
|
||||
require 'uri'
|
||||
|
||||
|
||||
# A simple wrapper method that accepts either strings or URI objects
|
||||
# and performs an HTTP GET.
|
||||
|
||||
module Net
|
||||
class HTTP
|
||||
def HTTP.get_with_headers(uri, headers=nil)
|
||||
uri = URI.parse(uri) if uri.respond_to? :to_str
|
||||
start(uri.host, uri.port) do |http|
|
||||
return http.get(uri.path, headers)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Let's get a web page in German.
|
||||
|
||||
res = Net::HTTP.get_with_headers('http://www.google.com/',
|
||||
{'Accept-Language' => 'de'})
|
||||
|
||||
# Check a bit of the body to make sure it's really in German
|
||||
s = res.body.size
|
||||
puts 'part of body in german=' + res.body[s-3670..s-3470].to_s
|
||||
|
||||
|
||||
Net::HTTP.get_with_headers('http://www.google.com/',
|
||||
{'User-Agent' => 'Ruby Web Browser v1.0'})
|
||||
|
||||
|
||||
uncompressed = Net::HTTP.get_with_headers('http://www.cnn.com/')
|
||||
puts 'uncompressed body size=' + uncompressed.body.size.to_s
|
||||
|
||||
gzipped = Net::HTTP.get_with_headers('http://www.cnn.com/',
|
||||
{'Accept-Encoding' => 'gzip'})
|
||||
puts 'gzipped Content-Encoding=' + gzipped['Content-Encoding']
|
||||
puts 'gzipped body size=' + gzipped.body.size.to_s
|
||||
|
||||
require 'zlib'
|
||||
require 'stringio'
|
||||
|
||||
body_io = StringIO.new(gzipped.body)
|
||||
unzipped_body = Zlib::GzipReader.new(body_io).read()
|
||||
|
||||
puts 'unzipped body size=' + unzipped_body.size.to_s
|
||||
|
||||
|
||||
uri = URI.parse('http://www.google.com/')
|
||||
request = Net::HTTP::Get.new(uri.path)
|
||||
['en_us', 'en', 'en_gb', 'ja'].each do |language|
|
||||
request.add_field('Accept-Language', language)
|
||||
end
|
||||
puts 'request[\'Accept-Language\']=' + request['Accept-Language'].to_s
|
||||
|
||||
Net::HTTP.start(uri.host, uri.port) do |http|
|
||||
response = http.request(request)
|
||||
puts 'response[\'Content-Type\']=' + response['Content-Type']
|
||||
puts 'response headers:'
|
||||
response.each_key do |key|
|
||||
puts " #{key}"
|
||||
end
|
||||
end
|
@ -1,29 +0,0 @@
|
||||
require 'resolv'
|
||||
require 'resolv-replace'
|
||||
|
||||
|
||||
def multiple_lookup(*names)
|
||||
dns = Resolv::DNS.new
|
||||
results = {}
|
||||
threads = []
|
||||
names.each do |name|
|
||||
threads << Thread.new(name) do |name|
|
||||
begin
|
||||
dns.each_address(name) { |a| (results[name] ||= []) << a }
|
||||
rescue Resolv::ResolvError
|
||||
results[name] = nil
|
||||
end
|
||||
end
|
||||
end
|
||||
threads.each { |t| t.join }
|
||||
return results
|
||||
end
|
||||
|
||||
|
||||
domains = ("a".."z").collect { |l| l + '.com' }
|
||||
multiple_lookup(*domains).sort.each do |name, addresses|
|
||||
if addresses
|
||||
puts "#{name}: #{addresses.size} " + \
|
||||
"address#{addresses.size == 1 ? "" : "es"}"
|
||||
end
|
||||
end
|
@ -1,11 +0,0 @@
|
||||
require 'resolv'
|
||||
|
||||
dns = Resolv::DNS.new
|
||||
domain = "oreilly.com"
|
||||
dns.each_resource(domain, Resolv::DNS::Resource::IN::MX) do |mail_server|
|
||||
puts mail_server.exchange
|
||||
end
|
||||
|
||||
dns.each_resource(domain, Resolv::DNS::Resource::IN::NS) do |nameserver|
|
||||
puts nameserver.name
|
||||
end
|
@ -1,3 +0,0 @@
|
||||
require 'resolv'
|
||||
|
||||
Resolv::DNS.new.each_address('oreilly.com') { |addr| puts addr }
|
@ -1,13 +0,0 @@
|
||||
require 'rubygems'
|
||||
require 'action_mailer'
|
||||
|
||||
|
||||
class SimpleMailer < ActionMailer::Base
|
||||
def headerful_message
|
||||
@headers['X-custom-header'] = 'Its value'
|
||||
body 'Body'
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
puts SimpleMailer.headerful_message
|
@ -1,18 +0,0 @@
|
||||
require 'rubygems'
|
||||
require 'net/smtp'
|
||||
require 'smtp-tls'
|
||||
|
||||
|
||||
body = <<EOM
|
||||
To: daniel.buch+rubytest@gmail.com
|
||||
From: daniel.buch@gmail.com
|
||||
Subject: minimalist message
|
||||
|
||||
AHOY
|
||||
EOM
|
||||
|
||||
Net::SMTP.start('smtp.gmail.com', 587, 'gmail.com',
|
||||
'daniel.buch@gmail.com', STDIN.readline, :login) do |smtp|
|
||||
smtp.send_message(body, 'daniel.buch@gmail.com',
|
||||
'daniel.buch+rubytest@gmail.com')
|
||||
end
|
@ -1,31 +0,0 @@
|
||||
require 'rubygems'
|
||||
require 'action_mailer'
|
||||
require 'mime/types'
|
||||
|
||||
|
||||
class SimpleMailer < ActionMailer::Base
|
||||
def directory_dump_message(recipient, directory)
|
||||
from 'directory-dump@localhost'
|
||||
recipients recipient
|
||||
subject "Dump of #{directory}"
|
||||
body %{Here are the files currently in "#{directory}":}
|
||||
|
||||
Dir.new(directory).each do |f|
|
||||
path = File.join(directory, f)
|
||||
if File.file? path
|
||||
mime_type = MIME::Types.of(f).first
|
||||
content_type = (mime_type ? mime_type.content_type :
|
||||
'application/octet-stream')
|
||||
attachments[f] = {
|
||||
:mime_type => mime_type,
|
||||
:encoding => ('quoted-printable' if content_type =~ /^text\//),
|
||||
:content => File.read(path)
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
puts SimpleMailer.directory_dump_message('daniel.buch+rubytest@gmail.com',
|
||||
'/tmp')
|
@ -1,28 +0,0 @@
|
||||
require 'rubygems'
|
||||
require 'action_mailer'
|
||||
|
||||
|
||||
class SimpleMailer < ActionMailer::Base
|
||||
def simple_message(recipient)
|
||||
from 'daniel.buch@gmail.com'
|
||||
recipients recipient
|
||||
subject 'A single-part message for you'
|
||||
body 'This message has a plain text body.'
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
puts 'generated message:'
|
||||
puts SimpleMailer.simple_message('daniel.buch+rubytest@gmail.com')
|
||||
|
||||
|
||||
ActionMailer::Base.smtp_settings = {
|
||||
:tls => true,
|
||||
:address => 'smtp.gmail.com',
|
||||
:port => "587",
|
||||
:domain => 'gmail.com',
|
||||
:user_name => 'daniel.buch@gmail.com',
|
||||
:password => STDIN.readline
|
||||
}
|
||||
|
||||
SimpleMailer.simple_message('daniel.buch+rubytest@gmail.com').deliver
|
@ -1,4 +0,0 @@
|
||||
.bundle
|
||||
db/*.sqlite3
|
||||
log/*.log
|
||||
tmp/
|
@ -1,31 +0,0 @@
|
||||
source 'http://rubygems.org'
|
||||
|
||||
gem 'rails', '3.0.9'
|
||||
|
||||
# Bundle edge Rails instead:
|
||||
# gem 'rails', :git => 'git://github.com/rails/rails.git'
|
||||
|
||||
gem 'sqlite3'
|
||||
|
||||
# Use unicorn as the web server
|
||||
# gem 'unicorn'
|
||||
|
||||
# Deploy with Capistrano
|
||||
# gem 'capistrano'
|
||||
|
||||
# To use debugger (ruby-debug for Ruby 1.8.7+, ruby-debug19 for Ruby 1.9.2+)
|
||||
# gem 'ruby-debug'
|
||||
# gem 'ruby-debug19', :require => 'ruby-debug'
|
||||
|
||||
# Bundle the extra gems:
|
||||
# gem 'bj'
|
||||
# gem 'nokogiri'
|
||||
# gem 'sqlite3-ruby', :require => 'sqlite3'
|
||||
# gem 'aws-s3', :require => 'aws/s3'
|
||||
|
||||
# Bundle gems for the local environment. Make sure to
|
||||
# put test-only gems in this group so their generators
|
||||
# and rake tasks are available in development mode:
|
||||
# group :development, :test do
|
||||
# gem 'webrat'
|
||||
# end
|
@ -1,76 +0,0 @@
|
||||
GEM
|
||||
remote: http://rubygems.org/
|
||||
specs:
|
||||
abstract (1.0.0)
|
||||
actionmailer (3.0.9)
|
||||
actionpack (= 3.0.9)
|
||||
mail (~> 2.2.19)
|
||||
actionpack (3.0.9)
|
||||
activemodel (= 3.0.9)
|
||||
activesupport (= 3.0.9)
|
||||
builder (~> 2.1.2)
|
||||
erubis (~> 2.6.6)
|
||||
i18n (~> 0.5.0)
|
||||
rack (~> 1.2.1)
|
||||
rack-mount (~> 0.6.14)
|
||||
rack-test (~> 0.5.7)
|
||||
tzinfo (~> 0.3.23)
|
||||
activemodel (3.0.9)
|
||||
activesupport (= 3.0.9)
|
||||
builder (~> 2.1.2)
|
||||
i18n (~> 0.5.0)
|
||||
activerecord (3.0.9)
|
||||
activemodel (= 3.0.9)
|
||||
activesupport (= 3.0.9)
|
||||
arel (~> 2.0.10)
|
||||
tzinfo (~> 0.3.23)
|
||||
activeresource (3.0.9)
|
||||
activemodel (= 3.0.9)
|
||||
activesupport (= 3.0.9)
|
||||
activesupport (3.0.9)
|
||||
arel (2.0.10)
|
||||
builder (2.1.2)
|
||||
erubis (2.6.6)
|
||||
abstract (>= 1.0.0)
|
||||
i18n (0.5.0)
|
||||
mail (2.2.19)
|
||||
activesupport (>= 2.3.6)
|
||||
i18n (>= 0.4.0)
|
||||
mime-types (~> 1.16)
|
||||
treetop (~> 1.4.8)
|
||||
mime-types (1.16)
|
||||
polyglot (0.3.2)
|
||||
rack (1.2.3)
|
||||
rack-mount (0.6.14)
|
||||
rack (>= 1.0.0)
|
||||
rack-test (0.5.7)
|
||||
rack (>= 1.0)
|
||||
rails (3.0.9)
|
||||
actionmailer (= 3.0.9)
|
||||
actionpack (= 3.0.9)
|
||||
activerecord (= 3.0.9)
|
||||
activeresource (= 3.0.9)
|
||||
activesupport (= 3.0.9)
|
||||
bundler (~> 1.0)
|
||||
railties (= 3.0.9)
|
||||
railties (3.0.9)
|
||||
actionpack (= 3.0.9)
|
||||
activesupport (= 3.0.9)
|
||||
rake (>= 0.8.7)
|
||||
rdoc (~> 3.4)
|
||||
thor (~> 0.14.4)
|
||||
rake (0.9.2)
|
||||
rdoc (3.9.1)
|
||||
sqlite3 (1.3.4)
|
||||
thor (0.14.6)
|
||||
treetop (1.4.10)
|
||||
polyglot
|
||||
polyglot (>= 0.3.1)
|
||||
tzinfo (0.3.29)
|
||||
|
||||
PLATFORMS
|
||||
ruby
|
||||
|
||||
DEPENDENCIES
|
||||
rails (= 3.0.9)
|
||||
sqlite3
|
@ -1,256 +0,0 @@
|
||||
== Welcome to Rails
|
||||
|
||||
Rails is a web-application framework that includes everything needed to create
|
||||
database-backed web applications according to the Model-View-Control pattern.
|
||||
|
||||
This pattern splits the view (also called the presentation) into "dumb"
|
||||
templates that are primarily responsible for inserting pre-built data in between
|
||||
HTML tags. The model contains the "smart" domain objects (such as Account,
|
||||
Product, Person, Post) that holds all the business logic and knows how to
|
||||
persist themselves to a database. The controller handles the incoming requests
|
||||
(such as Save New Account, Update Product, Show Post) by manipulating the model
|
||||
and directing data to the view.
|
||||
|
||||
In Rails, the model is handled by what's called an object-relational mapping
|
||||
layer entitled Active Record. This layer allows you to present the data from
|
||||
database rows as objects and embellish these data objects with business logic
|
||||
methods. You can read more about Active Record in
|
||||
link:files/vendor/rails/activerecord/README.html.
|
||||
|
||||
The controller and view are handled by the Action Pack, which handles both
|
||||
layers by its two parts: Action View and Action Controller. These two layers
|
||||
are bundled in a single package due to their heavy interdependence. This is
|
||||
unlike the relationship between the Active Record and Action Pack that is much
|
||||
more separate. Each of these packages can be used independently outside of
|
||||
Rails. You can read more about Action Pack in
|
||||
link:files/vendor/rails/actionpack/README.html.
|
||||
|
||||
|
||||
== Getting Started
|
||||
|
||||
1. At the command prompt, create a new Rails application:
|
||||
<tt>rails new myapp</tt> (where <tt>myapp</tt> is the application name)
|
||||
|
||||
2. Change directory to <tt>myapp</tt> and start the web server:
|
||||
<tt>cd myapp; rails server</tt> (run with --help for options)
|
||||
|
||||
3. Go to http://localhost:3000/ and you'll see:
|
||||
"Welcome aboard: You're riding Ruby on Rails!"
|
||||
|
||||
4. Follow the guidelines to start developing your application. You can find
|
||||
the following resources handy:
|
||||
|
||||
* The Getting Started Guide: http://guides.rubyonrails.org/getting_started.html
|
||||
* Ruby on Rails Tutorial Book: http://www.railstutorial.org/
|
||||
|
||||
|
||||
== Debugging Rails
|
||||
|
||||
Sometimes your application goes wrong. Fortunately there are a lot of tools that
|
||||
will help you debug it and get it back on the rails.
|
||||
|
||||
First area to check is the application log files. Have "tail -f" commands
|
||||
running on the server.log and development.log. Rails will automatically display
|
||||
debugging and runtime information to these files. Debugging info will also be
|
||||
shown in the browser on requests from 127.0.0.1.
|
||||
|
||||
You can also log your own messages directly into the log file from your code
|
||||
using the Ruby logger class from inside your controllers. Example:
|
||||
|
||||
class WeblogController < ActionController::Base
|
||||
def destroy
|
||||
@weblog = Weblog.find(params[:id])
|
||||
@weblog.destroy
|
||||
logger.info("#{Time.now} Destroyed Weblog ID ##{@weblog.id}!")
|
||||
end
|
||||
end
|
||||
|
||||
The result will be a message in your log file along the lines of:
|
||||
|
||||
Mon Oct 08 14:22:29 +1000 2007 Destroyed Weblog ID #1!
|
||||
|
||||
More information on how to use the logger is at http://www.ruby-doc.org/core/
|
||||
|
||||
Also, Ruby documentation can be found at http://www.ruby-lang.org/. There are
|
||||
several books available online as well:
|
||||
|
||||
* Programming Ruby: http://www.ruby-doc.org/docs/ProgrammingRuby/ (Pickaxe)
|
||||
* Learn to Program: http://pine.fm/LearnToProgram/ (a beginners guide)
|
||||
|
||||
These two books will bring you up to speed on the Ruby language and also on
|
||||
programming in general.
|
||||
|
||||
|
||||
== Debugger
|
||||
|
||||
Debugger support is available through the debugger command when you start your
|
||||
Mongrel or WEBrick server with --debugger. This means that you can break out of
|
||||
execution at any point in the code, investigate and change the model, and then,
|
||||
resume execution! You need to install ruby-debug to run the server in debugging
|
||||
mode. With gems, use <tt>sudo gem install ruby-debug</tt>. Example:
|
||||
|
||||
class WeblogController < ActionController::Base
|
||||
def index
|
||||
@posts = Post.find(:all)
|
||||
debugger
|
||||
end
|
||||
end
|
||||
|
||||
So the controller will accept the action, run the first line, then present you
|
||||
with a IRB prompt in the server window. Here you can do things like:
|
||||
|
||||
>> @posts.inspect
|
||||
=> "[#<Post:0x14a6be8
|
||||
@attributes={"title"=>nil, "body"=>nil, "id"=>"1"}>,
|
||||
#<Post:0x14a6620
|
||||
@attributes={"title"=>"Rails", "body"=>"Only ten..", "id"=>"2"}>]"
|
||||
>> @posts.first.title = "hello from a debugger"
|
||||
=> "hello from a debugger"
|
||||
|
||||
...and even better, you can examine how your runtime objects actually work:
|
||||
|
||||
>> f = @posts.first
|
||||
=> #<Post:0x13630c4 @attributes={"title"=>nil, "body"=>nil, "id"=>"1"}>
|
||||
>> f.
|
||||
Display all 152 possibilities? (y or n)
|
||||
|
||||
Finally, when you're ready to resume execution, you can enter "cont".
|
||||
|
||||
|
||||
== Console
|
||||
|
||||
The console is a Ruby shell, which allows you to interact with your
|
||||
application's domain model. Here you'll have all parts of the application
|
||||
configured, just like it is when the application is running. You can inspect
|
||||
domain models, change values, and save to the database. Starting the script
|
||||
without arguments will launch it in the development environment.
|
||||
|
||||
To start the console, run <tt>rails console</tt> from the application
|
||||
directory.
|
||||
|
||||
Options:
|
||||
|
||||
* Passing the <tt>-s, --sandbox</tt> argument will rollback any modifications
|
||||
made to the database.
|
||||
* Passing an environment name as an argument will load the corresponding
|
||||
environment. Example: <tt>rails console production</tt>.
|
||||
|
||||
To reload your controllers and models after launching the console run
|
||||
<tt>reload!</tt>
|
||||
|
||||
More information about irb can be found at:
|
||||
link:http://www.rubycentral.com/pickaxe/irb.html
|
||||
|
||||
|
||||
== dbconsole
|
||||
|
||||
You can go to the command line of your database directly through <tt>rails
|
||||
dbconsole</tt>. You would be connected to the database with the credentials
|
||||
defined in database.yml. Starting the script without arguments will connect you
|
||||
to the development database. Passing an argument will connect you to a different
|
||||
database, like <tt>rails dbconsole production</tt>. Currently works for MySQL,
|
||||
PostgreSQL and SQLite 3.
|
||||
|
||||
== Description of Contents
|
||||
|
||||
The default directory structure of a generated Ruby on Rails application:
|
||||
|
||||
|-- app
|
||||
| |-- controllers
|
||||
| |-- helpers
|
||||
| |-- mailers
|
||||
| |-- models
|
||||
| `-- views
|
||||
| `-- layouts
|
||||
|-- config
|
||||
| |-- environments
|
||||
| |-- initializers
|
||||
| `-- locales
|
||||
|-- db
|
||||
|-- doc
|
||||
|-- lib
|
||||
| `-- tasks
|
||||
|-- log
|
||||
|-- public
|
||||
| |-- images
|
||||
| |-- javascripts
|
||||
| `-- stylesheets
|
||||
|-- script
|
||||
|-- test
|
||||
| |-- fixtures
|
||||
| |-- functional
|
||||
| |-- integration
|
||||
| |-- performance
|
||||
| `-- unit
|
||||
|-- tmp
|
||||
| |-- cache
|
||||
| |-- pids
|
||||
| |-- sessions
|
||||
| `-- sockets
|
||||
`-- vendor
|
||||
`-- plugins
|
||||
|
||||
app
|
||||
Holds all the code that's specific to this particular application.
|
||||
|
||||
app/controllers
|
||||
Holds controllers that should be named like weblogs_controller.rb for
|
||||
automated URL mapping. All controllers should descend from
|
||||
ApplicationController which itself descends from ActionController::Base.
|
||||
|
||||
app/models
|
||||
Holds models that should be named like post.rb. Models descend from
|
||||
ActiveRecord::Base by default.
|
||||
|
||||
app/views
|
||||
Holds the template files for the view that should be named like
|
||||
weblogs/index.html.erb for the WeblogsController#index action. All views use
|
||||
eRuby syntax by default.
|
||||
|
||||
app/views/layouts
|
||||
Holds the template files for layouts to be used with views. This models the
|
||||
common header/footer method of wrapping views. In your views, define a layout
|
||||
using the <tt>layout :default</tt> and create a file named default.html.erb.
|
||||
Inside default.html.erb, call <% yield %> to render the view using this
|
||||
layout.
|
||||
|
||||
app/helpers
|
||||
Holds view helpers that should be named like weblogs_helper.rb. These are
|
||||
generated for you automatically when using generators for controllers.
|
||||
Helpers can be used to wrap functionality for your views into methods.
|
||||
|
||||
config
|
||||
Configuration files for the Rails environment, the routing map, the database,
|
||||
and other dependencies.
|
||||
|
||||
db
|
||||
Contains the database schema in schema.rb. db/migrate contains all the
|
||||
sequence of Migrations for your schema.
|
||||
|
||||
doc
|
||||
This directory is where your application documentation will be stored when
|
||||
generated using <tt>rake doc:app</tt>
|
||||
|
||||
lib
|
||||
Application specific libraries. Basically, any kind of custom code that
|
||||
doesn't belong under controllers, models, or helpers. This directory is in
|
||||
the load path.
|
||||
|
||||
public
|
||||
The directory available for the web server. Contains subdirectories for
|
||||
images, stylesheets, and javascripts. Also contains the dispatchers and the
|
||||
default HTML files. This should be set as the DOCUMENT_ROOT of your web
|
||||
server.
|
||||
|
||||
script
|
||||
Helper scripts for automation and generation.
|
||||
|
||||
test
|
||||
Unit and functional tests along with fixtures. When using the rails generate
|
||||
command, template test files will be generated for you and placed in this
|
||||
directory.
|
||||
|
||||
vendor
|
||||
External libraries that the application depends on. Also includes the plugins
|
||||
subdirectory. If the app has frozen rails, those gems also go here, under
|
||||
vendor/rails/. This directory is in the load path.
|
@ -1,7 +0,0 @@
|
||||
# Add your own tasks in files placed in lib/tasks ending in .rake,
|
||||
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
|
||||
|
||||
require File.expand_path('../config/application', __FILE__)
|
||||
require 'rake'
|
||||
|
||||
Hodgepodge::Application.load_tasks
|
@ -1,28 +0,0 @@
|
||||
class ApplicationController < ActionController::Base
|
||||
before_filter :set_user, :count_visits
|
||||
protect_from_forgery
|
||||
|
||||
private
|
||||
def count_visits
|
||||
value = (cookies[:visits] || '0').to_i
|
||||
cookies[:visits] = (value + 1).to_s
|
||||
@visits = cookies[:visits]
|
||||
end
|
||||
|
||||
protected
|
||||
def set_user
|
||||
@user = User.find(session[:id]) if @user.nil? && session[:id]
|
||||
end
|
||||
|
||||
def login_required
|
||||
return true if @user
|
||||
access_denied
|
||||
return false
|
||||
end
|
||||
|
||||
def access_denied
|
||||
session[:return_to] = request.fullpath
|
||||
flash[:error] = 'Oops. You need to login before you can view that page.'
|
||||
redirect_to :controller => 'user', :action => 'login'
|
||||
end
|
||||
end
|
@ -1,18 +0,0 @@
|
||||
class FooController < ApplicationController
|
||||
layout :figure_out_layout
|
||||
|
||||
def index
|
||||
end
|
||||
|
||||
def pretty
|
||||
end
|
||||
|
||||
def figure_out_layout
|
||||
if action_name =~ /pretty/
|
||||
'pretty'
|
||||
else
|
||||
'standard'
|
||||
end
|
||||
end
|
||||
|
||||
end
|
@ -1,5 +0,0 @@
|
||||
class HelloController < ApplicationController
|
||||
def world
|
||||
end
|
||||
|
||||
end
|
@ -1,6 +0,0 @@
|
||||
class IndexController < ApplicationController
|
||||
def index
|
||||
session[:first_time] ||= Time.now
|
||||
end
|
||||
|
||||
end
|
@ -1,20 +0,0 @@
|
||||
require 'sha1'
|
||||
|
||||
class ListController < ApplicationController
|
||||
def index
|
||||
@list = [1, "string", :symbol, ['list']]
|
||||
end
|
||||
|
||||
def shopping_list
|
||||
@list = [ListItem.new(4, 'aspirin'), ListItem.new(199, 'succotash')]
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
||||
class ListItem
|
||||
attr_accessor :name, :id
|
||||
def initialize(id, name)
|
||||
@id, @name = id, name
|
||||
end
|
||||
end
|
@ -1,25 +0,0 @@
|
||||
$one = 1
|
||||
|
||||
class NovelController < ApplicationController
|
||||
$two = 2
|
||||
|
||||
def index
|
||||
@title = 'Shattered View: a Novel on Rails'
|
||||
one_plus_one = 1 + 1
|
||||
increment_counter one_plus_one
|
||||
end
|
||||
|
||||
def helper_method
|
||||
@help_message = "I see you've come to me for help."
|
||||
end
|
||||
|
||||
def increment_counter(by)
|
||||
@counter ||= 0
|
||||
@counter += by
|
||||
end
|
||||
|
||||
def sequel
|
||||
$three = 3
|
||||
end
|
||||
|
||||
end
|
@ -1,5 +0,0 @@
|
||||
class PeopleController < ApplicationController
|
||||
def list
|
||||
end
|
||||
|
||||
end
|
@ -1,8 +0,0 @@
|
||||
class StatusController < ApplicationController
|
||||
def index
|
||||
@title = "System Status"
|
||||
time = Time.now
|
||||
@time = time
|
||||
@ps = `ps aux`
|
||||
end
|
||||
end
|
@ -1,28 +0,0 @@
|
||||
class UserController < ApplicationController
|
||||
before_filter :login_required, :only => :my_account
|
||||
|
||||
def login
|
||||
@user = User.new
|
||||
@user.username = params[:username]
|
||||
end
|
||||
|
||||
def process_login
|
||||
if user = User.authenticate(params[:user])
|
||||
session[:id] = user.id
|
||||
redirect_to session[:return_to] || '/'
|
||||
else
|
||||
flash[:error] = 'Invalid login.'
|
||||
redirect_to :action => 'login', :username => params[:user][:username]
|
||||
end
|
||||
end
|
||||
|
||||
def logout
|
||||
reset_session
|
||||
flash[:message] = 'Logged out.'
|
||||
redirect_to :action => 'login'
|
||||
end
|
||||
|
||||
def my_account
|
||||
end
|
||||
|
||||
end
|
@ -1,2 +0,0 @@
|
||||
module ApplicationHelper
|
||||
end
|
@ -1,2 +0,0 @@
|
||||
module FooHelper
|
||||
end
|
@ -1,2 +0,0 @@
|
||||
module IndexHelper
|
||||
end
|
@ -1,9 +0,0 @@
|
||||
require 'sha1'
|
||||
|
||||
|
||||
module ListHelper
|
||||
def create_li(item, i)
|
||||
%{<li class="#{ i % 2 == 0 ? 'even' : 'odd' }">#{i}:
|
||||
#{SHA1.new(item.object_id.to_s)}</li>}
|
||||
end
|
||||
end
|
@ -1,2 +0,0 @@
|
||||
module PeopleHelper
|
||||
end
|
@ -1,2 +0,0 @@
|
||||
module UserHelper
|
||||
end
|
@ -1,2 +0,0 @@
|
||||
class Person < ActiveRecord::Base
|
||||
end
|
@ -1,32 +0,0 @@
|
||||
require 'sha1'
|
||||
|
||||
|
||||
class User < ActiveRecord::Base
|
||||
attr_accessor :password
|
||||
attr_protected :hashed_password
|
||||
validates_uniqueness_of :username
|
||||
validates_confirmation_of :password, :if => lambda { |user|
|
||||
user.new_record? or not user.password.blank? }
|
||||
validates_length_of :password, :within => 5..40, :if => lambda { |user|
|
||||
user.new_record? or not user.password.blank? }
|
||||
|
||||
def self.hashed(str)
|
||||
SHA1.new(str).to_s
|
||||
end
|
||||
|
||||
def self.authenticate(user_info)
|
||||
user = find_by_username(user_info[:username])
|
||||
if user && user.hashed_password == hashed(user_info[:password])
|
||||
return user
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
before_save :update_password
|
||||
|
||||
def update_password
|
||||
if not password.blank?
|
||||
self.hashed_password = self.class.hashed(password)
|
||||
end
|
||||
end
|
||||
end
|
@ -1,2 +0,0 @@
|
||||
<h1>Foo#count</h1>
|
||||
<p>Find me in app/views/foo/count.html.erb</p>
|
@ -1,2 +0,0 @@
|
||||
<h1>Foo#index</h1>
|
||||
<p>Find me in app/views/foo/index.html.erb</p>
|
@ -1,2 +0,0 @@
|
||||
<h1>Foo#pretty</h1>
|
||||
<p>Find me in app/views/foo/pretty.html.erb</p>
|
@ -1,11 +0,0 @@
|
||||
<h1>Several increasingly silly ways of displaying “Hello world!”:</h1>
|
||||
|
||||
<p><%= "Hello world!" %></p>
|
||||
<p><%= "Hello" + " world!" %></p>
|
||||
<p><%= w = "world"
|
||||
"Hello #{w}!" %></p>
|
||||
<p><%= 'H' + ?e.chr + ('l' * 2) %><%= ('o word!').gsub('d', 'ld')%></p>
|
||||
|
||||
<% hello = "Hello" %>
|
||||
<% world = "world!" %>
|
||||
<%= hello %> <%= world %>
|
@ -1,4 +0,0 @@
|
||||
<p>You first visited this site on <%= session[:first_time] %>.</p>
|
||||
<p>That was <%= time_ago_in_words session[:first_time] %> ago.</p>
|
||||
|
||||
<p>You have visited this website's pages <%= @visits %> time(s).</p>
|
@ -1,9 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>My Website - <%= @title %></title>
|
||||
</head>
|
||||
<body>
|
||||
<%= yield %>
|
||||
</body>
|
||||
</html>
|
@ -1,9 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>BAR :: My Website - <%= @title %></title>
|
||||
</head>
|
||||
<body>
|
||||
<%= yield %>
|
||||
</body>
|
||||
</html>
|
@ -1,9 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>COUNT :: My Website - <%= @title %></title>
|
||||
</head>
|
||||
<body>
|
||||
<%= yield %>
|
||||
</body>
|
||||
</html>
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user