Roshan Book

My Tech Notebook

Tag Archives: ruby

Learning Ruby – Part 1

I always wanted to learn Ruby. Ruby and Python has been at the forefront of dynamic language revolution adding support to the old warhorse “Lisp” . Lisp is going to be the final frontier for me, but after Python, I wanted to taste water of Ruby.

I have started awesome video tutorials by thenewboston.com and finished first three lessons , which basically talked about setting up ruby on windows machine, running irb and doing some simple maths. Notes which I am going to list henceforth would be combination of “my notes+thenewboston instructions+various books+web research” .

Setting up Ruby on windows machine

1. Bucky has recommended rubyinstaller, which can be downloaded from here . But I have earlier came across another great installer called rails installer. It will set up everything for you,along with the rails support. So you don’t need to install anything , even if you decide to go with rails development later. You could get rails installer from here .

IRB

IRB is default console based program for playing with ruby code. Go to start>>type irb>>launch

Run simple math

Type 1+2 and hit enter. If you get ” =>3″ , you are ready to go to next lesson

Few Points to remember

Ruby considers new line, or ; as end of statement. If it detects + or / or – at end of line it considers it as continuation of statement.

Reserved Keywords in Ruby

BEGIN do next then
END else nill true
alias elsif not undef
and end or unless
begin ensure redo until
break false rescue when
case for retry while
class if return while
def in self __FILE__
defined? module super __LINE__

Ruby Begin statement, is the the set of codes which is run before running the program

Begin {}

Similarly Ruby End statement, are the codes which is run after program is ended

End {}

Comments are declared in Ruby with #

# I am a comment

To write several lines of comments we follow following syntax

=begin

comment 1

comment 2

=end

Another way for multi-line string is

tunam=<<mark

“line 1

“line 2

“mark

And it will print line  1 and line 2 in new line

Advertisements

Ruby lesson 1 basic

1. anything starting with ‘#’ is a comment
2. Classes names are in CamelCase while variable and functions are used_with_underscore
3. ALL_Uppercase for constants
4. Parenthesis () are for convenience and not a general rule. DO away with it whenever arguments are less, for conditional statements (if-else) and empty functions
5. puts is for print
6. You can insert multiple ruby statements in one line separated by ‘;’. Although try to follow one stement per line
7.

Starting with rails development

Installing

1. Install from http://railsinstaller.org/

2. Walkthrough

RailsInstaller 1.1.1 Demo from Engine Yard on Vimeo.

3. It will install all rails dependencies, gothub account, keys etc

4. Add the public key to your github account

Starting with rails

1. Create project : rails new appName

2. Starting the built in server – rails s

3. In cmd inside your project directory load .gitignore file by running

a) notepad .gitignore

b) A file having list of directories to be ignored is loaded up.

c) Delete all the entries and enter new entries from https://gist.github.com/732294 . They are

.bundle

db/*.sqlite3*

log/*.log

*.log

tmp/**/*

tmp/*

Gemfile.lock

doc/api

doc/app

*.swp

*~

.DS_Store

Starting git repository .  Visit http://www.blog.roshanbook.com/category/hacker/ for commands reference

Inside you app

1. git init

2. git add .

3. git status

4. git commit –m “comment”

5. git log

Untill now all the changes are inside local directory. To push into your git profile follow the following steps

6. git config –global user.name “manish roshan”

7. git config –global user.email manishroshan@gmail.com

8. git remote add origin git@github.com:manishroshan/test_app.git

9. git push origin master

This will publish your repository to github

Working on Branch

1. creating a branch – git checkout –b branchname

2. restore – git checkout –f

3. another way to restore – git reset –hardHEAD

4. commit all changes inside branch git commit –am “comment”

Restoring back to master file

5. Going as master- git checkout master

6. Getting branch list – git branch

7. Delete branch – git branch –D branchname

Starting a branch and merging with master

8. git checkout –b branchname

9. git mv oldfilename newfilename

10. git commit –am “comment”

11. git checkout master

12. Merging master and branch – git merge

13. check to see if everything is updated and nothing to commit-git status

14. git push

Deploying

1. Create login account at http://www.heroku.com/

2. In your cmd run : gem install heroku

3. add public key – heroku keys:add

4. create  a project on heroku – heroku create

5. from inside your app git push heroku master

6. To rename your app : heroku rename appname

Ruby Helpful Resources

Ruby Index

  1. Ruby Introduction
    1. Why Ruby
    2. Power of ruby
    3. Installing Ruby
  2. Ruby Helpful Resources
  3. Ruby Object Types
    1. Objects
    2. Variables
    3. Integers
    4. Floats
    5. Strings
    6. Arrays
    7. Array Methods
    8. Hashes
    9. Symbols
    10. Booleans
    11. Ranges
    12. Constants
  4. Control Structures
    1. Conditionals – If else elsif
    2. Conditional – unless, case
    3. Loops
    4. Iterators
  5. Code Block
    1. Definition
    2. Find
    3. Merge
    4. Collect
    5. Sort
    6. Inject
  6. Methods
    1. Defining and Calling Methods
    2. Variable Scope in Methods
    3. Arguments
    4. Argument default values
    5. return value
    6. Operators are also method
  7. Classes
    1. Defining and using classes
    2. Instances
    3. Attributes
    4. reader writer methods
    5. Attribute methods
    6. Initialize method
    7. Class methods
    8. Class attributes
    9. Class reader writer methods
    10. Inheritance
    11. Subclass overriding
    12. Accessing the superclass
  8. Modules
    1. Namespacing
    2. Mixins
    3. Load,require and include
    4. Enumerable as a mixin
  9. Working with files
    1. Input output basics
    2. File system basics
    3. File paths
    4. Accessing files
    5. Writing to files
    6. Reading from files
    7. File pointer
    8. Renaming and deleting files
    9. Examining file details
    10. Working with directories

Starting Ruby

%d bloggers like this: