Skip to main content

Ruby difference between collect and map ?

Ruby difference between collect and map ?

Ruby Difference between collect and map

This is common question for Ruby on Rails developer(Beginner). Map and Collect both are looping methods used on Array, Hash etc. This tutorial will help understanding the exact difference between these two methods.

Let's create sample array for the demo,

a = [1,2,31,4,7,9,21]
=> [1, 2, 31, 4, 7, 9, 21]

Now that array a has been created. Let's perform collect and map operation on array a.
Collect vs Map

    collect and map methods are one and the same considering their implementation.
    C level implementation of these methods explains how these methods are same.
    Refer source of collect and map on APIDock.
    i.e. Collect is alias to Map method.

Thus, we will see what collect/map does operations on Array below
Collect/Map in Action

    Returns Array evaluation condition/statement passed as code block passed to the method
    Internally these methods each on the array passed and evaluates code block and creates new array satisfying the condition.
    The new array is finally returned as the result.

Example,

For array a created above,

b = a.collect{|x| x.even?}
=> [false, true, false, true, false, false, false]

c = a.map{|x| x.even?}c = a.map{|x| x.even?}
=> [false, true, false, true, false, false, false]

The above invocation of collect and map methods will return array. Thus, b and c are Arrays.
If we see the output produced by Ruby Interpreter then, it is clear that b and c have same array value.

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…