BogoToBogo
  • Home
  • About
  • Big Data
  • Machine Learning
  • AngularJS
  • Python
  • C++
  • go
  • DevOps
  • Kubernetes
  • Algorithms
  • More...
    • Qt 5
    • Linux
    • FFmpeg
    • Matlab
    • Django 1.8
    • Ruby On Rails
    • HTML5 & CSS

Ruby On Rails - 1. Introduction 2020

RubyOnRails_logo




Bookmark and Share





bogotobogo.com site search:




Ruby
  1. A gem is a packaged Ruby application or library.
    Each gem has a name, version, and platform.
    For example, the rake gem has a 10.3.2 version.
    Rake's platform is ruby, which means it works on any platform Ruby runs on.

    rake-tree.png

    Here are the major components of a gem:

    1. The lib directory contains the code for the gem.
    2. The test or spec directory contains tests, depending on which test framework the developer uses.
    3. A gem usually has a Rakefile, which the rake program uses to automate tests, generate code, and perform other tasks.
    4. This gem also includes an executable file in the bin directory, which will be loaded into the user's PATH when the gem is installed.
    5. Documentation is usually included in the README and inline with the code.
    6. The final piece is the gemspec, which contains information about the gem. The gem's files, test information, platform, version number and more are all laid out here along with the author's email and name.




Ruby on Rails

Ruby on Rails emphasizes the use of well-known software engineering patterns and principles, such as active record pattern, convention over configuration (CoC), don't repeat yourself (DRY), and MVC.

Ruby is one of the popular programming language largely due to Rails. The Rails is a software library and an API that extends the Ruby. It is a framework for building websites. Rails is a Ruby gem (a gem is a packaged Ruby application or library). So, Rails is a package that run on top of Ruby.

Rails provides an extensive set of code generators, automated testing scripts and other features that help us to make the job of programming a web application easier.

As part of Rails ecosystem, a suite of additional tools are provides that make it easy to deploy a web application:

  1. rake - comes from RubyMAKE, it's a utility similar to make. We use it to create and migrate databases, clear web session etc.
  2. WEBrick - web server for hosting Rails web applications. We can also use other web servers, such as Apache, Unicorn, Thin, etc.
  3. SQLite - a simple database engine pre-installed with Rails.

For more info, What is Ruby on Rails?.




Install Ruby

To install Ruby: ruby-lang.org.

Using rvm (Ruby Version Manager), we can install Ruby on Ubuntu 14.04 like this:

$ sudo apt-get update
$ sudo apt-get install git-core curl zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev python-software-properties
$ sudo apt-get install libgdbm-dev libncurses5-dev automake libtool bison libffi-dev
$ curl -L https://get.rvm.io | bash -s stable
$ source ~/.rvm/scripts/rvm
$ echo "source ~/.rvm/scripts/rvm" >> ~/.bashrc
$ rvm install 2.1.3
$ rvm use 2.1.3 --default
$ echo "gem: --no-ri --no-rdoc" > ~/.gemrc




What is RVM?

RVM is similar to virtualenv in Python. RVM works virtually the same way how virtualenv works since it lets us sandbox different ruby versions and their gems.

The following sample shows how the Python virtualenv is working: from the current Python2, we can switch to Python3 in a sandboxed environment:

$ python
Python 2.7.6 (default, Jun 22 2015, 17:58:13) 
>>> 

$ virtualenv -p python3 venv3
Running virtualenv with interpreter /usr/bin/python3
Using base prefix '/usr'
New python executable in venv3/bin/python3
Also creating executable in venv3/bin/python
Installing setuptools, pip...done.

$ python
Python 2.7.6 (default, Jun 22 2015, 17:58:13) 
>>> 

$ source venv3/bin/activate
(venv3)k@laptop:~$ python
Python 3.4.0 (default, Jun 19 2015, 14:20:21) 
>>> 

Similarly, via RVM, we can install and use different versions of Ruby.

$ ruby -v
ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-linux]

Now, we want to install ruby 2.1.3:

$ rvm install ruby-2.1.3
...
ruby-2.1.3 - #generating default wrappers........

All we have to do is to specify which ruby we want to use:

$ rvm use 2.1.3
Using /home/k/.rvm/gems/ruby-2.1.3

$ ruby -v
ruby 2.1.3p242 (2014-09-19 revision 47630) [x86_64-linux]

$ rvm list

rvm rubies

 * ruby-2.1.2 [ x86_64 ]
=> ruby-2.1.3 [ x86_64 ]

# => - current
# =* - current && default
#  * - default

Now, our ruby version has been switched to 2.1.3 from 2.1.2!

We can set default version to 2.1.3:

$ rvm --default use 2.1.3
Using /home/k/.rvm/gems/ruby-2.1.3
k@laptop:~/PyGoogle$ rvm list

rvm rubies

   ruby-2.1.2 [ x86_64 ]
=* ruby-2.1.3 [ x86_64 ]

# => - current
# =* - current && default
#  * - default




What is bundler?

Bundler is a program for managing gem dependencies in our Ruby projects. With Bundler we can specify which gems our program needs, what versions they should be at, it can help us install them, load them at runtime and distribute them with our software.

Bundler provides a simple way to install gems. No more copying and pasting lists of gems, or even using the gem command manually. All of this is replaced by a simple command:

$ bundle install

The bundle command installs the dependencies specified in our Gemfile.

Note that in order to use bundle command, we need to install bundler first:

$ sudo gem install bundler

Then we will be able to use bundle command:

$ sudo bundle ...




What is Gemfile?

Gemfile keeps the list of gem dependencies of our Rails application. The dependencies are met by Bundler by running bundle install.

"Bundler maintains a consistent environment for ruby applications. It tracks an application's code and the rubygems it needs to run, so that an application will always have the exact gems (and versions) that it needs to run." "We designed bundler to make it easy to share your code across a number of development, staging and production machines. Of course, you know how to share your own application or gem: stick it on GitHub and clone it where you need it. Bundler makes it easy to make sure that your application has the dependencies it needs to start up and run without errors."

Again, analogous to Python, it's similar to requirement.txt when we do pip install -r requirement.txt.





Interactive Ruby

In this tutorial, we're going to use Ruby 2.1.2:

$ ruby -v
ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-linux]

Running IRB (which stands for Interactive Ruby) depends on the system:

  1. Linux, Mac OSX - irb
  2. Windows, from the Start Menu, open fxri from the Ruby section.
irb(main):001:0> 2014 * 2
=> 4028
irb(main):002:0> a = 5
=> 5
irb(main):003:0> b = 7
=> 7
irb(main):004:0> Math.sqrt(a+b)
=> 3.4641016151377544
irb(main):005:0> "Hello World"
=> "Hello World"
irb(main):006:0> 

To stop Ruby, type in "exit", "quit", "Ctrl-z", or "Ctrl-d".


Methods

The code def foo starts the definition of the method. It tells Ruby that we're defining a method whose name is foo. The next line is the body of the method, puts "Hello Ruby!". In the last line, end tells Ruby we're done defining the method. This response could be ==>foo or => nil depending on versions.

$ irb
irb(main):001:0> def foo
irb(main):002:1> puts "Hello Ruby!"
irb(main):003:1> end
=> nil
irb(main):004:0> foo
Hello Ruby!
=> nil
irb(main):005:0> foo()
Hello Ruby!
=> nil
irb(main):006:0> 



Methods with arguments

Now we want to define another function bar() to take a name as a parameter:

$ irb
irb(main):001:0> def bar(name)
irb(main):002:1> puts "Hello #{name}!"
irb(main):003:1> end
=> nil
irb(main):004:0> bar("Ruby")
Hello Ruby!
=> nil
irb(main):005:0> 

The #{name} is used for inserting something into a string. The bit between the braces is turned into a string.

The following code demonstrates two things:

  1. We can capitalized the string using #{name.capitalize}.
  2. We can set a default parameter as shown in the line, def bar2(name = "World!").
$ irb
irb(main):001:0> def bar2(name = "World!")
irb(main):002:1> puts "Hello #{name.capitalize}!"
irb(main):003:1> end
=> nil
irb(main):004:0> bar2 "ruby"
Hello Ruby!
=> nil
irb(main):005:0> bar2
Hello World!!
=> nil
irb(main):006:0> 


Class

We may want to use an object. Let's create a "Hello" class:

