Subtract Variable if Present in Ruby [closed] - ruby-on-rails

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)

Related

How to insert charactor like "don't" in Sqlite in ios [closed]

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.

Regular expression in ruby to find value of xml node [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 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"

How does delete_at(n) work 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 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

Is there any way i can enclose a text in latex [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 9 years ago.
Improve this question
first of all i'm new in latex
I wanted to ask if there is a way where i can enlose a text like a code like here in stack overflow thank you very much.
Ive searched here but i dont really know what to seek or how to ask something like this.
I don't know what do you mean by code like text. But you can use \verbatim to display some code like the following if this is what you wanted:
\begin{verbatim}
for i in range(1, 20):
print i
else:
print "the loop is done"
\end{verbatim}

How to find the longest string in a column with Ruby on Rails? [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 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...

Resources