Skip to main content

Check if key exists in hash in rails

In Ruby on Rails Programming you might have to check if key exists in hash and based on that you might have to perform other operation. There is simple way to detect if key exists in particular hash.


Ruby Hashes
:

Ruby hash contains Key-Value pairs. In Ruby, you can create a simple hash as follows,

my_hash = {}

Above code will initialize empty hash. Let's add some key value pair in the my_hash

my_hash['name'] = 'Ruby in Rails'

The above code will add 'name' key and 'Ruby in Rails' as it's value. Let's add one more key-value pair in the my_hash

my_hash['type'] = 'Ruby Programming Tutorials'



Now,
How to check if key exists in hash

You can check if particular key exists in a hash or not by using has_key? method available for the hash object in ruby.
Syntax     Description
hash#has_key? 'sample'     This will return true/false by checking if key 'sample' exists in hash object for which the method is invoked

my_hash.has_key? 'name'

=> true
This will return true as 'name' key exists in the my_hash hash

my_hash.has_key? 'abc'

=> false

Above code returns false as my_hash doesn't contain any key with named 'abc'


Performance of Hash#has_key? versun Array#include?
Parameter     Hash#has_key?     Array#include
Time Complexity     o(1) operation     o(n) operation.
Access Type     Accesses Hash[key] if it returns any value then true is returned to the Hash#has_key? call     Iterates through each element of the array till it finds the value in Array

You can find more about ruby hashes and various methods that can be used with hashes on apidock. So we have learned how to check if key exists in hash for Ruby language and compared it with include operation of array and found has_key is is faster as it is o(1) lookup as explained above.

Comments

Popular posts from this blog

Gem::LoadError (Specified 'mysql2' for database adapter, but the gem is not loaded. Add `gem 'mysql2'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord).?

Gem::LoadError (Specified 'mysql2' for database adapter, but the gem is not loaded. Add `gem 'mysql2'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord).) In rails?


1)change the gemfile in mysql version
 gem 'mysql2'

2)Change to mysql2 version
 gem 'mysql2','0.3.20'

Get user accessing ip address in rails?

Get user accessing ip address rails?

request.ip ->returns the ip, whether is is a local proxy ip (localhost address) or not.
*request.remote_ip -> is smarter and gets the ip address of the client outside of local proxies.
3)If you are using apache in front of a mongrel, then remote_ip will return the source address of the request, which in this case will be local host because the Apache web server is making the request, so instead put this in your controller:

@remote_ip = request.env["HTTP_X_FORWARDED_FOR"]

PayUMoney Integration in Ruby on Rails

PayUMoney Integration in Ruby on Rails


Generating Cheksum in Payu
   Hash or Checksum
= sha512(key|txnid|amount|productinfo|firstname|email|u
df1|udf2|udf3|udf4|udf5||||||salt)



Controller Code Will be
def payumoney
  @key = "gtKFFx"
  @salt =  "eCwWELxi"
  val = "#{key|txnid|amount|productinfo|firstname|email|u
df1|udf2|udf3|udf4|udf5||||||#{@salt}"
 @checksum = Digest::SHA512.hexdigest("#{val}")
end

<form action="https://test.payu.in/_payment" method='post' id='sub'  >
    <input type="hidden" name="key" value="<%=@key%>">
    <input type="hidden" name="hash" value="<%=@checksum%>">
    <input type="hidden" name="txnid" value="TXN1234">
    <input type="hidden" name="amount" value="200">
    <input type="hidden" name="firstname" value=&quo…