$ irb
irb(main):001:0> class Hello
irb(main):002:1>   def initialize(name = "World")
irb(main):003:2>     @name = name
irb(main):004:2>   end
irb(main):005:1>   def hi
irb(main):006:2>     puts "Hi #{@name}!"
irb(main):007:2>   end
irb(main):008:1>   def bye
irb(main):009:2>     puts "Bye #{@name}, see you soon."
irb(main):010:2>   end
irb(main):011:1> end
=> nil
irb(main):012:0> 

We introduced a new keyword, class. This defines a new class called Hello and a couple of methods for that class. Note the instance variable @name which is available to all the methods of the class.

Now let's create a Hello object and use it:

irb(main):012:0> h = Hello.new("Ruby")
=> #
irb(main):013:0> h.hi
Hi Ruby!
=> nil
irb(main):014:0> h.bye
Bye Ruby, see you soon.
=> nil

Once the h object is created, it remembers that the name is "Ruby". Then, what if we want to get the name directly?

irb(main):015:0> h.@name
SyntaxError: (irb):15: syntax error, unexpected tIVAR
	from /usr/bin/irb:12:in `<main>'
irb(main):016:0> 

As we can see from the run, we cannot get the name directly.







Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization

YouTubeMy YouTube channel

Sponsor Open Source development activities and free contents for everyone.

Thank you.

- K Hong






Sponsor Open Source development activities and free contents for everyone.

Thank you.

- K Hong







Ruby on Rails



Ruby On Rails Home

Ruby - Input/Output, Objects, Load

Ruby - Condition (if), Operators (comparison/logical) & case statement

Ruby - loop, while, until, for, each, (..)

Ruby - Functions

Ruby - Exceptions (raise/rescue)

Ruby - Strings (single quote vs double quote, multiline string - EOM, concatenation, substring, include, index, strip, justification, chop, chomp, split)

Ruby - Class and Instance Variables

Ruby - Class and Instance Variables II

Ruby - Modules

Ruby - Iterator : each

Ruby - Symbols (:)

Ruby - Hashes (aka associative arrays, maps, or dictionaries)

Ruby - Arrays

Ruby - Enumerables

Ruby - Filess

Ruby - code blocks and yield

Rails - Embedded Ruby (ERb) and Rails html

Rails - Partial template

Rails - HTML Helpers (link_to, imag_tag, and form_for)

Layouts and Rendering I - yield, content_for, content_for?

Layouts and Rendering II - asset tag helpers, stylesheet_link_tag, javascript_include_tag

Rails Project

Rails - Hello World

Rails - MVC and ActionController

Rails - Parameters (hash, array, JSON, routing, and strong parameter)

Filters and controller actions - before_action, skip_before_action

The simplest app - Rails default page on a Shared Host

Redmine Install on a Shared Host

Git and BitBucket

Deploying Rails 4 to Heroku

Scaffold: A quickest way of building a blog with posts and comments

Databases and migration

Active Record

Microblog 1

Microblog 2

Microblog 3 (Users resource)

Microblog 4 (Microposts resource I)

Microblog 5 (Microposts resource II)

Simple_app I - rails html pages

Simple_app II - TDD (Home/Help page)

Simple_app III - TDD (About page)

Simple_app IV - TDD (Dynamic Pages)

Simple_app V - TDD (Dynamic Pages - Embedded Ruby)

Simple_app VI - TDD (Dynamic Pages - Embedded Ruby, Layouts)

App : Facebook and Twitter Authentication using Omniauth oauth2

Authentication and sending confirmation email using Devise

Adding custom fields to Devise User model and Customization

Devise Customization 2. views/users

Rails Heroku Deploy - Authentication and sending confirmation email using Devise

Deploying a Rails 4 app on CentOS 7 production server with Apache and Passenger I

Deploying a Rails 4 app on CentOS 7 production server with Apache and Passenger II

OOPS! Deploying a Rails 4 app on CentOS 7 production server with Apache and Passenger (Trouble shooting)











Contact

BogoToBogo
contactus@bogotobogo.com

Follow Bogotobogo

About Us

contactus@bogotobogo.com

YouTubeMy YouTube channel
Pacific Ave, San Francisco, CA 94115

Pacific Ave, San Francisco, CA 94115

Copyright © 2024, bogotobogo
Design: Web Master