how to remove '\' from string [closed] - ruby

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{\"}, '')

Related

Laravel How to filter inputs before going to the DataBase? [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 5 months ago.
Improve this question
Laravel How to filter inputs before going to the DataBase
what shoud i do to filter my inputs before saving to the database? i want it to be uniformed in like this format "Name" a uper case followed by lowercase.
in some cases like when the user register a full caps name i want it to be re format as the example "Name"
there's an especific PHP function to do what you need: ucfirst(). This function turns to Uppercase the first char from a string.
For example:
//If $request->name is 'JoHn' or whatever
$filtered_name = ucfirst($request->name);
//Returns 'John'
If is a case with two names, you can use the ucwords() PHP function (Turn first letter uppercase of every word from a string)
//If $request->name is 'OlivER JAMES'
$filtered_name = ucwords($request->name);
//Returns 'Oliver James'
Hope this help you.

How to remove numbers from a 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 8 years ago.
Improve this question
I want the following:
"Set off to London 29min"
to become:
"Set off to London min"
I also want to remove the "min" and space as well, but I know how to do it in a very inefficient way.
This will do:
string.tr("0-9", "")
If I understand you correctly then here are some solutions.
string = "Set off to London 29min"
string.gsub!(/\d+/,"")
#=> "Set off to London min"
or if you would also like the literal word 'min' taken out as well
string = "Set off to London 29min"
string.gsub!(/(\d+|(min))/,"")
#=> "Set off to London "

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

Ruby Hash that's been Mashed needs to be First for Solidfist Rockgroin. What? [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 8 years ago.
Improve this question
I was never one to question peoples lifestyle choices. Especially when he walked in the door. His handshake was like iron, and his request was simple enough after the ringing in my ears subsided. "Listen user" he barked, "I need to be converted from XML to a pseudo-object and I need it done YESTERDAY!" And with that, he swept out of my office like a tiny muscle tornado on the heels of a sonic boom.
So I got to work. First up:
<employeeRecord>
<employee>
<firstName>Crunch</firstName>
<secondName>Crumble</secondName>
<thirdName>Destructo</thirdName>
<fourthName>Solidfist</fourthName>
<lastName>Rockgroin</lastName>
</employee>
<dependent>
<spouse type = "spouse">
<firstName>Betty</firstName>
</spouse>
<child>
<firstName>Timmy</firstName>
</child>
</dependent>
</employeeRecord>
Simple enough. After researching Ruby I came across a tiny gem called Hashie, and with it, I do the following:
class Person
def initialize
#xml = Mash.new(XmlToHash.getHash)
end
attr_accessor :xml
end
Wonderful! I simply need to reference my client now and as a simple test, pull up his last name. With the wonderful Mash, I can do this very easily!
MyClient = Person.new
MyClient.xml.employee.first.lastName # => 'Rockgroin'
Wait, hang on a second. What's first doing there? What if I got rid of it?
MyClient.xml.employee.lastName # => END OF THE WORLD. ABANDON YOUR IDE.
What? But...why? The content of the hash at .employee.first looks like this after getting Mashed:
#<Hashie::Mash firstName=["Crunch"] fourthName=["Solidfist"] lastName=["Rockgroin"] secondName=["Crumble"] thirdName=["Destructo"]>
And if there's a first, then what is last?
#<Hashie::Mash firstName=["Crunch"] fourthName=["Solidfist"] lastName=["Rockgroin"] secondName=["Crumble"] thirdName=["Destructo"]>
The same blasted thing?! What's going on here? This code is almost as mysterious as my client. What's happening? Can some kind soul explain what in the devil is going on here, and what first/last are doing?

How to set a predefined input [closed]

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]:

Resources