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 9 years ago.
Improve this question
I want to make a regular express to parse out the value (29.0) from this node:
"<currentPrice currencyId="USD">29.0</currentPrice>"
in an xml document. And I would like the value (price) of each and every instance of that node. There is no limit or minimum to what price could be.
Use Nokogiri::XML(myXml).xpath('//currentPrice/text()').map(&:to_s)
Using a regexp to parse XML makes certain infant deities cry.
Here is another way to answer this question, by using Ruby's standard Library REXML
require 'rexml/document'
#doc = REXML::Document.new('<currentPrice currencyId="USD">29.0</currentPrice>')
#doc.get_elements("//currentPrice")[0].text # => "29.0"
Related
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 am inserting some keyword like Don't , has'nt in Sqlite which is not inserted.any one have idea about it.
Use ' as escape character and insert it like this:
dont''t
Documentation:
A single quote within the string can be encoded by putting two single quotes in a row - as in Pascal
In case you are using an API to connect with Sqlite, instead of manipulating the original string a better approach would be to use sqlite3_bind_text() function to bind a value to a ? placeholder in the SQL. Thanks to #Rob for pointing this out.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
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.
Closed 9 years ago.
Improve this question
There is the following regular expression to describe regular expression:
validates :phone, format: { with: /\A(\+7|8)[0-9]{10}\z/ }
'89277777777' must match this expression, '+79277777777' must too. But I have got 'invalid phone' message always. How can I fix it? Thanks.
If all you want to do is a simple way to validate an international number, which may or may not start with a + followed by either a 7 or an 8, followed by 10 more digits, then this regex should do the trick:
\A\+?[78]\d{10}\z
Debuggex Demo
If my assumptions are incorrect, let me know in the comments and we'll work on a better solution.
Note: don't forget to surround the regex with // -- I didn't do that here due to the use of Debuggex
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 9 years ago.
Improve this question
If I create a new array with 10 elements, and populate it with the numbers 1 through 10, then call delete_at(4), the fourth element is "deleted".
How does this work, though? Does it completely remove the element and index and reduce the size of the array to 9, or does it nullify (or make nil) the value of that index and push it the the end of the array?
It copies all of the elements after the position back one with a single memory copy, then reduces the size of the array by one.
Why do you ask? Are you trying to reason about performance?
Ref: https://github.com/ruby/ruby/blob/9f45081627cf682b3ee938353da134d6f28560da/array.c#L2964
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
Given I have:
int1, int2, int3 = 1, nil, 3
how would I subtract these ints from another variable, only if they weren't nil? I can write something sloppy, but I want a single line process if possible.
another_variable - [int1, int2, int3].compact.sum
othervariable - int1.to_i # will turn nil into 0
You can write something like
ans = int1 && (int2-int1)
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 9 years ago.
Improve this question
What is the most efficient way in Ruby to find the longest string from a column X?
Update:
This also sort of works:
def self.length_of_longest_number
Invoice.order("LENGTH(number) DESC").first.number.length
end
Just curious to know if's efficient or not. And if it works in MySQL and Postgres...
With MySql:
# Change your model name and field to your needs
Participant.order("MAX(CHAR_LENGTH(first_name)) desc").limit(1)
# works too:
Participant.order("MAX(CHAR_LENGTH(first_name)) desc").first
# and this is the most efficient to get the field directly:
Participant.limit(1).order("MAX(CHAR_LENGTH(first_name)) desc").pluck(:first_name)
With postgres:
Participants.limit(1).order("MAX(CHAR_LENGTH(name)) desc").group("id").pluck(:name)
This also sort of works:
def self.length_of_longest_number
Invoice.order("LENGTH(number) DESC").first.number.length
end
Just curious to know if's efficient or not. And if it works in MySQL and Postgres...