Friday, February 22, 2013

Ruby

  • Ruby is a pure object-oriented programming language
Ruby is a general-purpose, interpreted programming language.
Ruby is a true object-oriented programming language.
Ruby is a server-side scripting language similar to Python and PERL.
Ruby can be used to write Common Gateway Interface (CGI) scripts.
Ruby can be embedded into Hypertext Markup Language (HTML).
Ruby has similar syntax to that of many programming languages such as C++ and Perl.
https://www.tutorialspoint.com/ruby/ruby_overview.htm


  • Whitespace characters such as spaces and tabs are generally ignored in Ruby code, except when they appear in strings

Ruby interprets semicolons and newline characters as the ending of a statement.
https://www.tutorialspoint.com/ruby/ruby_syntax.htm


  • Ruby is a perfect Object Oriented Programming Language. The features of the object-oriented programming language include −


    Data Encapsulation
    Data Abstraction
    Polymorphism
    Inheritance

https://www.tutorialspoint.com/ruby/ruby_classes.htm


  • Variables are the memory locations, which hold any data to be used by any program.

https://www.tutorialspoint.com/ruby/ruby_variables.htm


  • Ruby Parallel Assignment

Ruby also supports the parallel assignment of variables. This enables multiple variables to be initialized with a single line of Ruby code.
Ruby Ternary Operator
There is one more operator called Ternary Operator.
Ruby defined? Operators
defined? is a special operator that takes the form of a method call to determine whether or not the passed expression is defined. It returns a description string of the expression, or nil if the expression isn't defined
Ruby Dot "." and Double Colon "::" Operators
You call a module method by preceding its name with the module's name and a period, and you reference a constant using the module name and two colons.
Remember in Ruby, classes, and methods may be considered constants too.
https://www.tutorialspoint.com/ruby/ruby_operators.htm


  • Modules are a way of grouping together methods, classes, and constants. Modules give you two major benefits.

    Modules provide a namespace and prevent name clashes.
    Modules implement the mixing facility.
https://www.tutorialspoint.com/ruby/ruby_modules.htm


  • Ruby does not support multiple inheritances directly but Ruby Modules have another wonderful use. At a stroke, they pretty much eliminate the need for multiple inheritances, providing a facility called a mixin.

Mixins give you a wonderfully controlled way of adding functionality to classes. However, their true power comes out when the code in the mixing starts to interact with code in the class that uses it
https://www.tutorialspoint.com/ruby/ruby_modules.htm

  • What Object Relational Mapping and Active Record are and how they are used in Rails.

How Active Record fits into the Model-View-Controller paradigm.
How to use Active Record models to manipulate data stored in a relational database.
https://guides.rubyonrails.org/active_record_basics.html


  • Setting Up a Ruby Development Environment


Install Ruby
Install Bundler
Install an editor (optional)

Linux
Our recommended way of installing Ruby on a Linux distribution is to use rbenv to manage your Ruby installations. rbenv manages multiple Ruby version installations on your machine and a rbenv plugin named ruby-build adds support to rbenv to install a specified version of Ruby.


https://cloud.google.com/ruby/docs/setup

  • RubyGems is a package manager for Ruby. A package manager organizes packages during the development of an application.
https://www.vultr.com/docs/how-to-install-rubygems-on-linux

  • RubyGems is a package management framework for Ruby.
The RubyGems software allows you to easily download, install, and use ruby software packages on your system. The software package is called a “gem” which contains a packaged Ruby application or library.
https://rubygems.org/pages/download

  • A gem is essentially a Ruby plugin.

Bundler acts as a package manager by determining the full set of direct dependencies needed by your application, as well as sub-dependencies needed by those first-level dependencies.
The only aspect of gems that you'll have to manage is the list of names within a special file, known as a Gemfile.

Why gems?
One of the most obvious reasons relates to code reuse.
If you find yourself implementing the same feature over and over again across projects, there's a good chance that you've found the need for a gem.

Take for example a web service with a public API. In order to encourage potential users to consume your service, you might consider releasing a gem that acts as a client for your API, thereby reducing the barrier to entry.

releasing a gem as open-source provides others the opportunity to contribute by adding features, addressing issues that you might have overlooked, and generally making your gem provide an all-around better experience for its users.

you might consider releasing a gem to demonstrate your abilities as a developer.

Let's first look at the gemspec file (dogeify.gemspec in this case). This file provides metadata about the gem, such as the name, description, author, license, and any gem dependencies required for it to work.

RSpec, so my example tests will be using that DSL.
First, the rspec gem will need to be included.
we need to make the rspec rake task available. To do this, we could modify the Rakefile that Bundler provided automatically.

If you've never released a gem before, you'll be prompted to enter your RubyGems.org credentials so that they know it's you. After you've done this once, Bundler will remember for the future, meaning you won't be prompted for any other info.

https://quickleft.com/blog/engineering-lunch-series-step-by-step-guide-to-building-your-first-ruby-gem/


  • Structure of a Gem

The lib directory contains the code for the gem
The test or spec directory contains tests, depending on which test framework the developer uses
A gem usually has a Rakefile, which the rake program uses to automate tests, generate code, and perform other tasks.
https://guides.rubygems.org/what-is-a-gem/



  • Minitest is Ruby’s built-in test framework. There are lots of tutorials for using it online. There are many other test frameworks available for Ruby as well. RSpec is a popular choice

Let’s add some tests to Hola. This requires adding a few more files, namely a Rakefile and a brand new test directory:
The Rakefile gives you some simple automation for running tests:
https://guides.rubygems.org/make-your-own-gem/




  • Gemfile: Used to manage gem dependencies for our library’s development. This file contains a gemspec line meaning that Bundler will include dependencies specified in foodie.gemspec too. It’s best practice to specify all the gems that our library depends on in the gemspec.


Rakefile: Requires Bundler and adds the build, install and release Rake tasks by way of calling Bundler::GemHelper.install_tasks. The build task will build the current version of the gem and store it under the pkg folder, the install task will build and install the gem to our system (just like it would do if we gem install‘d it) and release will push the gem to Rubygems for consumption by the public.

https://bundler.io/v1.13/guides/creating_gem


  • The search command lets you find remote gems by name.

The install command downloads and installs the gem and any necessary dependencies then builds documentation for the installed gems.
The list command shows your locally installed gems:
The uninstall command removes the gems you have installed.
You can view the documentation for your installed gems with ri:
https://guides.rubygems.org/rubygems-basics/


  • YARD is a documentation generation tool for the Ruby programming language. It enables the user to generate consistent, usable documentation that can be exported to a number of formats very easily, and also supports extending for custom Ruby constructs such as custom class level definitions. Above is a highlight of the some of YARD's notable features.

https://yardoc.org/
  • Gems are the way Ruby libraries are distributed. You use the gem command to manage these gems. We'll use this command to install Rails.

Bundler is a tool that manages gem dependencies for projects. Install the Bundler gem next. as Rails depends on it.

When you install a gem, the installation process generates local documentation. This can add a significant amount of time to each gem's installation process, so turn off local documentation generation by creating a file called ~/.gemrc which contains a configuration setting to turn off this feature

https://www.digitalocean.com/community/tutorials/how-to-install-ruby-on-rails-with-rbenv-on-ubuntu-18-04
  • Bundler provides a consistent environment for Ruby projects by tracking and installing the exact gems and versions that you need. Bundler prevents dependencies and ensures that the gems you need are present in development, staging, and production. Starting work on a project is as simple as running the bundle install command.

https://help.dreamhost.com/hc/en-us/articles/115001070131-Using-Bundler-to-install-Ruby-gems

  • Why choose rbenv over RVM?

    Provide support for specifying application-specific Ruby versions.
    Let you change the global Ruby version on a per-user basis.
    Allow you to override the Ruby version with an environment variable.
https://github.com/rbenv/rbenv/wiki/Why-rbenv%3F


  • Compatibility note: rbenv is incompatible with RVM. Please make sure to fully uninstall RVM and remove any references to it from your shell initialization files before installing rbenv.

https://github.com/rbenv/rbenv#installation


  • Ruby Version Manager (RVM)

RVM is a command-line tool which allows you to easily install, manage, and work with multiple ruby environments from interpreters to sets of gems.
http://rvm.io/
  • Ruby Version Manager (RVM) is a utility that allows you to add your own personal version of Ruby to a user. It allows you to add, remove, or have multiple versions of Ruby and its libraries live in your user director

https://help.dreamhost.com/hc/en-us/articles/217185247-Ruby-Version-Manager-RVM-



  • RVM is a tool for installing and managing multiple Ruby versions.

There are other ways to install Ruby, e.g. through yum, apt-get, source tarball, rbenv and chruby
https://www.phusionpassenger.com/library/walkthroughs/deploy/ruby/ownserver/standalone/oss/install_language_runtime.html

  • Ruby on Rails

Ruby on Rails, often shortened to Rails, is an open source full-stack web application framework for the Ruby programming language.
Ruby on Rails is not to be confused with Ruby, which is a general-purpose programming language, on which Ruby on Rails runs.
Rails is a full-stack framework, meaning that it gives the Web developer the full ability to gather information from the web server, talking to or querying the database, and template rendering out of the box.
As a result, Rails features a routing system that is independent of the Web server.


http://en.wikipedia.org/wiki/Ruby_on_Rails


  • Ruby on Rails is one of the most popular application stacks for developers looking to create sites and web apps. The Ruby programming language, combined with the Rails development framework, makes app development simple.

