Ruby On Rails – 5 Tips You Need To Know

ruby on rails programming language tips coding advice

A developer community includes the people who have been evolving throughout the invention of computers and various methods of programming its behavioral aspects. Many programming languages had been invented to be used by the computer scientists in order to keep it upgraded according to the requirements of the whole world, Taking baby steps in the early stages of computing technologies. Given the fact, we of course will never be able to imagine the technology and its various aspects without being aware of the fundamentals used in it. However, App Developers & Programmers have been the proven with the greatest innovators and the kind of variety we can use/see in when it comes to utilize any specific tools or frameworks to address a specific needs of the programmable systems & devices. 

Each little thing is getting more outdated the latest updates and innovations done by the programming experts and other enthusiasts. Same is applicable in case of ROR (Ruby on Rails). So, here are the 5 things which you may not be aware of when it comes to Ruby on Rails. 

1. Easy Data Dumping 

You’ll often need to get data from production to development or development to your local or your local to another developer’s local. One plug-in we use over and over is Yaml_db. This nifty little plug-in enables you to dump or load data by issuing a Rake command. The data is persisted in a yaml file located in db/data.yml. This is very portable and easy to read if you need to examine the data. 

rake db:data:dump 

example data found in db/data.yml 


--- 

campaigns: 
columns: 
- id 
- client_id 
- name 
- created_at 
- updated_at 
- token records: 
- - "1" 
- "1" 
- First push 
- 2008-11-03 18:23:53 
- 2008-11-03 18:23:53 
- 3f2523f6a665 
- - "2" 
- "2" 
- First push 
- 2008-11-03 18:26:57 
- 2008-11-03 18:26:57 
- 9ee8bc427d94 

2. Use Rake  - The Best Time-Saving Method

Projects often include more than just application-specific code. Sample data have to be created, Web services have to be queried, files have to be moved, code snippets rewritten, etc. Resist the urge to shell script or to cram in a migration or controller. Use Rake. It rocks! It is a build tool written in Ruby, very similar to make Rails projects have several Rake tasks already defined; to see these, run the rake -T command. 

rake data:bootstrap # load in some basic data [caution: will nuke and replcace cate... 

rake db:create:all # Create all the local databases defined in config/database.yml rake db:drop # Drops the database for the current RAILS_ENV ... 

rake ts:run # Stop if running, then start a Sphinx searchd daemon using Thi... 

rake ts:start # Start a Sphinx searchd daemon using Thinking Sphinx's settings 

rake ts:stop # Stop Sphinx using Thinking Sphinx's settings 

Adding your own Rake tasks is quite easy. In the example below, you see that the task is name-spaced and has a description and task name, allowing you to write in Ruby. 

namespace :data do desc "load in some basic data [caution: will nuke and replcace categories, categorizations and inventory items]" 

task :bootstrap => :environment do 

# clean out existing: 

[Category, Categorization, InventoryItem].each{|m| m.find(:all).each{|i| i.destroy}} 

InventoryItem.create! :name => "Compass" 

c = Category.create! :name => "Basic Apparel" 

["Men’s Heavyweight Cotton T", 

"Men’s Heavyweight Polo", 

"Women’s Organic Cotton Fitted T", 

"Women’s Fitted Polo", 

"Children’s T-Shirt", 

"Jr’s Distressed Hoodie", 

"Hemp Messenger Bag"].each do |name| 

c.inventory_items.create! :name => name 

end 

... 

end 

3. Testing Is Easier With Rspec 

For most people, the word “test” brings back scary memories of high school or university exams. When working with the Ruby On Rails programming language, however, automated testing can make your development experience much more enjoyable. While lots of people have strong, nearly religious, opinions about them, at their core, automated tests are just little helper programs you write that run bits of your main code to make sure they do the right thing. When done right, testing will improve your workflow and increase your confidence in the results. 

Rails ships with a test framework baked right in, but for the last couple of years all the cool kids have been using an alternative called Rspec. Rspec’s biggest advantage is its syntax for specifying tests: 

