[Ruby]: Update hash values with Array

Hi Folks,

In this small tutorial, I am writing a program that updates value of each element in hash with a given array values.
Given that:
h = { h1: 'value1', h2: 'value2', h3: 'value3', h4: 'value4' }
a = [ '1', '2', '3', '4' ]
Where h is a Hash and a is an Array

Output we would like to achieve here is:
h = { h1: 'value1_1', h2: 'value2_2', h3: 'value3_3', h4: 'value4_4' }

Let’s code it:

h = { h1: 'value1', h2: 'value2', h3: 'value3', h4: 'value4' }
a = [ '1', '2', '3', '4' ]

h.each_with_index do |(k, v), i|
break if a.size > h.keys.size

a.each_with_index { |val, index|
if i == index
h[k] = v.to_s + ‘_’ + val.to_s
end
}
end

p h.inspect
# Output => h = { :h1 => “value1_1”, :h2 => “value2_2”, :h3 => “value3_3”, :h4 => “value4_4” }

I’ve tried to minimize the number of loops here. Appreciate your feedback and suggestions.

Happy Coding! ๐Ÿ™‚

Advertisements

Learning RSPEC with Rails 4 (Alpha)

Hi Folks,

Hope you all are fine and doing good! I am sure most of you, are using TDD for years now. And, once we think of TDD, the first thing comes into our mind is, RSPEC. We all agree that Rspec is a great framework of Test-Driven-Development and its integration with other libraries (like fixtures etc.) is seamless.

With rapidย revolution happening in Rails community, Rspec too has evolved over the years, and my motto is to bring this newly developed RSPEC framework in such a way so that, those who are eager to learn TDD can take tips and take this as a learning material. And those, who are planning to migrate can refer to this!

Environments I am using:

  • Ruby 2.1.2
  • Rails 4.2.1
  • Rspec 3.2.0

Idea is to share small code snippets while learning these changes, where we see: what is new. So let’s explore this now:

1. In Rspec 3, the support for ‘should‘ (e.g. foo.should == bar) is deprecated and is no longer supported. Instead of ‘should’, rspec encourages us to use ‘expect‘. Let’s see how it works:

If you try running this example, you’ll get this deprecation warning message:

Deprecation Warnings:

Using `should` from rspec-expectations‘ old `:should` syntax without explicitly enabling the syntax is deprecated. Use the new `:expect` syntax or explicitly enable `:should` with `config.expect_with(:rspec) { |c| c.syntax = :should }` instead

Now, let’s take an example of controller spec. Create a controller with scaffold and write the spec as below:

I’ll cover model, helper and view spec very soon.

Happy Coding ๐Ÿ™‚

[Ruby] Fetching Emails from Microsoft Outlook

Hi Folks,

Hope everyone is fine and doing good. In this article of mine, I am going to narrate, how you can fetch Emails from your Microsoft Outlook account. I am only going to cover ‘Inbox‘ here. However, the scope of library (Gem) is very vast and you can customize this as per your need.

PREREQUISITE:

  • Ruby
  • ViewPoint Gem: gem install viewpoint

For Debugging, I am using viewpoint/logging/config

Hope this helps someone! Keep in touch and Happy Coding ๐Ÿ™‚

Puneet

Rails 4: Changeset

Hey Everyone,

I know, I am very late in posting this article, as I didn’t get chance to do much hands-on in Rails 4. But now, that we all are on the same page, let me quickly set the summary here.

Most of us (1) have already started with Rails4, most of them (2) are planning to and most of them (3) are still on Rails 3. So my target audience for this post is #2 and #3. Developers working on Rails4, if they wish to provide any comments/suggestions they are most welcome.

Recently, I got a chance to develop a new application in Rails4. There are many articles available online, which will provide you insight about the changeset in Rails4. But my idea is to give a step by step information, as I continue to work on my existing application. Hence, this post will keep updating at regular intervals.

Folks can take the information from here which is relevant to them.

– If you wish to localize your gems (including Rails4), to your application, you must need to run:

After, rails new {APP_NAME}, then your bundle runs. Once it is finished, run this command:
bundle install --path vendor/gems

This command will create a .bundle folder inside your app. If you open this folder, there is a config file, where path of the gems is defined. You don’t need to define the path again and again. This config file will take care of it.

– If you have a pending migration and you’re trying to browse your application, Rails tells you to do a rake db:migrate before you see you app in action. See attached screenshot for more reference.

Pending Migration Rails4

