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
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 6 years ago.
Improve this question
I was wondering if there was a way to reference an object when assigning it to a variable. Here is an example of where this question would apply:
Let's say I wanted to assign the substring of a regex to a variable, call it i.
To assign, I could write
i = /some_regex/.to_s
and then
i = i[3...i.length]
I could also write it all in one line, like
i = /some_regex/.to_s[3.../some_regex/.to_s.length]
However, both of these examples seem somewhat redundant and the second approach could become unwieldy with big regex's or multiple method calls. Is there a way to reference the object being changed without having to rewrite everything?
Edit: Sorry for previous ambiguity.
Ruby evaluates the right side of the equals sign before setting the left side equal to it, so if i already exists you can do what you're talking about. A simple example:
i = 10
i = i + 1 # now i = 11
However, you can't use i to define itself. You could use the following two lines:
i = expression.match(/\d+[\+|-|\*|\/]/)
i = i[0..i.length - 1] # Note: this is the same as i = i[0...i.length]
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
Take an existing list of strings with whole and fractions of inch that includes the inch symbol:
['1"','1/2"','1 1/4"','1/4"','2"','1 1/8"']
Is there a best (rubyish, elegant, use of methods, object oriented) way in ruby to sort so it becomes
['1/4"','1/2"','1"','1 1/8"','1 1/4"','2"']
String#to_r will conveniently ignore trailing garbage (such as "):
The parser ignores leading whitespaces and trailing garbage.
so converting something like '1 1/2"' to a number that will compare sensibly is a simple matter of:
s = '1 1/2"'
r = s.split.map(&:to_r).inject(:+)
Split the string into pieces, convert each to a Rational using String#to_r, and then add them up using Enumerable#inject with a symbol argument. Clean and easy.
Once you have that, sorting is trivial:
array = ['1"','1/2"','1 1/4"','1/4"','2"','1 1/8"']
rationalized = lambda { |s| s.split.map(&:to_r).inject(:+) }
sorted = array.sort_by(&rationalized)
You don't have to use a lambda of course:
array.sort_by { |s| s.split.map(&:to_r).inject(:+) }
but I find that naming your little snippets of logic clarifies things.
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
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}."