menu

Showing posts with label Rails-Chapter1. Show all posts
Showing posts with label Rails-Chapter1. Show all posts

Rails - Make UI (landing page home) with javascript, erb, and sass


Melanjutkan tutorial kemarin tentang route, controller, view, tutorial kali ini akan membahas tentang membuat sebuah home page website pada Rails dengan menggunakan javascript, erb, dan sass.

Oke yang pertama silahkan generate terlebih dahulu controller home, caranya dengan mengetikkan code pada terminal ubuntu seperti ini,


Selanjutnya buat route baru untuk halaman home dengan mengedit file routes.rb pada path config/ menjadi seperti ini,
Rails.application.routes.draw do
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
  # root 'application#hello'
  get 'users' => 'users#index'
  get '' => 'home#index'
end

Kemudian, buat controller baru untuk menampilkan halaman index home dengan mengedit file home_controller.rb pada path app/controllers/ menjadi seperti ini,
class HomeController < ApplicationController
 def index

 end
end

Setelah itu edit file application.html.erb pada path app/views/layouts/ menjadi seperti ini,
<!DOCTYPE html>
<html>
  <head>
    <title><%= yield(:title) %> | Page</title>
    <%= csrf_meta_tags %>

    <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track': 'reload' %>
    <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
  </head>

  <body>
    <div class="header">
      <div class="content">
        <a href="/" class="button-header">Home</a>
        <div class="dropdown">
          <a class="button-header dropbtn" onclick="myFunction()">Table</a>
          <div id="myDropdown" class="dropdown-content">
            <a href="#">Link 1</a>
            <a href="#">Link 2</a>
            <a href="#">Link 3</a>
          </div>
        </div>
      </div>
    </div>
    <div class="isi">
      <%= yield %>
    </div>
    <div class="footer">
      COPYRIGHT © 2016 CODEDOCT.COM ALL RIGHTS RESERVED.
    </div>
  </body>
</html>

Selanjutnya, edit file home.scss pada path app/assets/stylesheets/ menjadi seperti ini,
// Place all the styles related to the home controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
body{
 margin: 0;
 padding: 0;
}

// Global css
.text-center{
 text-align: center;
}

.header{
 height: 50px;
 line-height: 50px;
 width: 100%;
 background-color: #1da1f2;
 position:fixed;
 top: 0;
 .content{
  padding: 0 50px;
  .button-header{
   text-decoration: none;
   padding: 15px 20px;
   &:hover{
    opacity: 0.1;
    cursor: pointer;
   }
  }
  .dropdown {
      position: relative;
      display: inline-block;
      .dropdown-content {
       display: none;
       position: absolute;
       background-color: #f9f9f9;
       min-width: 160px;
       overflow: auto;
       box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
       a{
        color: black;
        padding: 6px 16px;
        text-decoration: none;
        display: block;
        &:hover{
         background-color: #f1f1f1
        }
       }
   }
  }
 }
}
.isi{
 padding: 10px 20px;
 border: 1px solid #ddd;
 border-radius: 3px;
 width: 90%;
 margin: 100px auto; 
}
.footer {
 position:fixed;
 left:0px;
 bottom:0px;
 height:50px;
 width:100%;
 background:#fff;
 line-height: 50px;
 text-align: center;
 border-top: 1px solid #ddd;
}

Edit pula file home.js pada path app/assets/javascripts/ menjadi seperti ini,
// Place all the behaviors and hooks related to the matching controller here.
// All this logic will automatically be available in application.js.
function myFunction() {
  $('.dropdown-content').toggle('slow');
}

$(document).mouseup(function (e)
{
    var container = $('.dropdown-content');

    if (!container.is(e.target) // if the target of the click isn't the container...
        && container.has(e.target).length === 0) // ... nor a descendant of the container
    {
        container.hide();
    }
});

