Thursday, 16 January 2014

Ruby - Getting and Using User Input




Get, Use and Respond to User Input

Our Ruby scripts and programmes won’t be very interesting if we can’t get some input from the user. When we do, we’ll want to use it to do interesting and exciting things. This may be simply replaying the same input to the user, manipulating, storing or combining it, etc. Let’s look at getting some basic input, modifying it a little then playing it back to the user.

First thing, make a new .rb file, save it to your folder with other scripts and open it in your favourite editor. In the .rb file type:   puts “What’s your name?”  This will let the user know we need them to do something. Input from a user needs to be stored somehow and that somehow is via variables. Ruby has a whole bunch of rules around variables, but we’ll go into them in depth in a later post / video. For now let’s create a variable called name, as it’s easy to remember, then tell Ruby it’s equal to whatever string we get from the user. We do this by using the keyword gets as in get string. Pulling this together, we type: name = gets in our .rb file.

Now follow this by putting the string back to the screen, with a little message. To do that we use puts again. Put String, get it? Ruby is easy eh! In the .rb file type:
puts “Your name is “ + name

Your file should now have the following three lines and save the file:

puts "What’s your name? "
name = gets
puts "Your name is" + name + "."

You can also use print to output the user input as well, add the following, then we’ll see what happens:

print "Your name is " + name + "."

Don’t double click the file, instead open a CMD window, navigate to and run the file. For me that would be C:\Dev\ruby>gets.rb to run the file. In fact you could just run gets, leaving off the .rb as your system will know to use Ruby to run .rb files. Just like it knows how to open .txt, .jpg, etc. with the correct software.


 See what happened? The period at the end of the sentence is on another line! This is because gets adds a newline \n at the end of the string that it receives. Prove this by opening irb in a CMD window and typing gets then a string.









A simple way to remove this is by adding the word chomp after gets, like this

name = gets.chomp

Run it again and see what the effect is. Seem OK? Try changing the script to look like this, then run it again.

puts "What's your name?"
name = gets.chomp

puts "Your name is " + name + "."
puts "Your name is " + name + "."

print "Your name is " + name + "."
print "Your name is " + name + "."

How about now? What we find is the layout is messed up again. In that the last two lines using print, are running on the same line. Again this is because puts adds a \n newline at the end of the string!










OK, all good and slightly uninteresting but there’s a reason I’m harping on about this. When using Ruby, take the time to understand what each element, class, method does by a) reading the Ruby Docs and b) experimenting. If you see something ‘odd’, make a simple example and test out different variations.  The \n is a great example of how you WILL get caught out by something really really simple, that might take you hours to work out! Don’t worry about that. Just practice and do your research. Another example, change out the + symbols for & instead, as other languages would have it… you get the idea of simple syntax issues breaking things. Test often!

To tidy this script up just modify the first print line to read:
print "Your name is " + name + ".\n"


A little tidy up

Though we can write our lines with all the breaks and + symbols, there’s a way to make our line of code above much tidier, by using the #{} construct. Change the puts lines to read:

puts "Your name is #{name}."

It’s a much neater way to write out lines like this where we need to use the contents of a variable. Now let’s try calling a few methods and from some Classes we’ve in fact already been using.

puts "Your name is #{name.capitalize}."

Then enter say, david, instead of David and see what happens.

There’s a lot going on in the above and hopefully is a useful primer. We’ve covered variables, classes, methods, getting and transforming data along with learning a few gotchas that give us an idea of what to look out for elsewhere. Have a look over the Ruby Docs for what we’ve covered and get used to reading these to learn Ruby thoroughly.


Read More
http://cyreath.blogspot.co.uk/2014/02/ruby-constants.html
http://cyreath.blogspot.co.uk/2014/02/ruby-instance-and-class-variables.html
http://cyreath.blogspot.co.uk/2014/02/ruby-global-variables.html
http://cyreath.blogspot.co.uk/2014/02/ruby-local-variables.html
http://cyreath.blogspot.co.uk/2014/02/ruby-variables-categories-and-scope.html
http://cyreath.blogspot.co.uk/2014/01/ruby-variables-part-1.html
http://cyreath.blogspot.co.uk/2014/01/download-and-install-ruby.html


Links
Here's the links to the relevant Ruby docs.









0 comments: