Ruby method in 2.4 version:

 

  1. Float => round with keyword

 

#Ruby 2.3

(2.5).round => 3

 

#Ruby 2.4

(2.5).round => 2

 

In addition, Float#round now takes an argument which you can use to define the type of rounding you want. The options are:

:even

:up

:down

 

Eg:

(4.5).round(half: :up) => returns 5

(4.5).round(half: :down) => returns 4

 

Also Float#floor, Float#ceil & Float#trancate now take an optional argument that lets you set a precision.

 

 

  1. Set

Sets are data structure that is available as part of the standard library. It helps you keep a collection of unique items.

 

By default the objects are compared based on their value(or to be more accurate, based on their hash value)

 

Ex:

 

require ‘set’

set = Set.new

set << 123 << 123 << “abc” << “abc” => it returns  [123,”abc”]

 

 

 

  1. Hash#transform_values

 

Here another method extracted from Rails & coming directly to ruby.

 

Ex:

h = {a: 1, b:2, c:3}

h.transform_values {|v| v*10} => it returns  {a: 10, b: 20, c: 30}

 

 

 

  1. Kernel#clone now takes an optional keyword argument

 

-> It’s possible to make a copy of a Ruby object.

-> This is useful because most Ruby objects are mutable and you may want to avoid making changes to the original object.

 

We have two methods for making an object copy;

 

-> clone

-> dup

 

There are a few small differences between clone and dup, but for this post let’s just say that clone keeps the frozen status of the original object while dup does not.

 

New in 2.4 is the ability to call clone with a freeze flag.

 

 

 

  1. Binding#irb

 

Mostly in ruby we are using binding.pry for debugging, Now we have also binding.irb which works in a similar way.

 

Rails 4 & 5 Layouts

 

Partials can have their own layouts applied to them.Layout files reside in app/views/layouts.The stylesheet_link_tag helper method outputs a stylesheet <link>. In this instance, we are linking style.css style sheet. The yield command lets Rails know that it should put the html.erb for the method called here.

 

Rails 4 Example:

 

application.html.erb:

 

<%= render ‘layouts/’header %>

<%= yield %>

<%= render ‘layouts/footer’%>

 

 

Explanation:

 

Here layouts/header is the main file for all pages. It will show same header page in application, and same as footer.

 

yieldusing return the block of the code. That means another files like html.erb files.

 

Rails 5 Examples:

 

Rails 5 now supports this in a much more convenient manner that handles creating a request. Call the render from outside of the controller.

 

Example:

 

rendered_string = ApplicationController.render(

template: ‘users/show_page’,

assigns: { user: @user}

)

 

This renders app/views/users/show_page.html.erb and the sets the @user instance variable, so you don’t need to make any changes to your template. It automatically uses the layout specified in ApplicationController(application.html.erb by default).ApplicationController is main controller in ruby.