Tag Archives: Ruby

Installing Ruby and Rails with rbenv in Fedora 27

Screenshot from 2018-03-09 03-51-01

The first step is to install dependencies for Ruby.

sudo dnf install git-core zlib zlib-devel gcc-c++ patch readline readline-devel libyaml-devel libffi-devel openssl-devel make bzip2 autoconf automake libtool bison curl sqlite-devel

Install rbenv

cd
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
exec $SHELL

git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc
exec $SHELL

Install Ruby

rbenv install 2.5.0
rbenv global 2.5.0
ruby -v

Use this command if you do not want rubygems to install the documentation for each package locally.

echo "gem: --no-ri --no-rdoc" > ~/.gemrc

Install bundler

gem install bundler

Whenever you install a new version of Ruby or a gem, you should run the rehash sub-command. This will make rails executables known to rbenv, which will allow us to run those executables:

rbenv rehash 

Installing Rails

Rails depends on a Javascript runtime, install nodejs.

sudo dnf install epel-release
sudo dnf install nodejs

And now install Rails

gem install rails -v 5.1.5
rbenv rehash
rails -v

#Create your first Rails app

rails new myapp

# Move into the application directory
cd myapp

# Create the database
rake db:create

# Start the server
rails server

You can now visit http://localhost:3000 to view your new website.

Advertisements

Upgrade to Ruby 2.0.0-x

To upgrade your ruby version follow those steps.

First run the following commands:

rvm get head

followed by:

rvm requirements

If you don’t get any errors you can run:

rvm install 2.0.0-x

To set as your current version of Ruby run the following command:

rvm use 2.0.0-x

To make it the default Ruby:

rvm default 2.0.0-x

Or you can run single command for both use and set default:

rvm use 2.0.0-x --default

Now ruby 2.0.0-x is the default version. To check ruby version run:

ruby -v

and where it’s located executing the command:

which ruby

That’s it. Happy coding 🙂

Understanding .nil? .empty? .blank? .present? in Ruby on Rails

nil?, empty? and blank? and .present? are confusing for beginner.

Developer needs to be careful in using them and it is better that we understand it well before using it.

Let’s see which method does what.

.nil?

– It is Ruby method
– It can be used on any object and is true if the object is nil.
– “Only the object nil responds true to nil?” – RailsAPI

In Ruby, all classes inherit from the Object class. nil? is a method of Object; therefore, unless explicitly overridden, all classes have access to nil?. To better understand how Ruby expresses the concept of a non existent object, we need to look at its pseudo-variable nil. The same idea in other languages is often mapped to the primitive type, null or NULL. Depending on the language, NULL may point to nothing at all. Ruby differs here, in that, nil references an actual class called NilClass which exposes methods. This is why it’s possible to do seemingly illogical things like:

nil.nil?
=> true

When calling on an object that is non existent, invalid or explicitly set to nil, an instance of the singleton NilClass is returned. A few examples will illustrate how this works.

books = {:physics => "quantam mecanics", :chemistry => "organic chemistry"}

books[:mathmatics]
=> nil
books[:mathmatics].class
=> NilClass
books[:mathmatics].nil?
=> true
books[:physics].nil?
=> false

test_var = nil
test_var.nil?
=> true

[].nil?
=> false

"".nil?
=> false

0.nil?
=> false

false.nil?
=> false

empty?

Unlike nil?, empty? is only available on some Ruby objects. String, Hash and Array are a few of the classes that implement it. When calling empty? the receiving object is being checked to see if it possesses a non-nil value or values. empty? (at least for the mentioned classes) returns either true or false.

– It is Ruby method
– can be used on strings, arrays and hashes and returns true if:
String length == 0
Array length == 0
Hash length == 0
– Running .empty? on something that is nil will throw a NoMethodError

["physics", "chemistry", "mathmatics"].empty?
=> false

[""].empty?
=> false

[].empty?
=> true

{}.empty?
=> true

"".empty?
=> true

0.empty?
=> NoMethodError: undefined method `empty?' for 0:Fixnum

test_var2 = nil
test_var2.empty?
= > NoMethodError: undefined method `empty?' for nil:NilClass

The last example above clearly illustrates why one would get unexpected results when trying to do things like this:

dog = {:name => "Beauregard"}
puts "What kind?" if dog[:breed].empty?
=> NoMethodError: undefined method `empty?' for nil:NilClass

# Use nil? instead.
puts "What kind?" if dog[:breed].nil?
=> What kind?

.blank?

– It is Rails method
– operate on any object as well as work like .empty? on strings, arrays and hashes.
– It also evaluates true on strings which are non-empty but contain only whitespace:

“An object is blank if it‘s false, empty, or a whitespace string.
For example, “”, ” “, nil, [], and {} are blank.”

For instance, I often use blank? when operating on form field values that are considered optional by a model. Going back to the dog hash, we can see how blank? simplifies testing for values.

nil.blank? = true 
[].blank? = true 
{}.blank? = true 
"".blank? = true 
5.blank? == false
dog = {:name => "Beauregard"}
puts "What kind?" if dog[:breed].blank?
=> What kind?

dog = {:name => "Beauregard", :breed => ""}
puts "What kind?" if dog[:breed].blank?
=> What kind?
"  ".blank? == true"  ".empty? == false

present?
Quick tip: !obj.blank? == obj.present?
activesupport/lib/active_support/core_ext/object/blank.rb, line 17 # (Ruby 1.9)

def present? 
 !blank?
end

Happy Coding 🙂

Install Ruby on Rails with MySQL on Ubuntu 12.04

I have start working with Ruby and Ruby on Rails. I have faced some problem to install Ruby and Ruby on rails development environment  in my ubuntu. For this I share my experience to install ruby on rails development environment in ubintu.

Ruby on Rails:

Ruby on Rails, often shortened to Rails, is an open source full-stack web application framework for the  Ruby programming language.It  is one of the most popular framework for developing web applications using Agile approach. Rails (Latest version : 3.2) is based MVC (Model View Framework).  Here I show step by step installation via RVM (Ruby Version Manager).  Although RVM is not strictly necessary but it’s strongly recommended – it will allow you to use multiple versions of Ruby, which may be be required in some cases.

step 1. Install dependencies:

First you have to install git.

Git Install command :

sudo apt-get install git

Install Curl : Curl is a command line tool to get a file from FTP/HTTP/HTTPS server. To install, open terminal and execute the command –

sudo apt-get install curl

Continue reading Install Ruby on Rails with MySQL on Ubuntu 12.04