How to set a predefined input [closed] - ruby

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I want a simple pre-defined input in ruby. What I mean is that I want something to be there by default so the user can edit or just simply press Enter to skip. I'm using STDIN.gets.chomp.
not predifiend : "Please enter a title: "
predefined : "Please enter a title: Inception " // "Inception" is pre-defined input]

The following is a sub-optimal solution as the default answer is not cleared instantly as the user begins to type:
prompt = 'Please enter a title: '
default_answer = 'Inception'
# Print the whole line and go back to line start
print "#{prompt}#{default_answer}\r"
# Print only the prompt so that the cursor stands behing the prompt again
print prompt
# Fetch the raw input
input = gets
# If user just hits enter only the linebreak is put in
if input == "\n"
answer = default_answer
else # Otherwise the typed string including a linebreak is put in
answer = input.chomp
end
puts answer.inspect
If you want such a thing I guess you have to use more advanced terminal features. I guess ncurses could do the job.
Another option would be to just display the default answer in brackets and simply put the prompt behind that. A lot of simple command line tools do such thing. This could look like this:
Please enter a title [Inception]:

Related

Making a Console Application ask something and receive input

I have Programming in Basic in High School and was tasked with a project in which I was to make a program that says hello to the teacher as well as give a paragraph describing why programming is valuable in today's time.
My initial idea was to make a program that greets and asks the teacher a question on whether they want to know why programming is valuable. When the console asks the question there will be two options: Yes or No. Each having their own text output.
REM Period 6 Class Project for (My Name)
Dim input = Console.ReadLine
Dim Yes = input
Dim No = input
Console.WriteLine("Hello, would you like to get an explanation as to why programming is valuable?")
input = Console.ReadLine
If input = Yes Then
Console.WriteLine("Well the reason why...")
End If
Where I'm having trouble is actually making the text appear on the application as well as receiving input on whether the code is correct as well.
See if .. then .. else
input=input.ToLower().Trim()
If input="yes" then
...
ElseIf input="no" then
...
Else
..' unexpected answer
End if

how to remove '\' from string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am new to ruby.I have a string like
string = "You have successfully placed Service Request No. \#{#service_requests.id} for \#{#service_requests.category.name} . Our representative will be in touch with you soon for the same. Thank you."
and I want to remove "\" from whole string.
output_string = "You have successfully placed Service Request No. #{#service_requests.id} for #{#service_requests.category.name} . Our representative will be in touch with you soon for the same. Thank you."
How to make it possible.
>> string = "You have successfully placed Service Request No. \#{#service_requests.id} for \#{#service_requests.category.name} . Our representative will be in touch with you soon for
the same. Thank you."
>> puts(string)
=> You have successfully placed Service Request No. #{#service_requests.id} for #{#service_requests.category.name} . Our representative will be in touch with you soon for the same. Thank you.
>> puts(string.inspect)
=> "You have successfully placed Service Request No. \#{#service_requests.id} for \#{#service_requests.category.name} . Our representative will be in touch with you soon for the same. Thank you."
Make sure you know what the difference between string representation (puts string.inspect) and string content (puts string) is, and note the backslashes as the artifacts of the representation.
Try this:
string.gsub(/\\/, '') # or string.gsub!(/\\/, '') for inplace substitution
You can remove this like:
string.gsub(%r{\"}, '')

how to add list of strings to a text file in ruby [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have list of strings. I am trying to append those string values to a text file.
Here is my code:
java_location = "#{second}#{first}"
The output of java_location is:
1.6.0_43/opt/oracle/agent12c/core/12.1.0.4.0/jdk/bin/java
1.6.0_43/opt/oracle/agent12c/core/12.1.0.4.0/jdk/jre/bin/java
1.5.0/opt/itm/v6.2.2/JRE/lx8266/bin/java
1.6.0_35/u01/app/oracle/product/Middleware/Oracle_BI1/jdk/jre/bin/java
I want this output writing into a text file.
How can i do that?
File.write('file.txt', java_location)
You want to open the file in append mode ('a') rather than readwrite ('w+') which truncates the existing file to zero length before writing
http://alvinalexander.com/blog/post/ruby/example-how-append-text-to-file-ruby
if first && second
java_location = "#{second}#{first}"
a << java_location
File.open("/home/weblogic/javafoundmodified.txt", 'a') do |file|
a.each {
|item|
file.puts item
}
end
end

Gets stops processing script if it encounters a apostrophe

I'm futzing with a lesson over on Codecademy, pretty basic but the lesson prompts the user for a string and then prompts them for words to hide from the string. I've already finished with the lesson but I wanted to try messing with it outside of the lesson.
What I'm finding is the following script will run to completion in the lesson's scripting and interpreter area but if I try to run the same script over at say labs.codecademy.com or repl.it
I'll get prompted for the first question and if I enter a string containing an apostrophe it won't go on to the next prompt. I'm not getting an error, but eventually I'll get a "This program is taking too long to finish" pop up.
Here's the script:
puts "Tell me something."
text = gets.chomp
puts "What would you like me to forget?"
redact = gets.chomp
words = text.split(" ")
words.each { |text|
if redact.include? text
print "REDACTED "
else
print text + " "
end
}
So for example if you were to enter I really like blueberry pie that passes, but if you were to enter They've told me I should try the blueberry pie the program gets hung up.
I'm a complete novice when it comes to Ruby, but is there a way to have gets process that sort of punctuation? Or is that not the right way to go about it? Or is it just the environment I'm trying to run this program in?
IO.gets is a system-level call: http://apidock.com/ruby/IO/gets
it well read from STDIN (standard input) which is the text that you type in a terminal window: http://en.wikipedia.org/wiki/Standard_streams
browsers per-se don't have this and it might depend on the implementation of the web-based console about how they handle calls like that. usually, there are known limitations (like this one).
as #BroiStatse pointed out, it might as well be a bug in in their browser-based implementation.

Best way to split text into sentences avoiding acronyms clashes [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Given the following phrase
Ms. Mary got to know her husband Mr. Dave in her trip to U.S.A. and it
was cool. Did you know Dave worked for Microsoft? Well he did. He was even part of Internet Explorer devs.
What is the best "pseudo-code" way to split it into sentences? Python or any other similar language is also fine because of its pseudo-code resemblance.
What I've thought is to replace every occurrence of " a-zA-Z." (notice the space), ".a-zA-Z" and ".a-zA-Z." to its equivalent without the dot of course, so for example
" a."
" b."
" c."
" d."
" e."
" f."
...
and
".a."
".b."
".c."
".d."
".e."
".f."
...
and
" ab."
" ac."
" ad."
...
" ba."
" bc."
" bd."
...
The phrase should be nicely converted to the following
Ms Mary got to know her husband Mr Dave in her trip to USA and it
was cool. Did you know Dave worked for Microsoft? Well he did. He was even part of Internet Explorer devs.
...or am I wrong somewhere and I have a flawed logic?
For the future what's your question comments, I need to know what's the best way to split the example text into correct sentences avoiding clashes with acronyms.
This either explained in pseudo-code, Python or other languages similar to pseudo-code. I want it to be language agnostic so it can be implemented by anyone, regardless of the language they use.
All acronyms in the example are of the pattern Uppercase . or Uppercase lowercase .; none of the other -- regular -- occurrences of the full stop match this particular pattern.
So a simple RegEx can be used to remove the full stops. What's left after that can be split on the regular punctuation marks .!?. In Javascript:
str2 = str.replace(/([A-Z][a-z]?)\./g, '$1');
or using a GREP flavor that does understand most common character classes:
str2 = str.replace(/(\u\l?)\./g, '$1');
This results directly in the output as shown.
Using a RegEx is straightforward (and easily expanded!), but the same pattern can be tested in other languages as well. In C, you can copy input to output and test only when seeing the . character:
int main (void)
{
char input[] = "Ms. Mary got to know her husband Mr. Dave in her trip to "
"U.S.A. and it was cool. Did you know Dave worked for Microsoft? Well "
"he did. He was even part of Internet Explorer devs.";
char output[256], *readptr, *writeptr;
printf ("in: %s\n", input);
readptr = input;
writeptr = output;
while (*readptr)
{
if (*readptr == '.')
{
if ((readptr > input && isupper(readptr[-1])) ||
(readptr > input+1 && isupper(readptr[-2]) && islower(readptr[-1])))
{
readptr++;
continue;
}
}
*writeptr = *readptr;
readptr++;
writeptr++;
}
*writeptr = 0;
printf ("out: %s\n", output);
return 0;
}
These solutions remove full stops from the source text. If you want to keep them, you can replace them with a placeholder (for example, a character that does not normally occur in the source text), or do the reverse: when splitting on sentences, test to see whether or not a full stop is a valid breaking point.
Afterthought: it does work on the original sample sentence... but it does not on the one in the comments:
I made a trip to the U.S.A. It was cool.I liked it very much.
where you get the output
I made a trip to the USA It was cool.I liked it very much.
This requires checking for more possible scenarios:
common abbreviations, such as Ms. and Mr.: \u\l\.
in-sentence acronyms; "U.S.A." followed by a lowercase: (\u\.)+ (?=\l), where the full stop needs removing;
end-of-sentence acronyms; "U.S.A." followed by an uppercase: (\u\.)+ (?=\u), where the last full stop should remain.

Resources