Thursday, 5 June 2014

Bang Methods in Ruby Language

Bang Methods in Ruby Language :

             Hi today i would like to share some information regarding Bang methods in Ruby. Please take a look at following examples and give a try in rails console.

 name = "JOHN" 
 p string = name.downcase # prints john, doesn't modify name 
 p name # name is still JOHN          

Method which ends with exclamation mark in ruby is called as bang methods.
         
name = "JOHN" 
p name.downcase! # prints john,modify name 
p name # name becomes john after calling bang method

Wednesday, 14 May 2014

Difference between && , and in Ruby

Difference between && , and in Ruby is mainly the Precedence of these operators. ‘and’ and ‘&&’ both have different precedence. This tutorial will help understanding with the help of examples.

Use of ‘&&’

The ‘&&' operator is used as "Logical AND” on the boolean/ non boolean variable in Ruby.
Example,
Suppose we have variable a as,
a = true
And variable b as,
b = false
Then, logical AND will give result as,
c = a && b
puts "C is #{c}"
=> C is false

Use of ‘and’

The 'and' operator is also used as "Logical AND” on the boolean/ non boolean variable in Ruby.
But, ‘and’ has lower precedence than ‘&&’
This will also give same results for example shown for ‘&&’
a = true
b = false
c = a and b
puts "C is #{c}"
=> C is false

Difference

Suppose, we have a, b and c variables as given below,
a = true
b = false
c = true

Use of && - Use of and

d = a && b && c
puts "D is #{d}"
=> false
e = a and b and c
puts "E is #{e}"
=> true

Points to be noted:

  • && has more precedence than =, thus evaluated result is assigned to d
  • and has less precedence than =, thus value of a is directly assigned to e which gets printed.

Wednesday, 9 April 2014

Rails Truncate method

       
Today i would like to give some information on Rails api Truncate method.
I was looking for a  method in my Ruby on Rails project which can truncate a string  after some characters.. after searching I found 'truncate' method, which is a rails api method for truncating string in views.
So if you have string
str= "Hi, we are going to truncate any large string by this rails api method??" # 72 characters

the truncate method can be used in the view like this:


truncate(str, :length => 25)


and the result would be :
Hi, we are going to tr...


This was very useful since we had to write seprate ruby methods for truncating.


Saturday, 10 August 2013

Single Table Inheritance in Ror

Hi Folks/Programmers,
                                      I’ve come across many scenarios ,where a Ruby Rails Developer have to use Inheritance(which is a popular OOPS feature), So today i would like you to share some information about single table inheritance.

 With single table inheritance you have a base model which inherits from ActiveRecord::Base, then one or more sub-classes, which inherit from the base model.


Below here is my simple example:-
My Current example is based on Mobile system. We have a single model called ‘Mobile’ and other models such as ‘Samsung’, ‘Apple’, ‘Nokia’ etc., that inherits the property of Mobile model.

so structure come like


Super class
__________


class Mobile < ActiveRecord::Base

end


Base class
________


class Nokia < Mobile

end


while creating table Mobile  you just create a type string column on your votes table, and rails takes care of the rest..



When you do Nokia.create(model: "E7"), this record will be saved in the mobiles table with type = “Nokia”. You can then fetch this row again using Mobile.where(model: 'E7').first and it will return a Firm object.

If you don’t have a type column defined in your table, single-table inheritance won’t be triggered. In that case, it’ll work just like normal subclasses with no special magic for differentiating between them or reloading the right type with find.

Thursday, 25 July 2013

Viewing associations of a particular model

Today i would like to share about viewing associations of a particular model together in console.
How to get rails associations from console

  1) User Model associate with UserDetail and Photo Model

     class User < ActiveRecord::Base
       has_one :user_detail
       has_many :photos
    end

  2) Open your rails console

     User.reflect_on_all_associations.map{ |reflection| ":#{reflection.macro} => :#{reflection.name}"}

     => [":has_one => :user_detail",":has_many => :photos"]



Monday, 15 April 2013

Ruby Modules && Mixins

include and extend in ruby

Today I would like to explain differences between include  and extend in Ruby.
Include: Include it is adding methods of a class  to an instance which we created.
Extend: Extend it is for adding class methods itself.

If we go with a simple example we can get a clear picture
module Advertisement
def advertisement
puts ‘upgrade to new rails version’
end
end

class Movie
include Advertisement
end

Movie.new.advertisement # upgrade to new rails version
Movie.advertisement # NoMethodError: undefined method ‘advertisement’ for
Movie:Class


class Game
extend Advertisement
end


Game.advertisement # upgrade to new rails version
Game.new.advertisement # NoMethodError: undefined method ‘advertisement’ for #<Game:0x1e708>


So that from above example we can get a basic idea that “include” makes the advertisement method available to an instance of a class
and where as the extend makes the advertisement method available to the class itself.

Monday, 8 April 2013

form_for and form_tag

I would like to explain differences between form_for and form_tag

form_tag and form_for both are used to submit the form and it’s elements. The main difference between these two is the way of managing objects related to that particular model is different.

form_for:

We should use “form_for” tag for a specific model i.e. while crating a new row in database. It performs the “standard http post” which is having fields related to active record (model) objects.

Example for how to use form_for tag:

<% form_for :user, @user, :url => { :action => "update" } do |f| %>

After this, we can use the f as an object to create input filed.

Username: <%= f.text_field :username %>
Email : <%= f.text_field :email %>
Address: <%= f.text_area :address %>

<% end %>

form_tag:

It creates a form as a normal form. form_tag also performs the “standard http post” without any model backed and has normal fields. This is mainly used when specific data need to be submitted via form.


Example:

<% form_tag '/articles' do -%>
<%= text_field_tag "article", "firstname" %>
<% end -%>

It just creates a form tag and it is best used for non-model forms.