Inspect method

Hi
I want to know exactly what does inspect method do ?
and What does happened , if we don’t use inspect ?
thanks

On Aug 9, 2011, at 9:49 AM, amir e. wrote:

Hi
I want to know exactly what does inspect method do ?
and What does happened , if we don’t use inspect ?
thanks


Posted via http://www.ruby-forum.com/.

It depends (inspect can be overridden), but in general the to_s method
of the object is called. See more information here:

http://ruby-doc.org/core/classes/Object.html#M001025

Regards,
Chris W.
http://twitter.com/cwgem

Hello Chris,

On 9 Αυγ 2011, at 7:56 μ.μ., Chris W. wrote:

It depends (inspect can be overridden), but in general the to_s method of the
object is called. See more information here:

class Object - RDoc Documentation

Regards,
Chris W.
http://twitter.com/cwgem

I don’t know if Amir got it, but I certainly don’t. After throwing a few
‘irb’ lines I still don’t understand where should I use inspect of each.

“Time.new.inspect == puts x = Time.new”

Which as you said makes sense, because turns the obj into string, but
can you explain a bit the list example?

I have the same problem with .map which is often used by many
developers…

I don’t understand the difference between map and each. I find them to
have the same results.

regards


Panagiotis A.

email: [email protected]
blog: http://www.convalesco.org

The wise man said: “Never argue with an idiot. They bring you down to
their level and beat you with experience.”

On Tue, Aug 9, 2011 at 11:49 AM, amir e. [email protected] wrote:

Hi
I want to know exactly what does inspect method do ?

It returns a string representation of the object. It differs from to_s
in
that it is intended to be informative as opposed to pretty. For example:

require ‘date’

today = Date.today
today.inspect # => “#<Date: 2011-08-09
((2455783j,0s,0n),+0s,2299161j)>”
today.to_s # => “2011-08-09”

and What does happened , if we don’t use inspect ?

In what context?

I don’t know if Amir got it, but I certainly don’t. After throwing a few ‘irb’
lines I still don’t understand where should I use inspect of each.

“Time.new.inspect == puts x = Time.new”

Which as you said makes sense, because turns the obj into string, but can you
explain a bit the list example?

I have the same problem with .map which is often used by many developers

I don’t understand the difference between map and each. I find them to have the
same results.

I’m not sure why inspect is being referenced with map and each. However
I’ll first address inspect:

irb(main):008:0> x = “#{[1,2,3]}”
=> “[1, 2, 3]”
irb(main):009:0> x.eql? [1,2,3].inspect
=> true
irb(main):010:0> [1,2,3].inspect
=> “[1, 2, 3]”

The string interpolation of the array [1,2,3] calls to_s behind the
scenes, producing a string representation of the array. If we check this
against the output of the same array with the inspect method called on
it, notice how the string is equal. This is because inspect is also
calling to_s of the array, as shown by the final irb output. However, if
we have an object that overrides both to_s and inspect:

class MyObj

attr_reader :name, :age

def initialize(name, age)
@name = name
@age = age
end

def to_s
“#{@name} #{@age}”
end

def inspect
“[MyObj] name: #{@name} age: #{@age}”
end

end

Now I’ll load this file in an irb session:

irb(main):001:0> load ‘class.rb’
=> true
irb(main):002:0> obj = MyObj.new(“John”, 200)
=> [MyObj] name: John age: 200
irb(main):003:0> “#{obj}”
=> “John 200”
irb(main):004:0> “#{obj}”.eql? obj.inspect
=> false
irb(main):005:0>

The result is now false because I’ve overridden the inspect method to
produce a custom result different from to_s. This is what I meant by "it
depends. Now to answer you question about map or each, it’s important to
check the result:

irb(main):003:0> [1,2,3].each { | x | x * x }
=> [1, 2, 3]
irb(main):004:0> [1,2,3].map { | x | x * x }
=> [1, 4, 9]

Inside the block, the actions are the same. the element is multiplied by
itself. However each still has the original array while map returns a
new array with the result of the modification of each element, or more
specifically the result of each iteration of the block:

irb(main):005:0> [1,2,3].map { | x | “a” }
=> [“a”, “a”, “a”]

Since “a” is the return of the block, all array elements are set to “a”,
totally disregarding the original value. Map can also be used to modify
the original array by using the “!” suffixed version:

irb(main):006:0> x = [1,2,3]
=> [1, 2, 3]
irb(main):007:0> x.map! { | x | x * x }
=> [1, 4, 9]
irb(main):008:0> x
=> [1, 4, 9]

In this case the original value of x is lost and replaced with an array
that contains the results of the block called on each element. Does that
make sense?

Regards,
Chris W.
http://www.twitter.com/cwgem

On Tue, Aug 9, 2011 at 4:32 PM, Panagiotis A.
[email protected]wrote:

I have the same problem with .map which is often used by many developers

I don’t understand the difference between map and each. I find them to have
the same results.

regards

I talk about each and map in Ruby Kickstart Session 2 @ 5:05, you might
find
it useful. http://ruby-kickstart.com/#session2

inspect is especially useful in logging/debugging. Apparently, irb
calls inspect when outputting the value of an expression. Check this
out:

ree-1.8.7-2011.03 :007 > a = [1,2]
=> [1, 2]
ree-1.8.7-2011.03 :008 > a.to_s
=> “12”
ree-1.8.7-2011.03 :009 > a.inspect
=> “[1, 2]”
ree-1.8.7-2011.03 :010 > h = { :fruit => ‘apple’, :color => ‘yellow’ }
=> {:fruit=>“apple”, :color=>“yellow”}
ree-1.8.7-2011.03 :011 > h.to_s
=> “fruitapplecoloryellow”
ree-1.8.7-2011.03 :012 > h.inspect
=> “{:fruit=>“apple”, :color=>“yellow”}”

  • Keith

Hello,

On 10 Αυγ 2011, at 2:45 π.μ., Josh C. wrote:

I talk about each and map in Ruby Kickstart Session 2 @ 5:05, you might find
it useful. http://ruby-kickstart.com/#session2

Thank you all for the extensive replies :slight_smile:

Best Regards

Panagiotis A.

email: [email protected]
blog: http://www.convalesco.org

The wise man said: “Never argue with an idiot. They bring you down to
their level and beat you with experience.”