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 capture the text value and store it in a variable. Now I want to use the specific characters from the variable. How can I do it in Ruby?
var = "ABC-DEF-XYZ"
I want to use "DEF" value from the variable and want to skip the rest.
You can capture the 'y' value from your string using one of these 2 methods:
Regex
y = input_value.gsub(/^\w\w\w-(\w\w\w)-\w\w\w$/, "\\1")
Split
y = input_value.split('-')[1]
Once you have the 'y' value from your input, you can compare it to the table list.
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 2 years ago.
Improve this question
I want to get two value in one line without split function. Help me to solving this problem. Thank you!
If you want to assign 2 variables in one line you can do
def multiple_values
return 1, 2
end
x, y = multiple_values
# x = 1
# y = 2
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 have an array within an array. I can call a certain object by doing this...
array[1][2]
I would like to be able to insert a variable instead of one of these values. Maybe something like...
array[#{variable1}][#{variable2}]
Is this possible?
Just replace the numeric literal with the variable:
variable1 = 1
variable2 = 2
array[variable1][variable2]
What you mentioned is known as string interpolation, but can only be used in quotes.
For example, say you have a variable defined, price
price = 80
You can say something like this
puts "The price is $#{price}"
This will translate to The price is $80
It's not possible to do that in arrays, but it is possible to substitute values inside
a = 7
b = 9
array[a] will get the 8th value of the array, whereas array[b] will get the 10th value of the array.
Hope this helps.
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 9 years ago.
Improve this question
I have a Ruby narray automatically generated, which has this form: [x,y], where x and y are integers.
I want to transform [x,y] into this type of string:
"p\.x\.y"
I haven't succeeded in transforming it using a regular expression.
Regex is used to match patterns, not to generate strings.
To accomplish what you want, just use the splat operator with sprintf:
array = [1,2]
puts sprintf("p\\.%d\\.%d", *array)
This will output "p\.1\.2"
Alternative way:
x = [1,2]
puts "p\\.#{x[0]}\\.#{x[1]}" #=> p\.1\2
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 9 years ago.
Improve this question
I am trying to find a regex that does the following. Let's say I have a string in this form
wordcount = "THE:12 IT:3 TO:3".
which is a word and its frequency. I need a regex that can find for example THe, followed by :, followed by a number.
If you want all matches use the scan method:
mystring.scan(/\w+:\d+/)
Bonus if you are planning to make a hash:
Hash[mystring.scan(/(\w+):(\d+)/)]
# or, if you prefer to not use regexp:
Hash[x.split.map{|y| y.split(':')}]
You can do as below :
s = "THE:12 IT:3 TO:3"
p s.scan(/\w+:\d+/)
# >> ["THE:12", "IT:3", "TO:3"]
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 need a Ruby program that, given a file as parameter, returns a hash or array that gives the number of characters for each line.
How can I do this elegantly in Ruby ?
File.open('file_name').map(&:length)
Check this:
File.open('file_name').inject([]) do |counts, line|
counts << line.size
end
Take a note that it will also count new line characters.
For file with content
aa
aaaa
a
the result will be
[3, 5, 1]
If you don't want to count them, check this method String#chomp