This question already has answers here:
Ruby: How to iterate over a range, but in set increments?
(4 answers)
Closed 8 years ago.
I would like to get a range of numbers but only by 5. Is there method for this? Perhaps something like: (0..100).by(5) (I feel like I've seen this done somewhere...)
I know I can do this: (0..100).select{|x| x if x % 5 == 0} Can you suggest alternatives?
Look at the method Numeric#step. I am sure it is what, you want.
0.step(25,5).to_a
# => [0, 5, 10, 15, 20, 25]
You want #step .
(0..100).step(5)....
Related
This question already has answers here:
How to properly format currency on ios
(8 answers)
How to input currency format on a text field (from right to left) using Swift?
(9 answers)
Closed 4 years ago.
I have a values like that: 100, 1220, 10015 basically last 2 digits are cents and I need to convert to dollar (currency) format similar to:
1.00, 12.20, 100.15
Can somebody suggest a quick implementation?
var a = 1011
var b = Double(a) / 100
This question already has answers here:
A way to round Floats down
(11 answers)
Closed 6 years ago.
I have number num = 24.89808 and want to round it to 24.89
How can i do it?
i tried num.round(2) but it gives me 24.9 also number_to_currency=>24.90
If you are talking about rounding, 24.9 is by all means the correct result. Whether you are interested in ceiling it, here you go:
(24.89808 * 100).floor / 100.0
#⇒ "24.89"
First convert that into a decimal and then round it two two places,
Try this command,
num.to_d.round(2, :truncate).to_f
2.2.4 :040 > num = 24.89808
=> 24.89808
2.2.4 :041 > num.to_d.round(2,:truncate).to_f
=> 24.89
This question already has answers here:
Concatenation of tables in Lua
(13 answers)
Closed 7 years ago.
Is there an easy way to concatenate two tables which are sequences? For example
a = {1, 2, 3}
b = {5, 6, 7}
c = cat(a,b)
where c would be the table {1,2,3,5,6,7}?
function cat(t, ...)
local new = {unpack(t)}
for i,v in ipairs({...}) do
for ii,vv in ipairs(v) do
new[#new+1] = vv
end
end
return new
end
It uses iteration to add the elements of each array to a new one.
It's also worth noting that {unpack(t)} will only work if you have less than a specific number of elements, due to how tuples work in Lua. This varies across versions and depending on what you're doing, but if it's small you probably have nothing to worry about.
This question already has answers here:
What does ||= (or-equals) mean in Ruby?
(23 answers)
Closed 8 years ago.
I've been recently allocated to a new Rails project in which I could see assignments to variables are made using ||= instead of =. Can any one help me understand if this is a correct way or a good practice in Rails and the advantages/disadvantages of using it?
e.g.
a ||= b + c
(b and c are integers)
Thanks for any help :)
With:
a = b + c
a gets set to the sum of b and c no matter what.
With:
a ||= b + c
it only gets set to the sum if it's currently set to nil or false.
It's a subtle difference but one that Ruby bods should learn. People coming from C-like languages often see a ||= x as:
a = a || x
but that's not actually the case. Instead, it's:
a || a = x
(no assignment is actually done if a is already set to a non-nil/false value).
Ruby Inside goes into some more depth on the matter here.
This question already has answers here:
What does ||= (or-equals) mean in Ruby?
(23 answers)
Closed 8 years ago.
In my readings about structuring methods with options hashes for ruby, I've run into a coding "motif" a few times that I can't explain. Since I don't know what it's called, I'm having a lot of difficulty looking it up to learn more about it.
Here's an example:
1 def example_method(input1, options={})
2
3 default_options = {
4 :otherwise => "blue",
5 :be_nil => nil
6 }
7
8 options[:be_nil] ||= options[:otherwise]
9
10 # other code goes down here
11 end
So above, on line 8, you can see what I'm talking about. From what I can put together, the line of code acts similarly to a tertiary operator. Under one condition, it sets a variable to one value... under a different condition, it sets the variable to a different value. In this case, however, the code updates a hash that's stored in the "options" variable. Is that a correct assumption? Furthermore, what is this style/operator/functionality called?
THis is a conditional assignment. The variable on the left will be assigned a value if it is nil or false. This is the short for of saying:
unless options[:be_nil]
options[:be_nil] = options[:otherwise]
end