Ruby: how to use/turn multi-word strings into symbols - ruby

Most hash keys are single word names. Let's use movie names and ratings as an example. That's easy to make a symbol:
my_hash = { Ferris_Bueller: 5}
What if I don't want to use an underscore in my key name, but need that string as a symbol?
I thought of doing this, but it doesn't work:
my_hash = { "Ferris Bueller": 5}
The issue is, I'm trying to prompt the user to name a movie, rate it, then add those values as a key/value pair in my_hash. People will input two-word movies normally, without the underscore. How do I get that key to store in the hash as a symbol? I.e.:
puts "Name a movie:"
name = gets.chomp
puts "Rate it 1-5:"
rating = gets.chomp
my_hash[name.to_sym] = rating
The user types a two-word movie, such as Robo Cop. Why wont this work?

You can make a multi-word symbol using this notation:
:"multi word symbol" => value

You can also use .gsub! to convert the _ to a space and then .gsub! again to convert it back. This way the code is still registered but the user never sees the underscore. Here's an example:
movies = {
The_Raven: 4,
I_Robot: 3
}
puts "Howdy, please select one of the following."
choice = gets.chomp
case choice
when "update"
puts "Please select which movie you'd like to update."
title = gets.chomp
title.gsub!(/ /, "_")
#By doing this, the user can type 'The Raven' and Ruby will be able to
#register it as a title in your movies hash.
if
movies[title.to_sym].nil?
puts "That movie doesn't exist in our inventory!"
else
puts "What would you like to change the rating to? (Rating must be 1 - 5)"
rating = gets.chomp
movies[title.to_sym] = rating.to_i
title.gsub!(/_/, " ")
#Then you convert it back from an underscore to a space. This way Ruby
#will display to the user the title without underscores.
puts "#{title} has been updated with a new rating of #{rating}."
end`enter code here`

In order to get that working you might need to use hash rockets actually...
my_hash = { :"Ferris Bueller" => 5}

Related

create comma separated string in the First element of an array Ruby

So this may seem odd, and I have done quite a bit of googling, however, I am not really a programmer, (sysops) and trying to figure out how to pass data to the AWS API in the required format, which does seem a little odd.
So, working with resources in AWS, I need to pass tags which are keys and values. The key is a string. The value is a comma separated string, in the first element of an array. So in Ruby terms, looks like this.
{env => ["stage,qa,dev"]}
and not
{env => ["stage","qa","dev"]}
I'm created an admittedly. not a very pretty little app that will allow me to run ssm documents on targeted instances in aws.
I can get the string into an array element using this class I created
class Tags
attr_accessor :tags
def initialize
#tags = {"env" => nil ,"os" => nil ,"group" => nil }
end
def set_values()
puts "please enter value/s for the following keys, using spaces or commas for multiple values"
#tags.each { |key,value|
print "enter #{key} value/s: "
#tags[key] = [gets.strip.chomp]
#tags[key] = Validate.multi_value(tags[key])
}
end
end
I then call this Validate.multi_value passing in the created Array, but it spits an array of my string value back.
class Validate
def self.multi_value(value)
if value.any?{ |sub_string| sub_string.include?(",") || sub_string.include?(" ") }
value = value[0].split(/[,\s]+/)
return value
else
return value
end
end
end
Using pry, I've seen it gets for example ["stage dev qa"] then the if statement does work, then it spits out ["stage","dev","qa"].
and I need it to output ["stage,dev,qa"] but for the life of me, I can't make it work.
I hope that's clear.
If you have any suggestions, I'd be most grateful.
I'm not hugely experienced at ruby and the may be class methods that I've missed.
If your arrays are always coming through in the format ["stage dev qa"] then first we need to split the one string into the parts we want:
arr = ["stage dev qa"]
arr.split(' ')
=> ["stage", "dev", "qa"]
Then we need to join them with the comma:
arr.split(' ').join(',')
=> "stage,dev,qa"
And finally we need to wrap it in an array:
[arr.first.split(' ').join(',')]
=> ["stage,dev,qa"]
All together:
def transform_array(arr)
[arr.first.split(' ').join(',')]
end
transform_array(['stage dev qa'])
=> ['stage,dev,qa']
More info: How do I convert an array of strings into a comma-separated string?
I see no point in creating a class here when a simple method would do.
def set_values
["env", "os", "group"].map do |tag|
puts "Please enter values for #{tag}, using spaces or commas"
print "to separate multiple values: "
gets.strip.gsub(/[ ,]+/, ',')
end
end
Suppose, when asked, the user enters, "stage dev,qa" (for"env"), "OS X" (for"OS") and "Hell's Angels" for "group". Then:
set_values
#=> ["stage,dev,qa", "OS,X", "Hell's,Angels"]
If, as I suspect, you only wish to convert spaces to commas for "env" and not for "os" or "group", write:
def set_values
puts "Please enter values for env, using spaces or commas"
print "to separate multiple values: "
[gets.strip.gsub(/[ ,]+/, ',')] +
["os", "group"].map do |tag|
print "Please enter value for #{tag}: "
gets.strip
end
end
set_values
#=> ["stage,dev,ga", "OS X", "Hell's Angels"]
See Array#map, String#gsub and Array#+.
gets.strip.gsub(/[ ,]+/, ',') merely chains the two operations s = gets.strip and s.gsub(/[ ,]+/, ','). Chaining is commonplace in Ruby.
The regular expression used by gsub reads, "match one or more spaces or commas", [ ,] being a character class, requiring one of the characters in the class be matched, + meaning that one or more of those spaces or commas are to be matched. If the string were "a , b,, c" there would be two matches, " , " and ",, "; gsub would convert both to a single comma.
Using print rather than puts displays the user's entry on the same line as the prompt, immediately after ": ", rather than on the next line. That is of course purely stylistic.
Often one would write gets.chomp rather than gets.strip. Both remove newlines and other whitespace at the end of the string, strip also removes any whitespace at the beginning of the string. strip is probably best in this case.
What do you think about this?, everything gets treated in the Validates method. I don't know if you wanted to remove repeated values, but, just in case I did, so a
"this string,, has too many,,, , spaces"
will become
"this,string,has,too,many,spaces"
and not
"this,,,,string,,,has,too,,many,,,,,,spaces"
Here's the code
class Tags
attr_accessor :tags
# initializes the class (no change)
#
def initialize
#tags = {"env" => nil ,"os" => nil ,"group" => nil }
end
# request and assign the values <- SOME CHANGES
#
def set_values
puts "please enter value/s for the following keys, using spaces or commas for multiple values"
#tags.each do |key,value|
print "enter #{key} value/s: "
#tags[key] = Validate.multi_value( gets )
end
end
end
class Validate
# Sets the array
#
def self.multi_value(value)
# Remove leading spaces, then remove special chars,
# replace all spaces with commas, then remove repetitions
#
[ value.strip.delete("\n","\r","\t","\rn").gsub(" ", ",").squeeze(",") ]
end
end
EDITED, thanks lacostenycoder

