Difference between revisions of "Ruby on Rails"
From Wikicliki
(22 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
− | + | Ruby is a language, rails is a framework. Ruby is a object-oriented, general purpose language. I attended a Railsbridge session at newcontext on 13 May 2012. These are some notes from that lesson. | |
− | object-oriented, general purpose | + | |
+ | * http://curriculum.railsbridge.org/curriculum | ||
+ | * [http://stark-mist-8840.herokuapp.com/topics my first ruby app] | ||
== rails philosophy == | == rails philosophy == | ||
Line 11: | Line 13: | ||
== the tools == | == the tools == | ||
* rails | * rails | ||
− | * rake - like | + | * rake - like make for ruby (an easy way to run tasks) |
* git - source code control | * git - source code control | ||
* database - SQlite | * database - SQlite | ||
− | * heroku - | + | * heroku - free rails hosting |
== the command line == | == the command line == | ||
+ | |||
+ | * Current directory: . ("dot") | ||
+ | * Parent directory: .. ("dot dot") | ||
+ | * Home directory: ~ ("tilde") | ||
+ | |||
* ls - list | * ls - list | ||
* cd - go to directory (use cd ~ to go backwards) | * cd - go to directory (use cd ~ to go backwards) | ||
Line 27: | Line 34: | ||
* rm - remove (DELETE FOREVER) | * rm - remove (DELETE FOREVER) | ||
* man <command> - tells you more (press q to get out) (man - manual) | * man <command> - tells you more (press q to get out) (man - manual) | ||
+ | * mkdir - make directory | ||
+ | * rmdir - remove directory | ||
+ | * mdfind - find file (find via spotlight) | ||
+ | Example: | ||
+ | * to rename a file frm file1 to file2 and move file to a different dir: mv file1 dir1/file2 | ||
− | * to | + | * Command-K to clear screen |
− | |||
+ | * cat <filename> - prints the file | ||
+ | * ruby <filename> - runs the file | ||
+ | * git status - check git (versioning) status | ||
+ | * git add . - prepare to commit everything all files | ||
+ | * git add <filename> - prepare to commit specific file | ||
+ | * git commit -m - commit that file | ||
+ | * git help <function name> | ||
+ | == irb test example == | ||
+ | <pre> | ||
+ | 1.9.3p194 :024 > def test (name="debbie", country="singapore") | ||
+ | 1.9.3p194 :025?> puts "My name is #{name}" | ||
+ | 1.9.3p194 :026?> puts "My country is #{country}" | ||
+ | 1.9.3p194 :027?> end | ||
+ | => nil | ||
+ | 1.9.3p194 :028 > test | ||
+ | My name is debbie | ||
+ | My country is singapore | ||
+ | => nil | ||
+ | 1.9.3p194 :029 > | ||
+ | </pre> | ||
+ | == standalone file test - newfile.rb == | ||
+ | use chomp to delete the extra line break. ! means WATCH OUT | ||
+ | <pre># Clear screen | ||
+ | puts "\e[H\e[2J" | ||
+ | |||
+ | # Ask | ||
+ | puts "What is your name?" | ||
+ | $name = STDIN.gets | ||
+ | puts "\e[H\e[2J" | ||
+ | $name.chomp! | ||
+ | # $name = $name.reverse | ||
+ | puts "Hello " + $name | ||
+ | puts "Where do you live?" | ||
+ | |||
+ | $place = STDIN.gets | ||
+ | puts "\e[H\e[2J" | ||
+ | puts "Hello "+$name+" who lives in " + $place</pre> | ||
+ | |||
+ | == pretty links == | ||
+ | |||
+ | the pretty permalinks are referred to as "slug" here, there are slugger and slugify plugins but those are more complicated. to_param will do to make a pretty link. this code goes into the model. | ||
+ | |||
+ | <pre>def to_param # overridden | ||
+ | "#{id}-#{title.parameterize}" | ||
+ | end | ||
+ | </pre> | ||
+ | |||
+ | == Naming Conventions == | ||
+ | * methods and variables are in snake_case | ||
+ | * classes and modules are in CamelCase | ||
+ | * constants are in ALL_CAPS | ||
+ | |||
+ | == Variables == | ||
+ | * var # local variable (or method call) | ||
+ | * @var # instance variable | ||
+ | * @@var # class variable | ||
+ | * $var # global variable | ||
+ | * VAR # constant | ||
+ | |||
+ | == CRUD == | ||
+ | |||
+ | * Create new records in the database | ||
+ | * Read or show the records in the database | ||
+ | * Update existing records | ||
+ | * Destroy or delete records | ||
== See Also == | == See Also == | ||
− | * http://www.devchix.com/ | + | * [https://www.ruby-toolbox.com/ Ruby Toolbox] |
+ | * [http://www.devchix.com/ devchix] | ||
* http://workshops.railsbridge.org/ | * http://workshops.railsbridge.org/ | ||
* http://curriculum.railsbridge.org/workshop | * http://curriculum.railsbridge.org/workshop | ||
− | * github | + | * [https://github.com/ github] |
− | * slidedown | + | * [http://nakajima.github.com/slidedown/ slidedown] - also see slidedown on [https://github.com/nakajima/slidedown github] |
+ | * http://mxcl.github.com/homebrew/ | ||
+ | |||
+ | [[Category:Programming]] |
Latest revision as of 02:47, 12 July 2013
Ruby is a language, rails is a framework. Ruby is a object-oriented, general purpose language. I attended a Railsbridge session at newcontext on 13 May 2012. These are some notes from that lesson.
Contents
rails philosophy
- opinionated
- convention over configuration
- don't repeat yourself
- test driven
- minimal code - maximal effect
the tools
- rails
- rake - like make for ruby (an easy way to run tasks)
- git - source code control
- database - SQlite
- heroku - free rails hosting
the command line
- Current directory: . ("dot")
- Parent directory: .. ("dot dot")
- Home directory: ~ ("tilde")
- ls - list
- cd - go to directory (use cd ~ to go backwards)
- pwd - "print working directory"
- echo - repeats it (used to echo out $PATH)
- touch - creates a file
- cat - tells info
- mv - move
- cp - copy
- rm - remove (DELETE FOREVER)
- man <command> - tells you more (press q to get out) (man - manual)
- mkdir - make directory
- rmdir - remove directory
- mdfind - find file (find via spotlight)
Example:
- to rename a file frm file1 to file2 and move file to a different dir: mv file1 dir1/file2
- Command-K to clear screen
- cat <filename> - prints the file
- ruby <filename> - runs the file
- git status - check git (versioning) status
- git add . - prepare to commit everything all files
- git add <filename> - prepare to commit specific file
- git commit -m - commit that file
- git help <function name>
irb test example
1.9.3p194 :024 > def test (name="debbie", country="singapore") 1.9.3p194 :025?> puts "My name is #{name}" 1.9.3p194 :026?> puts "My country is #{country}" 1.9.3p194 :027?> end => nil 1.9.3p194 :028 > test My name is debbie My country is singapore => nil 1.9.3p194 :029 >
standalone file test - newfile.rb
use chomp to delete the extra line break. ! means WATCH OUT
# Clear screen puts "\e[H\e[2J" # Ask puts "What is your name?" $name = STDIN.gets puts "\e[H\e[2J" $name.chomp! # $name = $name.reverse puts "Hello " + $name puts "Where do you live?" $place = STDIN.gets puts "\e[H\e[2J" puts "Hello "+$name+" who lives in " + $place
pretty links
the pretty permalinks are referred to as "slug" here, there are slugger and slugify plugins but those are more complicated. to_param will do to make a pretty link. this code goes into the model.
def to_param # overridden "#{id}-#{title.parameterize}" end
Naming Conventions
- methods and variables are in snake_case
- classes and modules are in CamelCase
- constants are in ALL_CAPS
Variables
- var # local variable (or method call)
- @var # instance variable
- @@var # class variable
- $var # global variable
- VAR # constant
CRUD
- Create new records in the database
- Read or show the records in the database
- Update existing records
- Destroy or delete records