New Rubyist, trying to learn a simple contacts storage app

Hi Everyone,

Here is the code I currently have:

contacts = Hash.new

x = 0
y = 0

if x <= 5
puts “What contact would you like to add?”
ui = gets.chomp
contacts.store(y, ui)
puts “Contact added is #{contacts}”
x += 1
y += 1
puts “X is #{x}, Y is #{y}” # so I know if the values are being
incremented
else
puts “You’ve reached the maximum # of contacts.”
end

Also, how can I print a hash to the console without a key value?

Thank you

On 7 June 2015 at 08:00, Anonymity [email protected] wrote:

Hi Everyone,

Here is the code I currently have:

Also, how can I print a hash to the console without a key value?

Is this to be part of a Rails application? You don’t have a console
in a rails app, everything is done via the browser.

Colin

Hi,

The key / value pairs can be iterated like this:

str = ‘’
contacts.each do |key, value|
str += key.to_s + ’ : ’ + value + ', ’
end

puts 'contacts output : ’ + str

There are more compact ways to do this but will not be as easy to see
what
is happening.

Kind Regards,

Doug

Colin :

The Rails console can be accessed a few ways but in a rails app it’s a
terminal command :

RAILS_ENV=development bundle exec rails c

This will load the code and dependencies and give you a console.

There are other ways to run code in situ, like the gem better_errors
(with
binding of caller) which will allow you to execute most commands in a
browser like you would in a rails console session.

puts yourHash.to_yaml

Thank you, Douglas. It’ll take me a bit to see what your code is doing
but
I’m determined to learn. I appreciate it!