Terakhir buat file index.html.erb pada path app/views/home/ kemudian isikan code berikut ini,
<%= provide(:title, "Home") %>
<h1 class="text-center">Welcome to my ruby website</h1>
<br>
<p>
  Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
  tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
  quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
  consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
  cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
  proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>

Sehingga hasilnya akan tampak seperti ini,



===DONE!===

Rails - routes, controller, view

 

Kembali lagi ke Ruby on Rails,..!!!

Karena tutorial PHP dan Laravel sudah sangat jauh, maka mulai saat ini codedoct akan lebih fokus pada tutorial ruby on rails,,

Pada tutorial sebelumnya kita sudah membuat sebuah project Rails dengan nama project-blog-ruby, kali ini pembelajaran akan lebih menitik beratkan pada pemahaman tentang routes, controller, dan view dari framework Rails ini.

Ok, jalankan terlebih dahulu project-blog-ruby dengan mengetikkan code rails server pada directory project-blog-ruby sehingga akan terlihat seperti ini,


Selanjutnya buat controller baru dengan nama Users dengan cara seperti ini,


Buka file routes.rb pada path config/ dan edit menjadi seperti ini,
Rails.application.routes.draw do
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
  root 'application#hello'
  get 'users' => 'users#index'
end

Kemudian, setelah generate controller, rails akan secara otomatis membuat file controller baru dengan nama users_controller.rb pada path app/controllers/ dan edit menjadi seperti ini,
class UsersController < ApplicationController
 def index
  @nama = ['Jhon', 'Garp', 'Tirex']
 end
end

Setelah itu, buka file application.html.erb pada path app/views/layouts/ file ini akan kita jadikan sebagai file layout master, edit menjadi seperti ini..
<!DOCTYPE html>
<html>
  <head>
    <title><%= yield(:title) %> | Page</title>
    <%= csrf_meta_tags %>

    <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track': 'reload' %>
    <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
  </head>

  <body>
    <%= yield %>
  </body>
</html>

Terakhir buat file view baru dengan nama index.html.erb pada path app/views/users dan isikan code berikut,
<%= provide(:title, "User") %>
<div class="test">hai</div>
<% @nama.each do |x| %>
 <h4><%= x %></h4>
<% end %>

Jangan lupa untuk menambahkan css, anda bisa menambahkannya pada file users.scss yang terletak pada path app/assets/stylesheets/ file ini secara otomatis dibuat saat kita generate controller, edit file ini menjadi seperti ini,
// Place all the styles related to the Users controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
.test{
 color: green;
}
lain halnya pada Laravel, pada Rails anda tidak perlu mengcompile file scss ini menjadi file css,

Sehingga hasilnya akan tampak seperti ini,



===DONE!===

Syntax - Syntax ruby


Sebelum kita masuk ke tutorial ruby on rails ada baiknya kita mengenal terlebih dahulu beberapa syntax dari ruby,

Berikut beberapa syntax ruby yang sudah codedoct rangkum

Simple Ruby
====> comment one line
#content comment

====> comment many line
=begin
content comment
=end

====> input text
gets.chomp

====> write text
print

====> write text with new line
puts

====> aritmatika
i = 2
kuadrat: i ** 2
tambah: i + 1
kurang: i - 1
bagi: i / 2 # other way: i.fdiv(2)
hasil bagi: i % 2 # output is 0

====> write variabel
v = "test"
puts "value v : "+v

====> write variabel with on string
v = "test"
puts "value v : #{v}"

caption = "A giraffe surrounded by "
caption << "weezards!"
output:
"A giraffe surrounded by weezards!"

====> many way to code
age = 26
"I am " + age.to_s + " years old."
#==> "I am 26 years old."
"I am " << age.to_s << " years old."
#==> "I am 26 years old."
"I am #{age} years old."
#==> I am 26 years old.

Looping
====> each array
v = [1, 2, 3]
v.each {|x| puts x}

====> each array with other way
v = [1, 2, 3]
v.each do |x| 
  puts x
end

====> while
i = 0
while i<=20 do
 puts i
 i += 1
end

====> repeat until
i = 0
until i==20 do
 puts i
 i += 1
end

====> for
for i in 0..20
 puts i
end

====> times
i = 0
20.times do
 puts i
 i += 1 #it same about i = i+1
end

====> loop
i = 20
loop do
  i -= 1
  next if i>=19 
  puts "#{i}"
  break if i <= 0
end

Array
====> in ruby array can write with hash syntax
php: x = array();
 or just x = [];
ruby x = Hash.new() #remember Hash write with capitalize!
 or just x = Hash.new
 or just x = {}

====> array with key
x = {
 "red" => "merah"
 "green" => "hijau"
 "black" => "hitam"
}

====> array multidimensional
x = [[1, 2, 3], ["iam", "dark", "king"]]

====> write array multidimensional
x.each {|n,v| puts "number: "+n+" value: #{v}\n"}

====> write just one value array
x = ["red", "green", "black"]
puts x[1] #will display green

====> write just one key of array multidimensional
x = {
 "red" => "merah"
 "green" => "hijau"
 "black" => "hitam"
}
puts x[red]

====> write all value of array multidimensional
x = {
 "red" => "merah"
 "green" => "hijau"
 "black" => "hitam"
}
x.each do |eng,ina|
 puts ina
end

====> sort array integer
x = [1, 4, 5, 3, 2]
x.sort! # will display [1, 2, 3, 4, 5]

====> sort array string
x = ["a", "c", "b", "d", "e"]
x.sort! # will display ["a", "b", "c", "d", "e"]

====> sort with other way
books = ["Charlie and the Chocolate Factory", "War and Peace", "Utopia", "A Brief History of Time", "A Wrinkle in Time"]
books.sort! { |firstBook, secondBook| firstBook <=> secondBook }
books.sort! {|firstBook, secondBook| secondBook <=> firstBook}

====> sorting array with simple way
def alphabetize(arr, rev=false)
    arr.sort!
    if rev
        arr.reverse!
    end
    return arr
end
numbers = [5, 1, 3, 8]
puts alphabetize(numbers)

====> array with semi colon
menagerie = { :foxes => 2,
  :giraffe => 1,
  :weezards => 17,
  :elves => 1,
  :canaries => 4,
  :ham => 1
}
output: {:foxes=>2, :giraffe=>1, :weezards=>17, :elves=>1, :canaries=>4, :ham=>1}

====> push array with push
strings = ["HTML", "CSS", "JavaScript", "Python", "Ruby"]
symbols = []
strings.each do |s|
    s = s.to_sym
    symbols.push(s)
end
easy way with this:
alphabet = ["a", "b", "c"]
alphabet << "d"
output:
["a", "b", "c", "d"]

====> push array multidimensional
movies = {
    "warkop" => "4",
    "deadpool" => "4"
}
puts "what is movie title?"
title = gets.chomp
puts "how about the rating?"
rating = gets.chomp
movies[title] = rating
output:
{"warkop"=>"4", "deadpool"=>"4"}
what is movie title?
 jumanji
how about the rating?
 3
movie was added!
{"warkop"=>"4", "deadpool"=>"4", "jumanji"=>"3"}

Symbol
====> symbol not use string
movies = {
    :warkop => "comedy",
    :jumanji => "advanture"
}

====> other way write symbol
movies = {
    warkop: "comedy",
    jumanji: "advanture"
}

====> how fast symbol
require 'benchmark'
string_AZ = Hash[("a".."z").to_a.zip((1..26).to_a)]
symbol_AZ = Hash[(:a..:z).to_a.zip((1..26).to_a)]

====> select the symbols
movie_ratings = {
  memento: 3,
  primer: 3.5,
  the_matrix: 5,
  truman_show: 4,
  red_dawn: 1.5,
  skyfall: 4,
  alex_cross: 2,
  uhf: 1,
  lion_king: 3.5
}
good_movies = movie_ratings.select{|m,r| r > 3}
puts good_movies

