Roshan Book

My Tech Notebook

Category Archives: Ruby

Learning Ruby Part 12 Changing data type and Declaring constant

c=13 (here c has a integer value)

c.to_f – This changes c data type to float

This type of data changing is called Casting

Similarly for strings

num = 10
num.to_s => “10”

Declaring a constant

Start with capital letter. Like Variable=10

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.

Installing Ruby on Rails Ubuntu 11.10 using RVM and Ruby 1.9.2

This post was originally published at http://www.the-tech-tutorial.com/?p=1868

Ruby Rails is a Web application framework created in 2004 intended as a rapid development web framework that runs the Ruby programing language. The latest version of the Ruby is ‘Ruby 1.9.2‘ and was realised in August 2011, it brings many new features and bug fixeses. The most signifiant changes are Block local variables, An additional lambda syntax, Per-string character encodings are supported and a new New socket API (IPv6 support). You can find a great walkthrough of all the new features at Ruby Inside. coinciding with the release of Ruby 1.9 is the Rails 3.1 framework which requires Ruby 1.8.7 or higher. RubyonRails 3.1 brings many new features, most notably: jQuery as default, HTTP Streaming, a new assets pipeline powered by Sprockets 2.0. So this is all good stuff and I’m sure you want to be working with the latest version of Ruby and RubyonRails, especially if you setting up a new server.

 

Well if your using Ubuntu you can setupRuby and RubyonRails quickly using the supplied repository’s, a good tutorial for this can be found here. However this is not recommended as the Ubuntu repositories are often out of date and if you install ruby orrails via the repository’s it can be pain toupgrade. So in this guide we are going to go through installing RVM (this allows you toinstall, change and upgrade the installedRuby version), Ruby 1.9.2 and Rails 3.1. This Guide has been tested on Ubuntu 11.10 however it should work on all versions of Ubuntu later then Ubuntu 9.04.

First of all you’ll need to install some packages that Ruby, RVM and RubyOnRailswill require later, the easiest way to do this is using the repositories so go to your terminal and type:

sudo apt-get update
sudo apt-get install build-essential git-core curl libmysqlclient16-dev nodejs

Now let’s install the latest version of RVM using curl:

bash < <(curl -s https://rvm.beginrescueend.com/install/rvm)
echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"' >> ~/.bashrc

Now you’ll need to ask RVM if it needs any more programs and if so you’ll need to install them to do this type:

rvm requirements

Two things could happen here if you see the following message saying you need to install ‘rvm‘, ignore this it and restart your terminal, this should fix the problem.

The program 'rvm' is currently not installed.  You can install it by typing:
sudo apt-get install ruby-rvm

You should see something that looks like this after you restart your terminal:

This tells you which packages you need to install using apt-get, just copy and paste the apt-get section under “# ForRuby” and run using the sudo command, this should look something like this:

sudo apt-get install build-essential openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev automake libtool bison subversion

Once you have installed all these packages its time to install Ruby 1.9.2 herself. The following command will installRuby 1.9.2 using the rmv package. However this can take a few minutes (15 in my case) to install so I recommend getting a drink, or my favourite, a pizza :lol: .

rvm install 1.9.2

Once ruby is installed you should set Ruby 1.9.2 as your default Ruby version, to do this enter:

rvm --default use 1.9.2

You can test that Ruby is installed correctly and running at version 1.9.2 by typing

ruby -v

This should output something like “ruby 1.9.2p290..” showing you that version 1.9.2p290 is installed and is now the defaultRuby version.

The nice thing about RVM is you can use it to install any version of Ruby and switch between the versions your have installed, to do this just enter “rvm install [Version Number]” then “rvm –default use [Version Number”, you can then use “rvm –default use” to change to any installed version.

Now that you have the latest version of ruby installed I’m sure you want to get the latest version of Rails installed, at the time of writing this tutorial that’s Rails 3.1. To installRails we can use GEM which is installed along with RVM and Ruby, to install the latest version of Rails just type, (this can take also take a while):

gem install rails

And that’s it you have now installed RVM, Ruby 1.9.2 and Rails 3.1 on Ubuntu 11.10 configured to use sqlite3, you can now setup your new Rails package with

rails new [project name]

Just as a final note, If you want to use MySQL instead of sqlite3 you’ll need to enter the following

sudo apt-get install mysql-server
gem install mysql2
rails new [project name] -d mysql

 

 

 

Creating to Deployment Rails application with scaffolding

1. rails new demo_app

2. rails generate scaffold User name:   user email  : string

3. You can destroy any generate function by firing destroy function like rails destroy user

4. create database as rake db:migrate

5. git status

6. git add .

7. git commit -m

8. rails console opens interactive console for rails app

9. heroku rake db.migrate – creates your database in heroku

Ruby Helpful Resources

Ruby Introduction

  1. Why Ruby
    1. Ruby is a general purpose language, can be used to write code once and run on various platforms. You just need a ruby interpreter for this.
    2. It is an interpreted language
    3. It has many avatars, viz Jruby, Iron-Ruby
  2. Power of ruby
    1. Below are the examples taken from http://www.ruby-lang.org/en/documentation/success-stories/
    2. Many people use Ruby in their daily jobs. Others just as a hobby. Here you’ll find a small sample of real world usage of Ruby.

      Simulations
      • A research group in Motorola uses Ruby to script a simulator, both to generate scenarios and to post process the data.
      3D Modeling
      • Google SketchUp is a 3D modeling application that uses Ruby for its macro scripting API.
      Business
      • Toronto Rehab uses a RubyWebDialogs based app to manage and track on-call and on-site support for the IT help desk and IT operations teams.
      Robotics
      • At MORPHA project, Ruby was used to implemented the reactive control part for the Siemens service robot.
      Networking
      • Open Domain Server uses Ruby to allow people using DynamicDNS clients to update in real time their IP configuration so that it can be mapped to static domains.
      Telephony
      • Ruby is being used within Lucent on a 3G wireless telephony product.
      System Administration
      • Ruby was used to write the central data collection portion ofLevel 3 Communications Unix Capacity and Planning system that gathers performance statistics from over 1700 Unix (Solaris and Linux) servers scattered around the globe.
      Web Applications
      • Basecamp, a web-based project management application developed by 37signals, is programmed entirely in Ruby.
      • 43 Things allows you to keep a list of goals and share it with the world. It was developed entirely in Ruby.
      • A List Apart, a magazine for people who make websites that has been around since 1997, has recently been revamped and uses a custom application built with Ruby on Rails.
      • Blue Sequence, a sophisticated mission-critical application which forms part of Toyota Motor Manufacturing’s own “sequence-in-time” production process, has recently been selected as finalist the British Computer (BCS) Information Management Awards.
      Security
      • The Metasploit Framework, a community open source project managed by Rapid7, is a free penetration testing platform that helps IT professionals assess the security of their networks and applications. The Metasploit Project consists of over 700,000 lines of code and has been downloaded over a million times in 2010. The commercial editions Metasploit Express andMetasploit Pro developed by Rapid7 are also based on Ruby.
  3. Installing Ruby
    1. You can download ruby from http://www.ruby-lang.org/en/downloads/
    2. When in windows, just run the installer and you are good to go.
    3. To check, open cmd and type irb –simple-prompt , if you get below screen, everything is good.

irb

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
%d bloggers like this: