String Trimming Ruby [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 9 years ago.
Improve this question
I have the following string "survey_questions_attributes_1392746726560_question_title" I need to trim all the strings starting from the first character till the first number. How to do that?

String#slice method supports regex,so you can do:
[67] pry(main)> "survey_questions_attributes_1392746726560_question_title"[/\d.*/]
=> "1392746726560_question_title"

"survey_questions_attributes_1392746726560_question_title".sub /\D*/,''

Behold the power of Rubular and bask in it's glory.
string = "survey_questions_attributes_223233333_question_title"
puts string.sub( string.scan(/survey_questions_attributes_(.*)/)[0][0] , " ")

I'd do using String#[]
s = "survey_questions_attributes_1392746726560_question_title"
s[/[a-z_]+(?:\d)/] = ""
s # => "1392746726560_question_title"

"survey_questions_attributes_1392746726560_question_title".split(/\D+/, 2).last
# => "1392746726560_question_title"

Related

How do I add a string at the end of another string if there is not match using sub [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 want to add string1 to the end of an string2 if string2 does not include string1.
Can I do this using sub?
I tried a few combinations but they are not doing anything. I'm not even sure I'm doing it right. I'm very bad at Regex and would really appreciate some help.
Generally you'd use this pattern:
string = "test"
insert = "er"
string << insert unless (string.match(insert))
# => "tester"
Why do you need a sub? you can just do something like:
a = "abcd"
b = "bc"
c = a + b unless a.include?(b)

< > inside a Ruby String [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I was reading something else posted by a user on here explaining how yield statements work in Ruby. Part of the code he was using was :
print_list( [1,2,3], 23 ) { |n| "<#{n}>"}
what do the < > mean inside the string? It's such a simple question but I haven't been able to find out the answer to it.
In a string literal neither < nor > have any implied meaning - although such might have meaning in the output or use of the resulting string.
Only escape sequences and # (in interpolated literals) have intrinsic meaning.
These characters are just a part of string.
And any character which lies inside #{ } will be evaluated, which is also referred to Interpolation

Swap two strings 2 by 2 in ruby [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I've go a string made in this way.
"AABBCCDD....." grouped by 4 with variable lenght.
I need a method that swap that 2 by two the chars in this string
def swap2_by_2( string )
???
end
If the input is AABBCCDD the output will be BBAADDCC
Thanks, i'm very noob in ruby.
Edit: my mistake, a more comprhensive example may be.. Input: ABCDEFGH -> CDABGHEF
It is not clear what the OP is trying to do, but if it is to flip the first and the second characters with the third and fourth characters for every four characters, then the example that the OP showed is highly misleading and inappropriate (It should have been "ABCD..." instead of "AABB..."). In that case, a solution would be:
string.gsub(/(..)(..)/, '\2\1')
Thinking about your question, an interpreting the "ABCDEF", I am sure, that you are looking for pack / unpack in Ruby: I found a good page here How to change bit order in Ruby
And here are two a non-regexp versions:
p 'AABBCCDD'.chars
.each_slice(2)
.each_slice(2)
.map(&:reverse)
.join
#=> "BBAADDCC"
# or
'AABBCCDD'.chars
.each_slice(4)
.map{|x| x.rotate(2)}
.join

Ruby: substitute all references in part of a string [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
I want to substitute all references of . to _ in parts of a given string inside a %{}. See this example:
'example 1.1 %{a.b.c} of {d.e.f}.'
should be replaced to
'example 1.1 %{a_b_c} of {d_e_f}.'
I have to do this because on older ruby example %{a.b.c}' % {:'a.b.c' => 'result'} doesn't work.
As #sawa suggested, a little tweak:
'example 1.1 %{a.b.c} of {d.e.f}.'.gsub(/{.+?}/) { |s| s.tr '.', '_' }
=> "example 1.1 %{a_b_c} of {d_e_f}."
Use gsub with a block:
data = 'example 1.1 %{a.b.c} of {d.e.f}.'
p data.gsub(/{.+?}/){|x| x.gsub('.','_')} #=> "example 1.1 %{a_b_c} of {d_e_f}."

Extract text between V="..." Ruby [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
Ok instead of printing the whole line:
<von_icd_code V="A00"/>
I only would like to extract the text between V="..", in this case A00
Using Nokogiri::XML::Document
require 'nokogiri'
doc = Nokogiri::XML::Document.parse('<von_icd_code V="A00"/>')
doc.at("von_icd_code")["V"] # => "A00"
scan is the wrong method if you are interested only in a single occurrence. There must also, in general, be a check that the substring was found at all.
The code should look like this
s = '<von_icd_code V="A00"/>'
if s =~ /V="([^"]*)"/
puts $~[1]
end
output
A00
Like this:
'<von_icd_code V="A00"/>'.scan(/V="(.+)"/)[0][0]
=> "A00"

Resources