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.
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 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 want to make a program like,ask a number and print 1 to number by using gets and by using loop. So,I am asking about gets and how to do program which is I given below as program title.
How to ask a number by using gets?If possible explain me with example.
By using gets,I want to print 1 to number. My program titleis Ask a number and print 1 to number by using Ruby.
How can I solve that program?Please help me on this.
As Arup, suggested use Kernel#gets to capture a user input from terminal. The remaining bit can be simply done with a for loop:
num = gets.to_i #Convert the user input to integer
for i in 1..num
puts i
end
You can further modify this to suit your need.
Do as below using Kernel#gets. #gets will give you a string, then to convert the number string to a number use String#to_i.
number = gets.to_i
If I want to make program which from 1 to number then what should I do?
Use a Range then.
(1..number).each do |n|
# code
end
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 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"]