====> write just key other way
movie_ratings = {
  memento: 3,
  primer: 3.5,
  the_matrix: 3,
  truman_show: 4,
  red_dawn: 1.5,
  skyfall: 4,
  alex_cross: 2,
  uhf: 1,
  lion_king: 3.5
}
movie_ratings.each {|m,r| puts m}

Condition
====> use unless for false
ruby_is_ugly = false

unless ruby_is_ugly
 puts "Ruby's not ugly!"
end

====> this is ruby
ruby_is_eloquent = true
ruby_is_ugly = false
puts "Ruby is eloquent!" if ruby_is_eloquent
puts "Ruby's not ugly!" unless ruby_is_ugly

====> Ternary
puts 1>0 ? "benar":"salah" #this is same with php he..he..he..

====> Case
puts "Hello there!"
greeting = gets.chomp
case greeting
    when "english" then puts "Hello!"
    when "french" then puts "Bonjour!"
    when "german" then puts "Guten Tag!"   
    when "finnish" then puts "Haloo!"
    else puts "I don't know that language!"
end

====> Case another way
puts "Hello there!"
greeting = gets.chomp
case greeting
    when "english"
      puts "Hello!"
    when "french"
      puts "Bonjour!"
    when "german"
      puts "Guten Tag!"   
    when "finnish"
      puts "Haloo!"
    else puts "I don't know that language!"
end

====> set value that nill
favorite_book = nil
puts favorite_book
favorite_book ||= "Cat's Cradle"
puts favorite_book
favorite_book ||= "Why's (Poignant) Guide to Ruby"
puts favorite_book
favorite_book = "Why's (Poignant) Guide to Ruby"
puts favorite_book
output:
Cat's Cradle
Cat's Cradle
Why's (Poignant) Guide to Ruby'

====> write code that divisible of 2
my_array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
my_array.each {|x| puts x if x % 2 == 0  }
output: 2
4
6
8
10

====> use upto
"L".upto("P") {|x| puts x }
output:
L
M
N
O
P
test

====> Play with array(CRUD array)
movies = {
    "warkop" => "4",
    "deadpool" => "4"
}

puts "what do you want your movie"
choice = gets.chomp

case choice
    when "add"
        puts movies
        puts "what is movie title?"
        title = gets.chomp
        if movies[title].nil?
            puts "how about the rating?"
            rating = gets.chomp
            movies[title.to_sym] = rating.to_i
            puts "movie was added!"
            puts movies
        else
            puts "that the movie already exists"
        end
    when "update"
        puts movies
        puts "What movie that you want to update rating?"
        title = gets.chomp
        if movies[title].nil?
            puts "Your movie not found!"
        else
            puts "Set the rating movie"
            rating = gets.chomp
            movies[title] = rating.to_i
            puts "rating was update!"
            puts movies
        end
    when "display"
        movies.each do |m,r|
            puts "#{m}: #{r}"
        end
    when "delete"
        puts movies
        puts "Type movie that you want to delete?"
        title = gets.chomp
        if movies[title].nil?
            puts "Movie not found!"
        else
            movies.delete(title)
            puts "That movie was deleted"
            puts movies
        end
    else puts "Error!"
end

Library
====> collect 
my_nums = [1, 2, 3]
my_nums.collect { |num| num ** 2 }
output:
[1, 4, 9]
=begin
This is because .collect returns a copy of my_nums,
but doesn't change (or mutate) the original my_nums array. 
If we want to do that, we can use .collect!
=end
my_nums.collect! { |num| num ** 2 }
=begin
Recall that the ! in Ruby means "this method could do something dangerous or unexpected!"
In this case, it mutates the original array instead of creating a new one.
=end