describe "My Cool library" do before do 

@cool = Cool.new end 


it "should be full of penguins." do 

@cool.get_penguins! 

@cool.penguin_count.should == 10 

end 


it "should melt if it gets too warm" 

end 

What’s great about Rspec’s syntax is how much English it uses. The describe block that sets the context and each assertion within it takes strings that you use to explain what your code should do. Often, this is the most important stage: you sit down to write the assertion, getting as far as you can, and then you think, “Right, what should this code actually do then?” 

Because Rspec lets you leave off the block that implements the assertion (as in the second melt example), you can quickly brainstorm all of your functionality, and then go back and implement the tests later as you write the code. In the meantime, Rspec will consider those tests as “pending” and give you little reminders about them in your test runs. 

Besides helping you write code in the first place, another great thing about tests is that, once you have enough of them, they let you see how all of your code is related, making it easy to know if your recent change broke anything else in your application. Rspec makes it easy to get good test coverage through the use of custom generators that create the tests right along with the rest of your code: 

$ script/generate rpsec_scaffold MyModel 

Once you’ve got tests to ensure that the basic functionality works successfully, you can make changes and add new code with confidence without worrying about introducing invisible bugs. As long as you run your tests regularly, you’ll know as soon as you break something. And as GI Joe taught us, knowing is half the battle! 

4. Haml – Time Has Gone For Ugly Views 

Views are how your Rails application generates the HTML pages your visitors actually see and use. By default, Rails uses the Erb templating system to let you embed bits of Ruby in your markup so that you can insert your data as needed. However, recent versions of Rails let you take your pick of templating languages, and nowadays the Ruby interwebs have been all abuzz about an alternative system called Haml. 

Haml is marketed as “markup Haiku.” Its CSS-inspired syntax lets you focus on the semantics of your data rather than worrying about closing all the angle brackets that come with using ERb and HTML. For comparison, here’s a bit of markup in standard ERb: 

<%= print_date %> 

<%= current_user.address %> 


<%= current_user.email %> 

<%= h current_user.bio %> 

Erb: And here’s the equivalent in Haml 

#profile 

.left.column 

#date= print_date 

#address= current_user.address 

.right_column 

#email= current_user.email 

#bio= h(current_user.bio) 

Haml’s not for everyone, and many people will find this syntax quite alien. But if you value concision and are fluent in CSS, Haml may be just the ticket. 

5. Keep Your Constant At One Place 

All applications have constants, variables that are defined with data that don’t change, such as the name of the application, the tagline, values for crucial options, etc. We use the Rails initializer feature to define a config/initializers/site_config.rb for housing these constants. By using this convention, all developers on a project know exactly where to look for the constants and can quickly make changes. 

Many people have questions about when to put a constant in the site_config.rb instead of the class it is used in. For a constant that is only used in a single class, we suggest putting it in that class. However, if the constant is used in more than one location, put it in the site_config.rb. For more on constants, have a look at Rubylearnin.

# File config/initializers/site_config.rb 

REPORT_RECIPIENT = 'jenn@scg.com' 

REPORT_ADMIN = 'michael@scg.com'


Author Krunal Vyas is an IT consultant at iQlance Solutions Pvt. Ltd. iQlance is one of the leading Website and Apps Development Company in Toronto & New York, he has helped more than 200+ clients to bring idea in to reality. He has attended many tech conferences as a company representative and frequently blogs about search engine updates, technology roll-outs, sales & and marketing tactics. For more details visit our Website www.iqlance.com/, Email us at info@iqlance.com or Connect with us on all major social media and messaging platforms.


I hope you enjoyed this article about the major things you need to know about Ruby On Rails programming language for developers.

Interested in reading more articles about business technology? 

Read My Blog Posts: 

- The Importance Of Wirless Technology In The Office

- What New Technology Holds For Us

More Lean Startup Life Lessons Below

The Lean Startup Life Media Network Newest Blog Posts: