Skip to content

Instantly share code, notes, and snippets.

@fabrinal
Created May 7, 2017 03:12
Show Gist options
  • Save fabrinal/02321781346a8c747035be7d2d9545d3 to your computer and use it in GitHub Desktop.
Save fabrinal/02321781346a8c747035be7d2d9545d3 to your computer and use it in GitHub Desktop.
Dasar-Dasar Ruby

Kontrol Logika Pada Ruby

If

  # Output "Angka: 10"
  
  angka = 10
  
  if angka > 5
    puts "Angka: #{angka}"
  end

  puts "Angka: #{angka}" if angka > 5

  if angka == 20
    puts "Angka bernilai 20"
  else
    puts "Angka: #{angka}"
  end

  if angka == 20
    puts "Angka bernilai 20"
  elsif angka == 10
    puts "Angka: #{angka}"
  else 
    puts "Angka kurang"
  end

unless

  # Output Huruf: B
  huruf = 'B'

  unless huruf == 'A'
    puts "Huruf: " + huruf
  end

  puts "Huruf: " + huruf unless huruf == 'A'

case

  # Output: "rujak buah pedes"
  makanan = "rujak"

  case makanan
    when "sayur asem"
      puts "sayur asem"
    when "rujak"
      puts "rujak buah pedes"
    else
      puts "Warungnya tutup"
  end

Ternary

  # Output: "Buatan Korea"
  merek_mobil = "hyundai"

  merek_mobil == "hyundai" ? "Buatan Korea" : "Buatan Jepang"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment