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]
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 1 year ago.
Improve this question
In this challenge you must implement the method code that receives a string with a mathematical calculation and returns the value that ? must have for the account result to be correct.
'100 + ? + 3 = 108'
How to find ? value in ruby.
can someone explain how to solve?
Computers are really dumb and need to be told what to do.
Try solving the problem yourself by hand and when it's very clear for you how to solve it, try to implement it.
I'll give you a start
Validate if the input is empty or nil
Split the input into its parts '100 + ? + 3 = 108' => ["100", "+", "?", "+", "3", "=", "108"] (you can put them in an array)
Check if each part is a number, an operator or a ?
If it's a number, transform it from string to number
if It's an operator check which one, +, -, etc. and then act on it taking left and right side of the operator
etc. etc
You'll notice it's not trivial, but eventually each one of those paragraphs can be translated into specific ruby code.
Give it a try and ask again when you get stuck.
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 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 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
When attempting to use iteration methods, I keep getting errors. This is an initialize method that creates a randomly sized array populated with random integers. Any help is appreciated.
def initialize
i = 0
#random_size = rand(3-12)
#new_arr = Array.new(#random_size)
loop do
#new_arr[i] = rand(1..50)
break if i >= #random_size
i += 1
end
end
Edit
The original question looked as below. Notice the rand(3-12).
In Ruby (and any language that has some functional capabilities, for that matter) you don't usually write explicit indexes, that's too imperative (and verbose). A functional approach would look something like this:
def initialize
#random_size = rand(3..12)
#new_arr = #random_size.times.map { rand(1..50) }
end