Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Ruby Foundations Hashes Creating Hashes

Iulia Soimaru
Iulia Soimaru
9,931 Points

Creating Hashes

Hello, I have problem with creating Hash

here is a code

Iulias-MacBook-Pro:~ iuliasoimaru$ gem install awesome_print
Successfully installed awesome_print-1.2.0
Parsing documentation for awesome_print-1.2.0
1 gem installed
Iulias-MacBook-Pro:~ iuliasoimaru$ ruby -v
ruby 2.0.0p594 (2014-10-27 revision 48167) [x86_64-darwin13.3.0]
Iulias-MacBook-Pro:~ iuliasoimaru$ irb
2.0.0-p594 :001 > require "rubygems"
 => false 
2.0.0-p594 :002 > gem "awesome_print"
 => true 
2.0.0-p594 :003 > require "awesome_print"
 => true 
2.0.0-p594 :004 > h = Hash.new
 => {} 
2.0.0-p594 :005 > h = {}
 => {} 
2.0.0-p594 :006 > h = {"hello" => "world"}
 => {"hello"=>"world"} 
2.0.0-p594 :007 > ap h
{
    "hello" => "world"
}
 => nil 

Why do I receive => nil at the end? In the video I see he receives {"hello" => "world"}

I know they for version 2.0.0 I don't need to require "rubygems" just "awesome_print", but it didn't work.

Thanks for help

1 Answer

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

So I tested this with Ruby 2.0.0 and 1.9.3-p545 and it behaves the same way - returns nil. And I believe that is the behavior we should be looking for - the one in the video is not what should be happening (I suppose it got fixed in the newer version of awesome_print). Functions that are used for printing stuff on screen should only print stuff and then return nil, not the whole value that they just printed. Just like puts returns nil.

Yep, as Maciej pointed out the nil you're seeing is just the return value from awesome_print which really should be nil as it's a print statement.

If you install the specific version Jason is using in the video here, 1.0.2, you'll see a return value.

gem install awesome_print -v '1.0.2'
>> require 'awesome_print'
=> true
>> h = {'hello' => 'world'}
=> {"hello"=>"world"}
>> ap h
{
    "hello" => "world"
}
=> {"hello"=>"world"}