You can easily install Ruby and Rails with the command-line tool rbenv. Using rbenv will provide you with a solid environment for developing your Ruby on Rails applications as it will let you easily switch Ruby versions, keeping your entire team on the same version.
Gems are packages that extend the functionality of Ruby. We will want to install Rails through the gem command.
So that the process of installing Rails is less lengthy, we will turn off local documentation for each gem we install. We will also install the bundler gem to manage application dependencies:
https://www.digitalocean.com/community/tutorials/how-to-install-ruby-on-rails-with-rbenv-on-ubuntu-16-04


  • Ruby vs. Python

The Ruby on Rails web framework is built using the Ruby programming language while the Django web framework is built using the Python programming language.
Ruby is designed to be infinitely flexible and empowering for programmers.
Python takes a more direct approach to programming. It’s main goal is to make everything obvious to the programmer.
Anything you can do in Ruby on Rails you could also do in Python and Django.
https://learn.onemonth.com/ruby-vs-python/


  • Ruby vs Golang

Ruby vs Golang: Performance
The key principles of Golang development are simplicity and productivity.
The C-family of programming languages influenced both Ruby and Golang
Golang programming is ideal for solving both simple and complex, multi-threaded tasks
Meanwhile, Ruby is ideal for creating tiny commercial projects, blogs, and personal pages thanks to the popular Ruby on Rails framework.
However, for complex solutions, Ruby is not very appropriate.
The performance data, as shown in this benchmark, leads us to the fact that, given equivalent environments, Golang projects run faster than similar projects written in Ruby.

Ruby vs Golang: Community
Ruby was created by one person. Go was designed by Google with its extensive cloud infrastructures, supportive community, forums, tutorials, and, most importantly, additional libraries that help the language move forward

Ruby vs Golang: Compatibility
Thanks to the natural compatibility of Go with other languages (especially with the C-family languages), many programmers use Golang even in a bundle with different stacks like Python and Go or PHP and Go to improve general app performance. Also, Go interacts with almost all databases as a server-side language. As for Ruby, its compatibility is limited only to the most popular databases.

Ruby vs Golang: Development Experience
Golang is perfect both for backend systems of any size and highly-scalable network servers. Go is a modern and competitive language. It provides you with fast and high-quality performance, clean environments, and good compatibility with other technologies. Ruby is useful if you need rapid result delivery, and if the quality of performance is not that important.


https://dzone.com/articles/ruby-vs-golang-comparison-which-is-the-best-soluti-2

  • Python, Ruby, and Golang: A Web Service Application Comparison


Flask (Python)
Sinatra (Ruby)
Martini (Golang)

comparison of Python, Ruby, and Golang for a command-line application I decided to use the same pattern to compare building a simple web service.

Simplicity
While Flask is very lightweight and reads clearly, the Sinatra app is the simplest of the three at 23 LOC (compared to 46 for Flask and 42 for Martini). For these reasons Sinatra is the winner in this category
For beginners to programming Flask and Sinatra are certainly simpler

Documentation
The Flask documentation was the simplest to search and most approachable.
For this reason Flask is the winner in this category.

Community
Flask is the winner hands down in this category. The Ruby community is more often than not dogmatic about Rails being the only good choice if you need anything more than a basic service (even though Padrino offers this on top of Sinatra). The Golang community is still nowhere near a consensus on one (or even a few) web frameworks, which is to be expected as the language itself is so young. Python however has embraced a number of approaches to web development including Django for out-of-the-box full-featured web applications and Flask, Bottle, CheryPy, and Tornado for a micro-framework approach.

Final Determination
If you are coming from a language like C or Java perhaps the statically-typed nature of Golang may appeal to you

https://realpython.com/python-ruby-and-golang-a-web-service-application-comparison/
  • Sinatra is a DSL for quickly creating web applications in Ruby with minimal effort:
http://sinatrarb.com/
  • Capybara
Tired of clicking around in your browser trying to make sure your applications work as expected? Capybara is a library written in the Ruby programming language which makes it easy to simulate how a user interacts with your application.
Capybara can talk with many different drivers which execute your tests through the same clean and simple interface. You can seamlessly choose between Selenium, Webkit or pure Ruby drivers
http://jnicklas.github.io/capybara/

  • Acceptance test framework for web applications

Key benefits
    No setup necessary for Rails and Rack application. Works out of the box.
    Intuitive API which mimics the language an actual user would use.
    Switch the backend your tests run against from fast headless mode to an actual browser with no changes to your tests.
    Powerful synchronization features mean you never have to manually wait for asynchronous processes to complete.


https://github.com/teamcapybara/capybara
  • Watir
Watir, pronounced water, is an open-source (BSD) family of Ruby libraries for automating web browsers. It allows you to write tests that are easy to read and maintain. It is simple and flexible.
http://watir.com/