Switch case with regex in Ruby? - ruby-on-rails

I made a Ruby script which uses a regex to cut line from a file and paste it in a new file. That part works fine. Now I want to enter a region into the console and the output should show me just the region which i typed in before.
For example I'm looking for this "ipc-abc-01" and type abc in, now it should show me all entries with abc.
found = 0
words = []
puts "Please enter a word to search the list"
input = gets.chomp
#Open text file in read mode
File.open("list.txt", "r+") do |f|
f.each do |line|
if m = line.match( /\b#{input}\b/i )
puts "#{m[0]} "
# ... etc.
found = 1
end
end
end

Is this what you were looking for? this searches all strings by user given input in regex form that is case insensitive..
words = []
print "Please enter a word to search the braslist: "
input = gets.chomp
#Open text file in read mode
File.open("braslist.txt", "r+") do |f|
f.each do |line|
words += line.split(" ").grep(/#{input}/i)
end
end
puts "#{words.length} words found!"
puts words

To match all words containing zhb in: zhb440 zhb440 izhh550 ipc-zhb790-r-br-01you can use:
\S*?(?:zhb)\S*
if line =~ /\S*?(?:zhb)\S*/
match = $~[1]
else
match = ""
end
See the DEMO

Related

How to get my API to return more than one value in Ruby

My CLI project is working almost correctly, however, is only returning one value. I would like my program to return multiple values.
Here's the code:
def display_info
puts "You'll love the following spots!"
puts "********************************"
#objects.each.with_index(1) {|brewery, index| puts "#{index}. #{brewery.name}"}
puts "Please make a selection by index number for more information:"
puts "****************************************************"
puts "Type Quit to end. Type Menu to try another location."
input = gets.strip.downcase
if(input.to_i > 0)
#brewery = #objects[input.to_i - 1]
puts "name: #{#brewery.name}"
puts "street: #{#brewery.street}"
puts "city: #{#brewery.city}"
puts "phone: #{#brewery.phone}"
puts "website_url: #{#brewery.website_url}"
display_info
elsif (input == "quit")
quit
elsif (input == "menu")
start
else
puts "Ooops, please try again to get more info:"
display_info
end
end
Here's the input result if that helps.
Austin
You'll love the following spots!
********************************
1. Oasis Texas Brewing Company
Please make a selection by index number for more information:
****************************************************
Type Quit to end. Type Menu to try another location.
quit
Goodbye. Drink responsibly and enjoy.
What can I do to return more than one value?
Here's the source of this code. This is the content.
class Breweries::API
def self.get_breweries(input)
#breweries_hash = HTTParty.get("https://api.openbrewerydb.org/breweries?by_city=#{input}")
return if #breweries_hash.empty? #empty array handle
breweries_obj = {
name: #breweries_hash[1]["name"],
street: #breweries_hash[3]["street"],
city: #breweries_hash[4]["city"],
phone: #breweries_hash[10]["phone"],
website_url: #breweries_hash[11]["website_url"]
}
Breweries::HoppyCode.new(breweries_obj)
end
end
end
You need to split the string you capture using gets with a character. You should ask for it in your message, normally that would be a comma, possibly surrounded by spaces.
I extracted the problem in a simplified script so that you can test it separately, which is always a good idea when solving a problem.
Mind the use of regular expressions between the // delimiters, and followed by the i flag to indicate that the comparison should be case-insensitive.
# what you would receive when you type this in the console following your message
# and captured by the gets, in this case brewerie 1 and 3
input = "1, 3"
#split by a comma preceded or followed or not by a space
breweries = input.split(/ *, */)
breweries.each do |brewerie|
if(brewerie.to_i > 0)
# #brewery = #objects[input.to_i - 1]
puts "displaying info about #{brewerie}"
elsif brewerie[/quit/i]
# quit
elsif brewerie[/menu/i]
# start
else
puts "Ooops, please try again to get more info:"
# display_info
end
end
Which returns:
displaying info about 1
displaying info about 3
From what I understand, You are asking the user to pick a brewery from a list of breweries. When they pick one, you show its info to them. What you could do instead is display a list of cities and let them pick a city and select all elements of the #objects where the city matches
def display_info
arr_of_cities = []
#objects.each{|element| arr_of_cities.push(element.city)}
arr_of_cities.each.with_index(1) {|city, index| puts "#{index}.#{city}"}
puts "Please make a selection by index number for more information:"
puts "****************************************************"
puts "Type Quit to end. Type Menu to try another location."
input = gets.strip.downcase
arr_of_breweries = #objects.where(city:arr_of_cities[input.to_i- 1])
if(input.to_i > 0)
arr_of_breweries.each.with_index(1) do |brewery,index|
puts "#{index}"
puts "name: #{#brewery.name}"
puts "street: #{#brewery.street}"
puts "city: #{#brewery.city}"
puts "phone: #{#brewery.phone}"
puts "website_url: #{#brewery.website_url}"
end
display_info
elsif (input == "quit")
quit
elsif (input == "menu")
start
else
puts "Ooops, please try again to get more info:"
display_info
end
end
Hopefully that helps. I haven't used ruby in a while so my syntax might be off a bit.

I trying to make a code that gives the user a personal number after they have made an user

Here is my ruby code. When you run it and you press 1 it will ask you for name and birth of date. I want to give the user a personal number after he is finished typing name and birth date. Futhermore I would be great to search for the users number to find them in the file. Hope someone can help!
I have written #HELP HERE were i need help. The code works fine, but I dont know how to code my problem...
file = File.new("Capgemini.txt", "a") #load information on startup, and create the file
class Customer # Making a class for the whole code to call it up later in the code
def new_custom
er # The costumer method
prompt = "> " #creating a class for prompt here, since I use it multiple times
puts"Full name of the person?"
print prompt
#name = gets.chomp.upcase #A global variabel which i can use outside the class
if File.read("Capgemini.txt").include?(#name) #so you don't register the same name, twice
puts"This name is already stored. Returning you to the main menu."
puts "_____________________________________________"
else
#puts"What is your employee number?"
#print prompt
##number = gets.chomp #Global
puts"Date of birth? (DD/MM/YYYY)"
print prompt
#birth = gets.chomp #Global
puts"Thanks for the input."
puts "_____________________________________________"
puts"Is this information correct? " #Giving the user all the information back to check for mistakes, etc.
puts ("Name: #{#name} Number: #{#number} Date of birth: #{#birth}")
puts "_____________________________________________"
puts "Yes or No?"
print prompt
while user_input = gets.chomp.upcase #loop while getting user input
case user_input
when "YES"
file = File.new("Capgemini.txt", "a")
file.puts("#{#name}, Number: #{#number}, Date of birth: #{#birth}") #puts the information into the textfile, separeted by commas
file.close
#NEED HELP HERE
number = File.readlines('Capgemini.txt')
puts "_____________________________________________"
puts
puts "Your employee number: "
puts "_____________________________________________"
#NEED HELP OVER HERE^
puts
puts "The information has now been stored in the Capgemini.txt file."
puts "_____________________________________________"
break # make sure to break so you don't ask again
when "NO"
puts "The information has not been stored. Returning you to the main menu."
puts "_____________________________________________"
break # and again
else
puts "Please either write 'Yes' or 'No'"
print prompt # print the prompt, so the user knows to re-enter input
end
end
end
end
def search_customer(search)
keyword = File.readlines('Capgemini.txt') #converting all the lines into indexes in an Array
matches = keyword.select { |name| name[/#{search}/] } #
if File.read("Capgemini.txt").include?(search) #An if statement that will print the results if the textfile matches the keyword
puts "_____________________________________________"
puts ("Search results including the word/number/birth " + search + ":")
puts "_____________________________________________"
puts matches
puts "_____________________________________________"
else #If not it will give the user feedback that its not there
puts "_____________________________________________"
puts ("Sorry, we couldnt find #{search} in the textfile.")
puts "_____________________________________________"
end
end
def all_customers
f = File.new("Capgemini.txt","r")
while !(f.eof?)
line = f.gets()
puts line
end
end
def delete_customer
puts("What customer do you want to delete?")
print("> ")
keyword = gets.chomp.upcase
txt_file = File.readlines('Capgemini.txt')
matches = txt_file.select { |name| name[/#{keyword}/] }
search_results = matches.length
if search_results > 1
puts "_____________________________________________"
puts "The name you entered gave these outputs:"
puts ""
puts matches
puts ""
puts "Please specify the name better, as we only allow one person to be deleted at the time. \nReturning you to the main menu."
puts "_____________________________________________"
else
if File.read("Capgemini.txt").include?(keyword) #An if statement that will print the results if the textfile matches the person
puts "_____________________________________________"
puts ("Is this the person you want to delete?")
puts matches
puts "_____________________________________________"
puts "Yes or No?"
print "> "
while user_input = gets.chomp.upcase # loop while getting user input
case user_input
when "YES"
no_matches = txt_file.reject { |name| name[/#{keyword}/] }
File.open('Capgemini.txt','w+'){|out| out.puts no_matches}
puts"User has been deleted. Returning you to the main menu."
puts "_____________________________________________"
break # make sure to break so you don't ask again
when "NO"
puts "User will not be deleted. Returning you to the main menu."
puts "_____________________________________________"
break # and again
else
puts "Please either write 'Yes' or 'No'"
print "> " # print the prompt, so the user knows to re-enter input
end
end
puts "_____________________________________________"
else #If not it will give the user feedback that its not there
puts "_____________________________________________"
puts ("Sorry, we couldnt find #{keyword} in the textfile.")
puts "_____________________________________________"
end
end
end
end
customer = Customer.new
require 'io/console'
select = 0
prompt = "> "
puts
puts
puts "Welcome to Capgemini Sogeti Denmark"
puts "_____________________________________________"
loop do (select != 7)
puts
puts("Press 1 to register a new user.\nPress 2 to search for a employee or keyword within the textfile.\nPress 3 to show all customers.\nPress 4 to delete a customer.\nPress 5 to exit.")
puts "_____________________________________________"
select = STDIN.getch.to_i
if(select == 1)
customer.new_customer
elsif(select == 2)
puts("What customer/keyword do you want to search for?") #You can search for a keyword, like forexample 'Manzur' which will prompt you back with every user names Manzur
print prompt
customer.search_customer(gets.chomp.upcase)
elsif(select == 3)
customer.all_customers
puts "_____________________________________________"
elsif(select == 4)
customer.delete_customer
elsif(select == 5)
puts
puts "The application will now exit."
puts "_____________________________________________"
break
else
puts"Invalid input. Please try again."
puts "_____________________________________________"
end
end
I am not sure how you want the numbers generated, but something like this could work.
This will always give you the highest number from your text file, and add 1 to it.
Keep in mind this method will be slow with a rather large amount of employees.
if File.exist?('Capgemini.txt')
number = File.readlines('Capgemini.txt')
#number = 1
number.each do |x|
customer = x.split(',')
customer_number = customer[1].gsub('Number: ', '').to_i
if customer_number >= #number
#number = customer_number + 1
end
end
else
#number = 1
end
Output:
BOB ROSS, Number: 1, Date of birth: 07/07/2007
WILL SMITH, Number: 2, Date of birth: 08/08/2008
JIM BOB, Number: 3, Date of birth: 09/09/2009
You can also use a similar method for searching through an array:
number = File.readlines('Capgemini.txt')
number.each do |x|
customer = x.split(',')
customer_name = customer[0]
customer_number = customer[1].gsub('Number: ', '').to_i
customer_bday = customer[2].gsub('Date of birth: ', '')
if customer_name == some_variable
puts x
end
end
There is a lot to say about this code (yours and mine). Run Ruby with the -w option to display unused variables, it points to possible mistakes like :
$ ruby -w t_op.rb
t_op.rb:180: warning: mismatched indentations at 'end' with 'class' at 3
t_op.rb:191: warning: possibly useless use of != in void context
t_op.rb:1: warning: assigned but unused variable - file
There are a lot of File.read, I have replaced them by an IO.readlines which creates an array of lines (OK for files which are not Gigabytes big). It also allows to store user numbers.
As always in Ruby, there are many ways to do the same thing.
require 'io/console'
class Customer
attr_reader :file
def initialize(p_file_name)
#file_name = p_file_name
refresh
#next_number = #numbers.max + 1
end
def refresh
#lines = IO.readlines(#file_name) # load information on startup
#numbers = []
#names = #lines.collect do | line |
# the line has the format : <name>, Number: <number>, Date of birth: <birth>
idxn = line.index(', Number')
idxd = line.index(', Date')
#numbers << line[idxn + 10...idxd].to_i
line[0...idxn]
end
end
def new_customer
prompt = "> " # creating a local variable for prompt, since I use it multiple times
puts 'Full name of the person ?'
print prompt
name = gets.chomp.upcase
if #names.include?(name) #so you don't register the same name, twice
then
puts_underlined 'This name is already stored. Returning you to the main menu.'
else
puts 'Date of birth? (DD/MM/YYYY)'
print prompt
birth = gets.chomp # TODO check validity
puts_underlined 'Thanks for the input.'
puts 'Is this information correct ?' # Giving the user all the information back to check for mistakes, etc.
puts_underlined "Name: #{name} Number: #{#next_number} Date of birth: #{birth}"
puts 'Y(es) or N(o)'
print prompt
while user_input = gets.chomp.upcase #loop while getting user input
case user_input[0]
when 'Y'
line = "#{name}, Number: #{#next_number}, Date of birth: #{birth}"
#next_number +=1
#lines << line
File.open(#file_name, 'a') do | file | # open the file for append, ensure it will be closed by the end of the block
file.puts line # puts the information into the textfile, separeted by commas
end
puts
puts_underlined "The information has now been stored in the #{#file_name} file."
break # make sure to break so you don't ask again
when 'N'
puts_underlined 'The information has not been stored. Returning you to the main menu.'
break # and again
else
puts 'Please either write Y(es) or N(o)'
print prompt # print the prompt, so the user knows to re-enter input
end
end
end
end # new_customer
def search_customer(search)
matches = #lines.grep(/#{search}/)
unless matches.empty? # An if statement that will print the results if the textfile matches the keyword
puts_underlined()
puts_underlined "Search results including the word/number/birth #{search} :"
puts matches
puts_underlined()
else # If not it will give the user feedback that it's not there
puts_underlined()
puts_underlined "Sorry, we couldnt find #{search} in the text file."
end
end
def search_customer_number(search)
index = #numbers.index(search.to_i)
if index
then # found, print the user
puts_underlined()
puts_underlined "This is the user number #{search} :"
puts #lines[index]
puts_underlined()
else # not found, it will give the user feedback that it's not there
puts_underlined()
puts_underlined "Sorry, we couldnt find the user #{search}."
end
end
def all_customers
puts #lines
end
def delete_customer
puts 'Which customer do you want to delete ?'
print '> '
keyword = gets.chomp.upcase
matches = #lines.grep(/#{keyword}/)
case matches.size
when 0 # not found, give the user feedback that it's not there
puts_underlined()
puts_underlined "Sorry, we couldnt find #{keyword} in the textfile."
when 1 # print the results if the textfile matches the person
puts_underlined()
puts 'Is this the person you want to delete ?'
puts_underlined matches
puts 'Yes or No?'
print '> '
while user_input = gets.chomp.upcase # loop while getting user input
case user_input
when 'YES'
no_matches = #lines.reject { | line | line[/#{keyword}/] }
File.open(#file_name, 'w+') { | out | out.puts no_matches }
refresh
puts_underlined 'User has been deleted. Returning you to the main menu.'
break # make sure to break so you don't ask again
when 'NO'
puts_underlined 'User will not be deleted. Returning you to the main menu.'
break # and again
else
puts "Please either write 'Yes' or 'No'"
print '> ' # print the prompt, so the user knows to re-enter input
end
end
else
puts_underlined()
puts 'The name you entered gave these outputs:'
puts
puts matches
puts
puts_underlined "Please specify the name better, as we only allow one person to be deleted at the time. \nReturning you to the main menu."
end
end # delete_customer
end # class Customer
def puts_underlined(p_text = nil)
puts p_text if p_text
puts '_____________________________________________'
end
file_name = 'Capgemini.txt'
customer = Customer.new(file_name)
prompt = "> "
puts
puts_underlined 'Welcome to Capgemini Sogeti Denmark'
loop do
puts
puts_underlined "Press 1 to register a new user.\nPress 2 to search for a employee.\nPress 3 to search for a keyword within the textfile.\nPress 4 to show all customers.\nPress 5 to delete a customer.\nPress 6 to exit."
select = STDIN.getch.to_i
case select
when 1
customer.new_customer
when 2
puts 'Which customer number do you want to search for ?'
print prompt
customer.search_customer_number(gets.chomp.upcase)
when 3
puts 'What keyword do you want to search for ?' # You can search for a keyword, like for example 'Manzur' which will prompt you back with every user names Manzur
print prompt
customer.search_customer(gets.chomp.upcase)
when 4
customer.all_customers
puts_underlined()
when 5
customer.delete_customer
when 6
puts
puts_underlined 'The application will now exit.'
break
else
puts_underlined 'Invalid input. Please try again.'
end
end

RUBY: Read text file into 2D array, while also removing newlines?

I'm trying to input text from a file, ignoring the first line, and adding each character to a 2D array at a separate index [[],[]]. So far I can add the characters to their own index but can't remove the newline characters with .chomp or etc.
I want my end result to be
[['*','.','.','.']","['.','.','*','.']","['.','.','.','.']]
So that [0][0] will return * for example, and [0] will return *...
Right now I'm returning
[['*','.','.','.',"\n"]","['.','.','*','.',"\n"]","['.','.','.','.',"\n"]]
The code in question is:
def load_board(file)
board = File.readlines(file)[1..-1].map do |line|
line.split("").map(&:to_s)
end
end
origin_board = load_board('mines.txt')
print origin_board
If I try the following code:
def load_board(file)
board = File.readlines(file)[1..-1].map do |line|
line.split.map(&:to_s)
end
end
origin_board = load_board('mines.txt')
print origin_board
I end up with a 2D array like:
[["*..."],["..*."],["...."]]
Stripping your input should help:
def load_board(file)
board = File.readlines(file)[1..-1].map do |line|
# ⇓⇓⇓⇓⇓
line.strip.split("").map(&:to_s)
end
end
String#strip will remove leading-trailing blanks from a string. The other option is not to use readlines in favour of read and split by $/ (line separator):
def load_board(file)
board = File.read(file).split($/)[1..-1].map do |line|
line.split("").map(&:to_s)
end
end
You can add a .chomp before the .split, like this:
def load_board(file)
board = File.readlines(file)[1..-1].map do |line|
line.chomp.split("").map(&:to_s)
end
end
Haven't exactly tested it but did some fiddling and it should work http://repl.it/hgJ/1.

Ruby inline storage with gets command line support

Newbie to ruby, tried to play around with ruby script with ruby execute file. Like rails want to store an object in ruby and it should display to me.
ruby test.rb
puts "Welcome to BookDetails"
puts "1:Add"
puts "2:Delete"
puts "3:Update"
puts "4:View"
puts "5:Exist"
print "Enter your choice:"
grade = gets.chomp
emp = []
i= 0
case grade
when "1"
emp[i] = Hash.new
puts "Enter empname:"
emp[i][:empname] = STDIN.gets
puts "Enter emp email:"
emp[i][:empemail] = STDIN.gets
puts "Enter emp phno:"
emp[i][:empphno] = STDIN.gets
i= i +1
puts emp
when "2"
puts 'Enter the row to be deleted:'
$delete = STDIN.gets
when "3"
puts 'update the row:'
$update = STDIN.gets
when "4"
puts 'view:'
when "5"
puts 'exist:'
else
puts "You just making it up!"
end
Now when i run ruby test.rb, it executes everything and get the numbers from command line but at last it comes out without showing the added object. It should exit only when am giving 5.
Till user choose 5 it shouldn't exit the interface.User can choose #1 and add the details and choose #4 to view the the details whatever added.
Ruby has a loop method that "repeatedly executes the block":
loop do
print "Enter your choice: "
case gets.to_i
when 1
# ...
when 2
# ...
when 3
# ...
when 4
# ...
when 5
break # <- 'break' exits the loop
end
end
There's a few problems,
there's no loop for accepting additional input... after you enter '1' the program ends.
You could do
request_to_exit = false
until request_to_exit
case grade
---
when '5'
request_to_exit = true
else
...
end
end
Another problem is that when you do
grade = gets.chomp
emp = []
You are wiping out all employees (setting to an empty array) after you accept the grade (what action to perform)
The emp = [] should be before the until request_to_exit starts.
That should help you progress in your project.

How to split a text file into 2 files using number of lines?

I have been facing some issue with file concepts. I have a text file in which I have 1000 lines. I want to split that file into 2 and each of which should contain 500 lines.
For that I wrote the following code, but it splits that by giving certain memory space.
class Hello
def chunker f_in, out_pref, chunksize = 500
File.open(f_in,"r") do |fh_in|
until fh_in.eof?
ch_path = "/my_applications//#{out_pref}_#{"%05d"%(fh_in.pos/chunksize)}.txt"
puts "choose path: "
puts ch_path
File.open(ch_path,"w") do |fh_out|
fh_out << fh_in.read(chunksize)
puts "FH out : "
puts fh_out
end
end
end
end
end
f=Hello.new
f.chunker "/my_applications/hello.txt", "output_prefix"
I am able to split the parent file according to memory size(500kb).
But I want that gets splitted by number of lines. How can I achieve that.
Please help me.
Calculating the middle line pivot, and output according it.
out1 = File.open('output_prefix1', 'w')
out2 = File.open('output_prefix2', 'w')
File.open('/my_applications/hello.txt') do |file|
pivot = file.lines.count / 2
file.rewind
file.lines.each_with_index do |line, index|
if index < pivot
out1.write(line)
else
out2.write(line)
end
end
end
out1.close
out2.close
file = File.readlines('hello.txt')
File.open('first_half.txt', 'w') {|new_file| new_file.puts file[0...500]}
File.open('second_half.txt', 'w') {|new_file| new_file.puts file[500...1000]}

Resources