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}."
Related
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
The following shows that when I use the to_f method to covert a string to a floating point number and the last decimal point is dropped. How can preserve all decimal points in a given number?
irb(main):002:0> value='1.7.8'
=> "1.7.8"
irb(main):003:0> value.to_f
=> 1.7
Some context:
I am writing the the value to a file and If I write it as a string I get the quotes '1.7.8'. What I am looking for infact is 1.7.8. Hope that makes sense.
EDIT:
I see the error in my question so I'm trying to close it however I can only vote to close it.
just to clarify what I've found is actually contrary to what I said above.
turns out if I write the string '1.7' to a file it is written as '1.7' but with the string '1.7.8' it is written as 1.7.8. I'm just trying to understand why this is occurring.
To write it to a file simply write it like so:
value = "1.7.8"
File.open("file") { |f| f.puts("#{value}") }
The string in the file will not have quotes around it.
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
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"
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 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
Improve this question
So say I have three strings. I am trying to check if the letters in two appear EXACTLY ONCE in "doopdedoo", and if the letters in three appear an unlimited amount of times.
one = "doopdedoo"
two = "dp"
three = "o"
if one.{|a| a.chars.all? {|c| "#{three}".include?(c)}} && one.{|a| a.chars.once? {|c| "#{two}".include?(c)}}
I have used the above to test for the presence of an unlimited amount of o's. How to test for a limited amount of d's and p's?
Edit:
Sorry but I need to clarify. My expected output would be nothing for this case.
[]
Because doopdeedoo contains more than one instance of d or p.
It does contain many o's, so that's fine.
I also added the &&... part to the method above. I realize there is no 'once' method but if there is something like that I'd like to use it.
You can use the String#count method like this:
test_string = "foopaad"
must_appear_once = ['d', 'p']
must_appear = ['o']
must_appear_once.all? {|c| test_string.count(c) == 1} \
and must_appear.all? {|c| test_string.count(c) > 0}
This ensures that 'd' and 'p' each appear exacly once and that 'o' appears in the string (no matter how often).
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