We are still actively working on the spam issue.

Difference between revisions of "Ruby on Rails"

From InstallGentoo Wiki
Jump to: navigation, search
m (category:howto)
 
(One intermediate revision by one other user not shown)
Line 158: Line 158:
  
 
[[Category:Programming languages]]
 
[[Category:Programming languages]]
 +
[[Category:HowTo]]

Latest revision as of 16:39, 24 February 2016

A programmer's best friend

Ruby is a general-purpose dynamic programming language. Having been inspired by Smalltalk, Ruby displays a very high degree of object orientation, but it also features Perl's strong text processing and Unix programming abilities and Lisp's metaprogramming facilities.

The language was created in 1995 by Yukihiro Matsumoto, often referred to as "Matz" in the Ruby community, and is now at version 2.1. Ruby has a strong focus on programmer happiness, its motto being "A programmer's best friend". It achieves that goal by being simple, powerful, practical and predictable. It features elegant and readable syntax that is easy to write and read, as well as nice APIs to use and a great standard library. When learning, you can often guess a method name correctly.

Notable projects written in Ruby:

  • Metasploit Framework - Penetration Testing
  • Vagrant - Virtualized Development Environments
  • Puppet - Server automation
  • Homebrew - OS X Package Manager
  • Cucumber - Behavior Driven Development
  • Sass & Compass - Enhanced CSS
  • Discourse - Next-generation Forum
  • Diaspora - Social Network
  • Rails & Sinatra - Web Frameworks

Books

See this page for Ruby book recommendations.

Installation

It is recommended to use Ruby in an Unix-like environment. Linux and OSX are great choices for Ruby programming, as they provide an extremely comfortable environment for developing Ruby software.

If you use Linux, your distribution very likely has a version of Ruby packaged and ready to be installed through the package manager. Search your repositories for a ruby implementation, install it and you're ready to go. That is the quickest, simplest way to install Ruby.

RVM

Many different implementations of Ruby exist. In order to live in such an heterogeneous environment, Rubyists created RVM. It was originally a tool to make switching between Ruby implementations as painless as possible, but has since evolved to be capable of much more.

RVM will:

  • Manage your entire Ruby environment
  • Fetch, patch, compile and install Ruby implementations
  • Allow you to switch between them seamlessly and at any time
  • Allow you to switch between them on a per-project basis
  • Allow you to create an isolated environment containing only the gems you want

Installing RVM

To install RVM, you can run the following command:

$ \curl -sSL https://get.rvm.io | bash -s stable

RVM will add itself to your shell's initialization file. Restart your terminal after it completes so that the changes will be applied. Verify that RVM is working as it should:

$ type rvm | head -n 1
rvm is a function

Installing implementations

After installing RVM, you can then install a Ruby implementation:

$ rvm install ruby-2.1.0

To get a list of available Ruby implementations, enter:

$ rvm list known

Windows

Ruby is supported on Windows via RubyInstaller. It includes pre-compiled binaries of the reference Ruby implementation as well as the standard library and executables. However, while it will work on Windows, it will be very awkward to run Ruby programs and develop projects due to the lack of a good terminal.

IDEs

Ruby enjoys some IDE support. The Eclipse Dynamic Languages Toolkit offers an integrated environment, and RubyMine, from JetBrains, is a full-fledged Ruby IDE.

However, Rubyists tend to not use IDEs, relying only on their trusty editor and terminal to develop software. This is most likely due to their choice in operating system. IDEs are valuable for Windows users.

Examples

Example code can illustrate the Ruby programming language, its syntax and features. The code can be typed directly into Interactive Ruby (irb) or saved to a file and subsequently executed by typing ruby code.rb.

Hello World!

puts 'Hello World!'

Basics

The basics of Ruby includes numbers, strings, and some data structures.

Numbers

2 + 2                       # => 4
2 ** 8                      # => 256

# Transparent big integers
2 ** 100                    # => 1267650600228229401496703205376

# Numbers are objects
1024.even?                  # => true
-10.abs                     # => 10

Local variables and strings

first = 'John'
last = 'Doe'

first.length                # => 4
last.index 'o'              # => 1

# String concatenation
first + ' ' + last          # => 'John Doe'

# String interpolation
"#{first} #{last}"          # => 'John Doe'

Arrays

name = [first, last]        # => ['John', 'Doe']

name.join                   # => 'JohnDoe'
name.join ' '               # => 'John Doe'
name.reverse.join ', '      # => 'Doe, John'

numbers = [1, 1, 2, 3, 5, 5, 6, 7]

numbers.uniq                # => [1, 2, 3, 5, 6, 7]

Hash tables and symbols

qualities = { water: 'Wet.', fire: 'Hot.' }

puts qualities[:water]  # prints "Wet."
qualities[:ruby] = 'Great!'

Gems

Ruby libraries are called gems. They enjoy full integration with the language and their own dedicated package manager. Most gems are downloaded via the RubyGems repository, which allows anyone to upload their own projects for distribution. You can, however, create your own gem server quite easily.

Visit the this RubyGems Guides page for a full command reference.

Installing a gem

To install a gem, run the following command:

$ gem install <gem name>

It will automatically resolve and install all dependencies. Some gems have native extensions that let Ruby interface with native code; those will have to be compiled, so ensure your development environment is sane.

Uninstalling a gem

To uninstall a gem, run the following command:

$ gem uninstall <gem name>