Skip to content
April 1, 2010 / mattdyor

Everything you wanted to know about Ruby

Ruby

Classes always start with a capital letter
each class has attributes, specified by attr_accessor :name, :age, :gender

class Person
attr_accessor :name, :age, :gender
end

inheritance

class User < Person
attr_accessor :password
def hollar
puts “hollar”
end
end

iterate using do and end or { and }

“string”.scan(/./) {|x| puts x}

RAILS:

Overview:
Ruby and Ruby on Rails come pre-installed in the Mac
You will need to update Rails
..this requires XCode developer tools from Apple
..open Terminal, type “sudo gem update –system” enter “sudo gem install rails” enter “sudo gem update rake” enter “sudo gem install sqlite3-ruby” enter (i had to type “go”, but this might have been a rain dance)

To get to an interactive ruby session, type Terminal in spotlight (search in the top right corner) and type “irb” and hit enter.
Now you can type puts “hello world” and it should say hello world
You can also do some of the other ruby commands shown above

Terminal (like the command window)

cd = cd
ls = dir

Ruby Commands

This is a paint by numbers reference to help people remember steps needed to get a new machine or a new site. If you want to learn rails, go to http://www.ruby-doc.org/docs/

In Finder, create a new folder called RubyApps
Go to terminal and cd to RubyApps
type “rails XXX” where xxx is the app name you are going to build
BOOM…a lot of data starts flying by as Rails chunks out the skeleton of your app.

Random notes: if you want to get a copy of the documentation for rails, you can navigate to your newly created app directory (XXX) in Terminal and type in “rake rails:freeze:gems” enter “rake doc:rails” enter – after a few minutes of building the docs, you can navigate to /doc/api/index.html and check out the docs (feel free to move it to a location you can more readily find…these docs are not specific to the application but are for Rails generally)

Terminal: navigate to XXX and type “ruby script/server -d” (if you have Mongrel, OS will use that, otherwise, it will use WEBrick which is installed by default) To get Mongrel, type “gem install mongrel”

Now, in a browser you can check out http://localhost:3000 (localhost is the a reference to the name of the machine you are on…it a web server’s way of saying “me”).

In Terminal, To create a controller, navigate to XXX and type “ruby script/generate controller Home”

In finder,  navigate to XXX/app/controllers/home_controller.rb

Open this file up (if it is not opening in the editor of your choice, click on the more info > open with option). Add a action of your choice, such as “def hello” enter “end”

In finder, go to XXX/public/ and rename or delete index.html

In finder, go to XXX/app/views/hello and create a file called “hello.html.erb” (the name of the view corresponds to the action)

In terminal, type “script/server” to fire up the webserver

In web browser, navigate to localhost:3000/home/hello

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: