Check the return value of “save”, otherwise use “save!”

Before

If you use “save” on an invalid record, it will not be saved:

post = Posts.new do |p|
  p.title = "example"
  p.body = "An example"
end
post.save

This code may work at the moment, but it is fragile. If a later refactoring introduces a new required column to Posts, then the save call will silently start failing.

Refactor

If you you think the record can never be invalid, or don’t want to check the return value, use “save!

post = Posts.new do |p|
  p.title = "example"
  p.body = "An example"
end
post.save!

Now you will get an error if the post cannot be saved, which will alert you to the problem.

And for production you can use https://github.com/fnando/notifier to send `UnCaught` exception notifications

How a developer can be more productive

As a developer for more than 5 years… wait no bullshits.. coming to the point

  • You need to type code a lot faster (think before you type)
    • You need a great IDE (Text Editor)
    • Learn the KeyBoard shortcuts to everything
    • Add plugins you are gonna need
    • If altering code snippet is difficult then its the hightime you move on to new IDE ( My favorite is RubyMine.)
  • Git is the version control system I recommend
    • Lots of great features
  • Terminals like Guake Terminal can make you job easier
  • Make processes companyWide or TeamWide for better results
  • Keep getting/asking continuous feedback from peers and Seniors
  • CodeReviews are what that make sure you get smarter everyday
  • As a developer you need to have good understanding of your Project
    • Business Aspect of it as well
    • If you are working on Hospital management system then study about the internal processes, management frameworks, Patient discharging/admission policies and all

 

TO BE CONTINUED..

Programming : Atomic analogy of OOP Concept

Let us consider the Lewis Structure for the Water molecule. Lets define Lewis structure first – “Lewis Structures are visual representations of the bonds between atoms and illustrate the lone pairs of electrons in molecules.

screenshot-from-2016-12-05-12-59-16

Oxygen has 6 valence electrons and Hydrogen has 1; altogether water molecule has 8 valence electrons. If we pay attention and analyze individual atom, we see every atom has fulfilled it octet – Hydrogen has 2 electrons and Oxygen has 8 electrons(counting 2 it got from 2 Hydrogen atoms).

In chemistry, a valence electron is an electron that is associated with an atom, and that can participate in the formation of a chemical bond; in a single covalent bond, both atoms in the bond contribute one valence electron in order to form a shared pair.

My point

Consider the valence electrons as public methods and non-valence electrons as private/protected methods. OOP suggests to keep data at the core so that it is always private. So, consider private data as nucleus of the atom.

When two unstable atoms bond with each other to form a rather stable structure sharing their valence electrons, then yields more usable stable chemical structure. Now lets make analogy:

  • bonding                      –> message passing
  • unstable atoms            –> Individual software components (say classes) that need to cope with other entities to form a usable software.
  • valence electrons        –> Public methods (public API) that take part in assembly
  • lone pair of electrons –> Public methods that might not be used in that software at all.
  • core electrons               –> Private methods that do not take part in
  • Atom nucleus              –> Private data of object; never shared directly with other
  • Yielded usable stable chemical structure  –> A releasable software component
analogy

Atomic analogy of OOP

 

Ruby : What happens to dependencies when method is extracted out as Proc

It is clear that whenever you try to save any method bound to any object; it is extracted out as an instance to Method class. People may be in misconception that methods are extracted out as instance of Proc.

What about the dependencies viz. instance variables and Other object instance?

Since, no new object is created; the same Method object defined within the object is referenced, so no dependency issue shall occur.  See the code below to understand what  I was trying to say.

Continue reading

Ruby : There difference between puts and `p` methods

Note: There is vast difference between `p` and `puts` though they might look behaving similarly. For normal string there seem no difference in behavior however when there comes multi-line string or string containing CR / LF characters then the difference is vivid.

p "first line\n second line>>  "first line\n second line" 
 
puts "first line\n second line>>  first line 
        second line

Continue reading

Ruby : Unexpected error : Got surprised :: unexpected ‘+’, expecting keyword_end

Today I stumbled upon something strange in RUBY,

if @user
  content_tag(:span, class: "post-editer") do
    link_tag "#" do
      content_tag(:i, "", class: "fa fa-cog")
    end +
        get_dropdown_option
  end
else
  "".html_safe
end

ruby_error

/home/john/projects/thepact/app/helpers/pact_profile_page_helper.rb:240: syntax error, unexpected '+', expecting keyword_end

Extracted source (around line #240):
238
239
240
241
242
243
 
 link_tag "#" do
 content_tag(:i, "", class: "fa fa-cog")
 end +
 get_dropdown_option
 end
 else

Solution:

content_tag(:span, class: "post-editer") do
  link_markup = link_tag "#" do
    content_tag(:i, "", class: "fa fa-cog")
  end
  link_markup + get_dropdown_option
end