Ruby Advanced Guide

In this article, I will introduces some concepts of Ruby that not all Ruby developers know about

Screen Shot 2018-06-12 at 11.14.29 AM

Block

Is a way to group a short piece of code as a replacement of do … end. It’s not an object, so it cannot be passed as a parameter in a function.

[1, 2, 3].map { |e| e + 1 } # [2, 3, 4]

Proc

Proc is another way we use to shorten our code. Unlike Block, when using Proc, we instantiate an object, and then we can reuse that object in argument list as block

p = Proc.new { |e| e + 1 }
[1, 2, 3].map(&p) # [2, 3, 4]
p.object_id # 47417179317860
p.class # Proc

or in a method as a parameter

def proc_in_method arr, proc
  proc.call(arr)
end
proc = Proc.new { |arr| arr.sum }
proc_in_method [1, 2, 3], proc

Continue reading Ruby Advanced Guide

Install Rails with MySql on Windows

Hello Ruby guys!

It had been 4 years before last year I used Ubuntu or MacOS for Ruby on Rails development. It is absolutely that Linux environment is much easier for developers than Windows. The only thing that make me switch from Linux to Windows is that documentation on Windows is much more comfortable.

At first, I tried to use Vmware to install a virtual Ubuntu and program on that operating system. However, problem occurs. I cannot work on virtual machine for long duration, my computer get slower after several hours.

Thus, it’s critical to program Ruby on Rails directly on Windows. After several tries, finally I got a brief note of steps

  1. You should install Ruby and then install rails as gem, instead of using rails installer. You can install latest ruby version on https://rubyinstaller.org/downloads/. You can choose to install along with DevKit. Just follow the ruby installation guide, and remember to choose MSYS development tool when install devkit
  2. Install Git Bash to work with git on command line as on Linux terminal and to have another cool terminal. Many people might prefer using Git tool like SourceTree, SmartGit, etc … It’s your choice.
  3. Open Ruby Command Prompt or Git Bash terminal, test your ruby installation by
     gem install json --platform=ruby --no-ri --no-rdoc
  4. Install XAMPP, the easiest way to have mysql and mysql management tool (phpmyadmin)
  5. Download C connecter , extract to
    C:\

    and copy

    libmysql.dll

    from

    C:\mysql-connector-c-noinstall-6.0.2-win32\lib

    to your ruby installation directory

    C:\Programs\Ruby25\bin
  6. Install rails gem
    gem install rails --platform=ruby --no-ri --no-rdoc
  7. Install mysql2 gem to be able to use mysql as database for your rails project
    gem install mysql2 --platform=ruby --no-ri --no-rdoc
  8. Create new rails project with mysql as database
    rails new ProjectName -d mysql

Happy Coding!