Including modules in Rails4: If you wish to include any re-usable module in a common place (say application_controller.rb) from your {Rails.root}/lib folder, then you must need to tell your application to load the path. To do this, type:
config.autoload_paths += %W(#{config.root}/lib)

Model.find_or_create_by_{COLUMN_NAMES} is deprecated in Rails4. To use it:
Model.where(col_1: SOME_VALUE, col_2: SOME_VALUE).first_or_create

ActiveRecord::Relation is now changed to #<ActiveRecord::Associations::CollectionProxy []>

– using of conditions while defining associations is no longer supported. Till Rails 3, one can use this syntax:

– Adding Stylesheets: If you’re trying to add any stylesheet into layout, You must need to add it under ‘config/initializers/assets.rb’
Rails4 shows it in a very beautiful way (refer to the attached screenshot)

assets handling in rails4
assets handling in rails4

ActionMailer: deliver_now & deliver_later: Action Mailer is now integrated with Active Job so that you can send emails outside of the request-response cycle, hence the user doesn’t have to wait for the request to complete.

However, if you wish to send the mail straightaway, use deliver_now.

UserMailer.welcome_mail(@user).deliver_later
UserMailer.welcome_mail(@user).deliver_now

Kick-Off: First Rails 4 Application with HABTM Association

Hello Folks,

Its been a long time since I’ve not written a post. Now, I realize that it’s time to share some useful snippet with all of you.

Recently, I got a chance to get my hands cleaned with Rails 4. So much is happening around Rails community and with the rapid development of Rails 4, I was finding it bit difficult to keep my momentum going.

Well, leave all this apart. Now, by putting some extra efforts my system is ready with:

Ruby 2.1 & Rails 4.1

In order to start with my first Rails 4 Application, I took HABTM Association (weird, isn’t it?). It came randomly into my mind. However, I will dig into other associations as well with time, but to start with let’s take HABTM association with example.

I assume most of you are aware what changes Rails 4 has introduced. If you’re new to Rails 4 or would like to re-visit the changes, here’s the most useful link I’ve found. Just take a look.

Once you’re sure about changeset in Rails4, let’s draw an example:

Prerequisites:

  • Ruby 1.9.2+
  • Rails 4.1.0+
  • Traditional Database (MySQL/Postgres)

Key-Entities:

  • Person Model
  • Communtiy Model
  • A Join Table

Defining the Relationships & creating tables:

For rest of the code-flow (controllers, views) you can download the zip of the application from my GitHub page, run it on your local and see Rails4 HABTM in action.

Happy Coding ๐Ÿ™‚

Add duplicate rows in Excel using Ruby

Happy New Year Folks,

Few days back a friend of mine came to me and said, “I am a RUBY Programmer and I am stuck in a requirement, wherein I have to read an excel-sheet (that contains multiple records and each record can have multiple entries in it.)”

Think of an example like given in the link:ย timesheet. It says we have 2 columns: EmployeeID and WorkingHours. Each EmployeeID can have multiple working hours.

Now, I would like to write a small Ruby program that iterate over each record in the excel, combine EmployeeID’s that has multiple entries and add working hours.

Let’s code it then!

Requirements:

  • Ruby 1.9.2 or higher
  • Spreadsheet gem: to read Excel
  • Download timesheet excel from the link above

Happy Coding! ๐Ÿ™‚

Installing mysql2 gem with Ruby 2.0

Hi Folks,

In this short tutorial of mine, I am going to write down the steps of how you can install mysql2 gem with Ruby2.0 on Windows machine.

Installing Mysql gem is always a big time pain for most of the developers who are working on Windows machine. But thanks to active folks over the internet for their comments, posts.

Prerequisites:

1. Ruby 2.0.0 pre-installed.
2. DevKit installed
3. gem install rails – command should install all the gems. Return should be success.

Steps to install mysql gem:
1. Go to: http://dev.mysql.com/get/Downloads/Connector-C/mysql-connector-c-noinstall-6.0.2-win32.zip/from/pick, download will start automatically (don’t think at this moment that you’ve a 32 bit or 64 bit operating system.

2. Extract the folder to some place (in my case I am extracting it to C:\mysql-connector-c-noinstall-6.0.2

3. Run this command:

4. Output will return in below manner:

5. If you see this message, create a new Rails4 application, open your Gemfile and use:
gem 'mysql2', '0.3.13'

and run bundle install. Once bundle install runs successfully. You should try:
rake db:create and
rake db:migrate

to ensure that gem is installed correctly.

Hope this will help someone!

Happy Coding!! ๐Ÿ™‚