What does gets.chomp do?

I was also wondering if I always have to put the exclamation point after .capitalize!
print "What's your first name? "
first_name = gets.chomp
first_name.capitalize!
print "What's your last name? "
last_name = gets.chomp
last_name.capitalize!
print "What city are you from? "
city = gets.chomp
city.capitalize!
print "What state or province are you from? "
state = gets.chomp
state.capitalize!.upcase!
puts "Your name is #{first_name} #{last_name} and you're from #{city}, #{state}!"
What does gets.chomp do?
If you type denisreturn then gets would return "denis\n" where \n is the line feed character from the return key. chomp removes such trailing newline:
"denis\n".chomp
#=> "denis"
I was also wondering if I always have to put the exclamation point after .capitalize!
In general, a bang method is a method like any other: (from the documentation for method names)
The bang methods (! at the end of method name) are called and executed just like any other method.
But:
[...] by convention, a method with an exclamation point or bang is considered dangerous.
Dangerous can mean various things, depending on the context. For built-in methods from Ruby's core library it usually means:
[...] that when a method ends with a bang (!), it indicates that unlike its non-bang equivalent, permanently modifies its receiver.
So capitalize! (with !) will modify first_name: (the method's receiver)
first_name = 'denis' #=> "denis"
first_name.capitalize! #=> "Denis"
first_name #=> "Denis"
capitalize! will also return nil if the string is already capitalized:
first_name = 'Denis' #=> "Denis"
first_name.capitalize! #=> nil
first_name #=> "Denis"
Whereas capitalize (without !) will always return a new (capitalized) string, leaving first_name unchanged:
first_name = 'denis' #=> "denis"
first_name.capitalize #=> "Denis"
first_name #=> "denis"
Apparently, the capitalize call above doesn't make much sense because the return value is not used anywhere. You usually want to do something with that new string like assigning it to a variable:
capitalized_first_name = first_name.capitalize
or passing it to a method:
puts "Your name is #{first_name.capitalize}"
chomp removes the linebreak character at the end of the string input.
Adding the ! will mutate the value of your variable. It is not required. It depends on your use case.
foo = 'foo'
foo.capitalize
print foo
=> 'foo'
bar = 'bar'
bar.capitalize!
print bar
=> 'Bar'
Of course you don't need the "!". Let's try to get by without them:
print "What's your first name? "
first_name = gets.chomp.capitalize
print "What's your last name? "
last_name = gets.chomp.capitalize
print "What city are you from? "
city = gets.chomp.capitalize
print "What state or province are you from? "
state = gets.chomp.upcase
puts "Your name is #{first_name} #{last_name} and you're from #{city}, #{state}!"
There you go. You can avoid mutation and just manipulate your strings with standard methods. No "!" methods need apply!
ps: capitalize followed by upcase is the same as just upcase.

How to match a user-input with a key in the hash table in Ruby

I have a hash:
guide_book = {"Delhi" => "011", "Noida" => "0120", "Indore" => "0731", "beas" => "458", "goa" => "835"}
I want to match the user input string city_name with the key in the hash table. If the input matches any key, then the code should be printed, else a note of invalid city should be printed.
This is the code I wrote:
puts "Please enter the city name"
city_name = gets.chomp
puts "Thank You"
puts "The available cities in the city guide are"
guide_book.each{|k,v| puts k}
guide_book.default = "invalid city"
puts guide_book[city_name]
You can check if a hash contains a key by using has_key?
Something like:
guide_book.has_key?(city_name)
Edit: (Thanks to Jordan for the suggestion)
You can use Hash#key? method, as Hash#has_key? is deprecated (See this).

How can I stop the lines from repeating?

Look at this code. I got the desired result, which was to scan a person's input to see if it matches an internal array.
sentence = []
compare = []
database_array = ["Mouse", "killer", "Blood", "Vampires", "True Blood", "Immortal" ]
def parser_sentence compare
database_array = ["Mouse", "killer", "Blood", "Vampires", "True Blood", "Immortal"]
initial_index = 0
while compare.count > initial_index
compare.each do |item|
if item == database_array[initial_index]
puts "You found the key word, it was #{item}"
else
puts "Sorry the key word was not inside your sentence"
end
end
initial_index = initial_index + 1
end
end
puts "Please enter in your sentences of words and i will parse it for the key word."
sentence = gets.chomp
compare = sentence.split (" ")
Because each loop is telling it to repeat, it does so, but how can I stop this?
In this case, regex will be more efficient and less error prone than splitting the input string, especially since you have a two-word phrase in the keyword list.
def parser_sentence(sentence)
matching_words = sentence.scan(Regexp.union(database_array))
if matching_words.empty?
puts "Sorry the key word was not inside your sentence"
else
puts "You found the key word, it was #{matching_words.join(" ")}"
end
end
Slight modifications can make it case sensitive (if you need it), or add word boundaries to the keywords so as to not match partial words.
One possible solution that doesn't involve looping is to intersect your compare and database_array arrays, like so:
matching_words = compare & database_array
This will compare both arrays and create a new array containing only elements that are common to both. For example:
# If the user input the sentence "The Mouse is Immortal", then...
compare = ["The", "Mouse", "is", "Immortal"]
# matching_words will contain an array with elements ["Mouse", "Immortal"]
matching_words = compare & database_array
You can then check the length of the array and display out your messages. I believe this can replace your entire function like so:
def parser_sentence compare
matching_words = compare & database_array
if matching_works.length > 0
puts "You found the key word, it was #{matching_words.join(" ")}"
else
puts "Sorry the key word was not inside your sentence"
end
end
Note about the use of join, if you're unfamiliar with that, it basically creates a string using each element in the array separated by the separator string passed in, which in my example is merely a blank space; substitute for your own separate of course, or whatever you want to do with it.

Ruby- find strings that contain letters in an array

I've googled everywhere and can't seem to find an example of what I'm looking for. I'm trying to learn ruby and i'm writing a simple script. The user is prompted to enter letters which are loaded into an array. The script then goes through a file containing a bunch of words and pulls out the words that contain what is in the array. My problem is that it only pulls words out if they are in order of the array. For example...
characterArray = Array.new;
puts "Enter characters that the password contains";
characters = gets.chomp;
puts "Searching words containing #{characters}...";
characterArray = characters.scan(/./);
searchCharacters=characterArray[0..characterArray.size].join;
File.open("dictionary.txt").each { |line|
if line.include?(searchCharacters)
puts line;
end
}
If i was to use this code and enter "dog"
The script would return
dog
doggie
but i need the output to return words even if they're not in the same order. Like...
dog
doggie
rodge
Sorry for the sloppy code. Like i said still learning. Thanks for your help.
PS. I've also tried this...
File.open("dictionary.txt").each { |line|
if line =~ /[characterArray[0..characterArray.size]]/
puts line;
end
}
but this returns all words that contain ANY of the letters the user entered
First of all, you don't need to create characterArray yourself. When you assign result of function to a new variable, it will work without it.
In your code characters will be, for example, "asd". characterArray then will be ["a", "s", "d"]. And searchCharacters will be "asd" again. It seems you don't need this conversion.
characterArray[0..characterArray.size] is just equal to characterArray.
You can use each_char iterator to iterate through characters of string. I suggest this:
puts "Enter characters that the password contains";
characters = gets.chomp;
File.open("dictionary.txt").each { |line|
unless characters.each_char.map { |c| line.include?(c) }.include? false
puts line;
end
}
I've checked it works properly. In my code I make an array:
characters.each_char.map { |c| line.include?(c) }
Values of this array will indicate: true - character found in line, false - character not found. Length of this array equals to count of characters in characters. We will consider line good if there is no false values.

Resources