====> example collect
fibs = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
print "#{fibs}\n"
doubled_fibs = fibs.collect {|x| x**2}
print "#{fibs}\n"
print doubled_fibs
output:
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
[1, 1, 4, 9, 25, 64, 169, 441, 1156, 3025]
print "#{fibs}\n"
doubled_fibs = fibs.collect! {|x| x**2}
print "#{fibs}\n"
print doubled_fibs
output:
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
[1, 1, 4, 9, 25, 64, 169, 441, 1156, 3025]
[1, 1, 4, 9, 25, 64, 169, 441, 1156, 3025]

====> yield
def double(param)
    puts "yeehhaaaa"
    yield(param)
    puts "end"
end

double(2) {|x|puts x*2}
output:
yeehhaaaa
4
end

====> Proc
multiples_of_3 = Proc.new do |n|
  n % 3 == 0
end
(1..100).to_a.select(&multiples_of_3)
output:
[3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99] 

====> Proc with call
hi = Proc.new do
    puts "Hello!"
end
hi.call
output:
Hello!

====> floor
floats = [1.2, 3.45, 0.91, 7.727, 11.42, 482.911]

round_down = Proc.new do |x|
    x.floor
end

ints = floats.collect(&round_down)
output:
[1, 3, 0, 7, 11, 482]

====> symbols meet 
Procnumbers_array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
strings_array = numbers_array.map(&:to_s)
output:
["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]

====> lambda
def lambda_demo(a_lambda)
  puts "I'm the method!"
  a_lambda.call
end
lambda_demo(lambda { puts "I'm the lambda!" })
output:
I'm the method!
I'm the lambda!

====> wonderfull lambda
strings = ["leonardo", "donatello", "raphael", "michaelangelo"]
symbolize = lambda {|x| x.to_sym}
symbols = strings.collect(&symbolize)
output:
[:leonardo, :donatello, :raphael, :michaelangelo]

====> different proc and lambda?
def batman_ironman_proc
  darkking = Proc.new { return "Batman will win!" }
  darkking.call
  "Iron Man will win!"
end

puts batman_ironman_proc

def batman_ironman_lambda
  darkking = lambda { return "Batman will win!" }
  darkking.call
  "Iron Man will win!"
end

puts batman_ironman_lambda

output:
Batman will win! #this is pro
Iron Man will win! #this is lambda

====> filter with array
my_array = ["raindrops", :kettles, "whiskers", :mittens, :packages]
symbol_filter = lambda {|x| x.is_a? Symbol}
symbols = my_array.select(&symbol_filter)
output:
[:kettles, :mittens, :packages] 

Method
====> method
def name_method
  content_method
end

====> method with return boolean
def is_true?
  content_method
end

====> method with parameter
def welcome(name)
  return "Hello #{name}"
end
welcome("Dark King")

====> to string, float, integer, simbol
i = 2
i.to_s
i.to_f
i.to_i
i.to_sym

====> sorting array with method
def alphabetize(arr, rev=false)
  if rev
    arr.sort { |item1, item2| item2 <=> item1 }
  else
    arr.sort { |item1, item2| item1 <=> item2 }
  end
end
books = ["Heart of Darkness", "Code Complete", "The Lorax", "The Prophet", "Absalom, Absalom!"]
puts "A-Z: #{alphabetize(books)}"
puts "Z-A: #{alphabetize(books, true)}"

====> the function semicolon
puts "string".object_id
puts "string".object_id
puts :symbol.object_id
puts :symbol.object_id
output:
15100680
15100480
319848
319848

string_time = Benchmark.realtime do
  100_000.times { string_AZ["r"] }
end
symbol_time = Benchmark.realtime do
  100_000.times { symbol_AZ[:r] }
end
puts "String time: #{string_time} seconds."
puts "Symbol time: #{symbol_time} seconds."

====> class
class Computer
  $manufacturer = "Mango Computer, Inc."
  @@files = {hello: "Hello, world!"}
  def initialize(username, password)
    @username = username
    @password = password
  end
  def current_user
    @username
  end
  def self.display_files
    @@files
  end
end

# Make a new Computer instance:
hal = Computer.new("Dave", 12345)
puts "Current user: #{hal.current_user}"
# @username belongs to the hal instance.
puts "Manufacturer: #{$manufacturer}"
# $manufacturer is global! We can get it directly.
puts "Files: #{Computer.display_files}"
# @@files belongs to the Computer class.
output:
Current user: Dave
Manufacturer: Mango Computer, Inc.
Files: {:hello=>"Hello, world!"}

====> you can do thisclass ApplicationError
  def display_error
    puts "Error! Error!"
  end
end
class SuperBadError < ApplicationError
end
err = SuperBadError.new
err.display_error
output:
Error! Error!

====> Override class
class Creature
  def initialize(name)
    @name = name
  end
  def fight
    return "Punch to the chops!"
  end
end
class Dragon < Creature
   def fight
       return "Breathes fire!"
   end
end
show = Dragon.new
puts show.fight
output:
Breathes fire!

====> OOP
class Account
    attr_reader :name, :balance
    def initialize(name,balance=100)
        @name = name
        @balance = balance
    end
    private 
    def pin
        @pin = 1234
    end
    def pin_error
        return "Access denied: incorrect PIN."
    end
    public
    def display_balance(pin_number)
        if pin_number == pin
            puts "Balance: $#{@balance}."
        else
            pin_error
        end
    end
    def withdraw(pin_number,amount)
        if pin_number == pin
            puts "Withdrew #{amount}. New balance: $#{@balance}."
        else
            pin_error
        end
    end
end
checking_account = Account.new("Conteriano",10_000_000)
puts checking_account.display_balance(4321)
puts checking_account.display_balance(1234)
puts checking_account.withdraw(4321,10001)
puts checking_account.withdraw(1234,10001)
output:
Access denied: incorrect PIN.
Balance: $10000000.
Access denied: incorrect PIN.
Withdrew 10001. New balance: $10000000.

===DONE!!!===

Rails - Install Rails on Ubuntu


Oke melanjutkan artikel sebelumnya, kali ini kita akan menginstall Rails..

Rails merupakan sebuah framework pada Ruby yang akan memudahkan anda membuat sebuah website dengan menggunakan bahasa Ruby yang sangat elegan..

Kebanyakan orang yang menguasai Ruby on Rails adalah programmer yang sudah bisa dikatakan "PRO" dikarenakan kebanyakan  dari mereka yang jago Ruby pasti sebelumnya sudah pernah belajar PHP dan membandingkan kedua bahasa pemrogramman tersebut. Tapi tidak sedikit juga programmer yang langsung mempelajari Ruby tanpa mengerti PHP hehe..

Tentu saja hal itu sangat disayangkan  karena PHP dengan penggemar yang sangat banyak diseluruh dunia juga merupakan bahasa yang elegan dan penuh dengan "kebebasan" => "U Can Make Anything With PHP" haha..

Oke langsung saja kita install Railsnya, Here we go..

Jangan lupa install Ruby terlebih dahulu,

Dalam pengembangannya nanti Rails sangat bergantung pada JavaScript oleh sebab itu kita install terlebih dahulu NodeJS
curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -
sudo apt-get install -y nodejs

Kemudian Install Rails
gem install rails -v 5.0.0.1

Pastikan kembali rbenv Ruby dapat eksekusi
rbenv rehash

Check installation
rails -v

Sehingga tampilannya akan terlihat seperti ini


===DONE!===

Ruby - Upgrade Ruby with rbenv


Melanjutkan tutorial sebelumnya tentang install Ruby pada ubuntu, kali ini saya akan share cara mengupgrade version Ruby yang sudah diinstall

Pada komputer saya Ruby yang terinstall adalah versi 1.9.3 yang akan saya upgrade menjadi versi 2.3.1.

Sangat dianjurkan mengerti syntax ubuntu terlebih dahulu.

Oke kita langsung saja caranya..

Pertama hapus terlebih dahulu folder .rbenv yang lama
sudo rm -rf .rbenv

Kemudian install versi ruby yang anda inginkan dalam hal ini saya akan menginstall Ruby versi 2.3.1.
//update all apt 
sudo apt-get update 
//install library pendukung Ruby 
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 libffi-dev

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

rbenv install 2.3.1

Selanjutnya edit isi file .ruby-version
vim .ruby-version

Ganti sesuai dengan versi Ruby yang sudah diinstall, tampilan vim akan terlihat seperti ini.


Ubah menjadi


Jika sudah selesai kemudian save :wq

Terakhir install bundler
rbenv global 2.3.1
gem install bundler
ruby -v

Sehingga tampilan akan menjadi seperti ini


===DONE!===

Ruby - Install Ruby on Ubuntu


Masih suasana lebaran, kali ini saya akan memulai tutorial ruby on rails.

Berbeda dengan tutorial Laravel on PHP yang sudah saya posting, tutorial Ruby on Rails kali ini akan saya terapkan pada ubuntu tidak pada windows. Dikarenakan agar saya tidak lupa syntax-syntax pada terminal ubuntu hehe..

Oke pada posting kali ini saya akan menginstall Ruby pada ubuntu, jika anda ingin meginstall Ruby pada windows silahkan klik disini.

Cara yang saya gunakan untuk menginstall Ruby pada tutorial ini menggunakan metode rbenv karena menurut saya pada pengembangannya  rbenv ini sangan mudah digunakan, sedangkan versi Ruby yang akan diinstall adalah ruby version 2.3.1.

Sangat dianjurkan mengerti syntax ubuntu terlebih dahulu.

Langsung saja kita ke tutorialnya,,

Pertama, tentu saja buka terminal anda kemudian install beberapa library pendukung Ruby.
//update all apt 
sudo apt-get update 
//install library pendukung Ruby 
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 libffi-dev

jika ada pertanyaan seperti ini ketik "y" saja


Selanjutnya, install Ruby dengan metode rbenv dengan syntax ini.
//pastikan berada pada path user home 
cd 

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

//download or clone plugin rbenv 
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

rbenv install 2.3.1
rbenv global 2.3.1
ruby -v

Terakhir install bundler
gem install bundler

sehingga hasilnya akan menjadi seperti ini.


===DONE!===

Ruby - Install ruby di windows


Ruby merupakan sebuah bahasa pemrograman berbasis objek. Banyak orang beranggapan bahwa ruby adalah bahasa pemrograman yang terhebat.

Tapi bagi saya, jangan terlalu fanatik terhadap satu bahasa pemrograman karena setiap bahasa pemrogramman memiliki kelebihan dan kekurangannya masing-masing. Jika anda hebat dalam bahasa php maka tidak ada salahnya untuk belajar ruby, begitu juga sebaliknya.

Semakin banyak bahasa pemrograman yang anda kuasai maka anda akan melihat bahwa dunia pemrograman itu sempit.

Saat inipun sudah banyak pengembangan ruby salah satunya dalam pemrograman web yaitu ruby on rails,,

Oke langsung saja kita install ruby pada windows, lain waktu akan saya share juga cara install ruby pada ubuntu.

  1. Download ruby disini
  2. Install seperti biasa jangan lupa centang add Ruby executeables to your PATH agar path ruby otomatis dimasukan kedalam global path
  3. Jika sudah selesai coba cek ruby pada cmd dengan mengetikan ruby -v maka akan muncul tulisan

===DONE!!!===

Why
  • Instalasi ruby sudah selesai dan finish dengan sempurna tapi kenapa pada saat cek di cmd not found maka anda pasti lupa mencentang add Ruby executeables to your PATH caranya untuk menambahkan PATH ruby pada global lihat disini