May 3 10

Learning Ruby and Rails Notes

by Dave

Strings

Strings are surrounded in quotes. For example, "David" is a string.

A complete list of Ruby string methods is here.

.reverse
"David".reverse
>> "divaD"

.length
"David".length
>> 5

.strip
Removes white space around a string.
"David ".strip
>> "David"

multiplication
"David" * 5
>> "DavidDavidDavidDavidDavid"

Converting Object Types

.to_s – converts to strings
.to_i – converts to integers
.to_a – converts to arrays

Arrays / Lists

Use brackets [].
[12, 47, 35]

.max
[12, 47, 35].max
>> 47

Hash / Dictionary / Associative Array

A hash is when you want to associate one thing with something else, like a dictionary. These are called Key, Value pairs.
Hashes use curly braces {}.

food["Fish"] = :yum
food["Dog"] = :yuck
food["Beef"] = :delicious

food
>> {"Fish" => :yum, "Dog" => :yuck, "Beef" => :delicious}
rating
food.keys
>> ["Fish", "Dog", "Beef"]

Blocks

Using the above example of food, you may use a block like the following to tally your results.

books.values.each { |rate| ratings[rate] += 1 }
>> [:yum, :delicious, :yuck]

ratings
>> {:yum => 1, :yuck => 1, :delicious => 1}

Another example of using a block:
5.times {print "Yes!"}
>> Yes!Yes!Yes!Yes!Yes!

Classes

String, Array, Hash are all examples of Classes.

Creating a class
class BlogEntry
attr_accessor :title, :time, :fulltext, :mood
end

attr_accessor is how you define variables, or in this case attributes attached to a class.

Let’s start a new entry.
entry = BlogEntry.new
entry.title = "Hello world!"
entry.fulltext = "Yes this is my first post!"
entry.mood = :sick
entry.time = Time.now

The initalize Method
Using the initalize method you won’t have to type the time every post.

class BlogEntry
def initalize( title, mood, fulltext )
@time = Time.now
@title, @mood, @fulltext = title, mood, fulltext
end
end

Important things to notice
Outside the class we use accessors: entry.time = Time.now. But inside we use instance variables: @time = Time.now.

Apr 12 10

Tim Ferriss – How to prepare for Public Speaking

by Dave

http://www.fourhourworkweek.com/blog/2010/04/11/public-speaking-how-i-prepare-every-time/

Apr 12 10

1,000 True Fans

by Dave

http://www.kk.org/thetechnium/archives/2008/03/1000_true_fans.php

Mar 10 10

5 Creepy ways Video Games are trying to get you addicted

by Dave

http://www.cracked.com/article_18461_5-creepy-ways-video-games-are-trying-to-get-you-addicted.html

Dec 4 09

Funny GIFs

by Dave

More after the fold!
read more…

Nov 18 09

Reoccuring billing systems for SaaS

by Dave

http://blog.recurly.com/2009/08/how-many-companies-does-it-take-to-process-a-credit-card/

https://cheddargetter.com/

http://chargify.com/

http://recurly.com/

Nov 18 09

Bad graphics performance on your Macbook Pro?

by Dave

I did not realise that you had to actually activate the 9600M GT on your Macbook Pro to enjoy any sort of graphical performance.

Do this by going into System Preferences, Energy Settings and setting it to Max performance.

Nov 9 09

Will Smith drops some life knowledge!

by Dave

Sep 10 09

960 Grid System

by Dave

Very fast way to prototype websites and give them that Web 2.0 look and spacing.

Normal: http://960.gs/
Fluid: http://www.designinfluences.com/fluid960gs/12/

Sep 8 09

Quote of the Day

by Dave

A user interface is well-designed when the program behaves exactly how the user thought it would.