New Rubyist, trying to learn a simple contacts storage app

42 views
Skip to first unread message

Anonymity

unread,
Jun 7, 2015, 3:31:44 AM6/7/15
to rubyonra...@googlegroups.com
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

Colin Law

unread,
Jun 7, 2015, 3:39:56 AM6/7/15
to rubyonra...@googlegroups.com
On 7 June 2015 at 08:00, Anonymity <wicket...@gmail.com> 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

Douglas Mills

unread,
Jun 7, 2015, 8:53:28 AM6/7/15
to rubyonra...@googlegroups.com
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 

Douglas Mills

unread,
Jun 7, 2015, 8:53:28 AM6/7/15
to rubyonra...@googlegroups.com
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. 

Mister Reese

unread,
Jun 7, 2015, 3:10:19 PM6/7/15
to rubyonra...@googlegroups.com
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!

Elizabeth McGurty

unread,
Jun 7, 2015, 3:52:47 PM6/7/15
to rubyonra...@googlegroups.com
puts yourHash.to_yaml
Reply all
Reply to author
Forward
0 new messages