Printing 0 to 50 inclusive? - ruby-on-rails

Why is this code not printing 0 to 50 inclusive?
i = 0
until i <= 50 do
print i
i += 1
end

Either use
until i > 50 do
# ...
end
or
while i <= 50 do
# ...
end
Here's a more "Ruby like" example:
(0..50).each do |i|
puts i
end

Ugh.
i = 0
until i <= 50 do
print i
i += 1
end
That would generate 51 iterations because you're starting at 0 and trying to run until 50 is reached, except that until is "notting" your condition. If you want to loop perhaps while would be a better test:
i = 0
while i <= 50 do
print i
i += 1
end
>> 01234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950nil
But, even with while there are still 51 values being output:
i = 0
output = []
while i <= 50 do
output << i
i += 1
end
output.size # => 51
If you want to loop 50 times, why not use:
50.times do |i|
puts i
end
Or:
50.times { |i| puts i }

Change until to while. until is basically the same thing as while, but the conditional is inverted.

Another iterative method to use would be upto:
0.upto(50) do |i|
puts i
end
I really love this method for quick number iterations. It's super idiomatic (it does what it says) and it's inclusive of both start and end values so you don't have to calculate/account for an exclusive end val.

until stops executing when the condition it has is true. Because it is true from the beginning, nothing happens.

Just swap the comparison operators.
i = 0
until i > 50 do
print i
i += 1
end
You could also do
i = 0
while i <= 50 do
print i
i += 1
end

Use Idiomatic Ruby
Other answers have addressed why your original code doesn't work, and have pointed out the logic error in your conditional. However, it's worth noting that a more idiomatic way to do what you're doing would avoid the conditional altogether. For example:
(1..50).each { |i| pp i }

This works. ;)
i = 1
while i < 51 do
print i
i += 1
end

the first thing you have the comparison sing backwards
you want to do something like:
i = 0
until i >= 50 do
print i
i += 1
end
you can take a look at http://ruby-doc.org/core-2.1.2/doc/syntax/control_expressions_rdoc.html#label-until+Loop for more info

Related

Using loop to solve if number < than 200 previous numbers, return 1

I'm writing a small program in lua and I was hoping to get some pointers on what would be the most correct way to approach this. Basically, if my number is bigger than the last 200 numbers, return 1, else return 0.
I figure the best way would be a loop?
Let's say x is the position of my number in a table, and then I want to check that it's bigger than the previous 200 numbers and return 1 at the end if it is and 0 if it's not.
while (x > x-a)
do
isbigger = 1;
a = a+1;
return isbigger;
end
I'm not sure if that's correct syntax wise, it also would technically return 1 everytime it goes through the loop and I just want it at the end if true that my number is bigger than the 200 previous one. Also, how do get out of the loop if its false (I'd guess with a break)?
Thanks for any help and have a nice day!
If you are looping over an array of values, you should use a for loop. you also do not want to return isbigger from inside the loop as it will prematurely end the loop
local last = 10
local myNumber = 123
local numbers = {}
--Setup our example numbers table
math.randomseed(os.clock())
for i = 1, 40 do
numbers[i] = math.random(1,200)
end
--Run comparison on our the last x values
local isBigger = true
for i = #numbers, #numbers - last, -1 do
print(myNumber, numbers[i], myNumber > numbers[i])
isBigger= isBigger and myNumber > numbers[i]
end
print("isBigger is: " .. isBigger)
return isBigger and 1 or 0
Example Output
123 181 false
123 6 true
123 77 true
123 78 true
123 145 false
123 130 false
123 104 true
123 114 true
123 6 true
123 4 true
123 15 true
isBigger is: false
The for loop above is better for understanding what is happening, but this one is better for use as it will exit once it has found an result that shows myNumber is not bigger.
local isBigger = true
for i = #numbers, #numbers - last, -1 do
print(myNumber, numbers[i], myNumber > numbers[i])
if not (myNumber > numbers[i]) then
isBigger = false
break
end
end

How do I convert this string to milliseconds?

I’m using Rails 4.2.7. I want to calculate the number of milliseconds given a duration, which could include hours, minutes, and seconds. So I wrote these two functions:
def convert_to_hrs(string)
if !string.nil?
string.strip!
case string.count(':')
when 0
'00:00:' + string.rjust(2, '0')
when 1
'00:' + string
else
string
end
else
"00:00:00"
end
end
def duration_in_milliseconds(input)
input = convert_to_hrs(input)
if input.match(/\d+:\d\d:\d\d\.?\d*/)
h, m, s = input.split(':').map(&:to_i)
(h.hours + m.minutes + s.seconds) * 1000
else
0
end
end
Unfortunately, when I call duration_in_milliseconds(input) with a number like 8:49, the result is zero. The result should be interpreted as 8 minutes and 49 seconds, which in milliseconds would be 529000. How do I adjust the above to account for this?
I'll just leave this one here:
def convert_to_ms(string)
string.split(':').map(&:to_i).inject(0) { |a, b| a * 60 + b } * 1000
end
convert_to_ms('8:49')
#=> 529000
Just another implementation.. Cause it's fun :)
def duration_in_milliseconds(string)
string.split(':')
.map(&:to_i)
.reverse
.zip([1,60,3600])
.map{ |segment, multiplier| segment*multiplier }
.inject(:+) * 1000
end

This seems to just be filling up an array with the greatest number, why?

def third_greatest(nums)
idx = 0
arr = []
i = 1
largest = 0
while idx < nums.length
while i < nums.length
if nums[idx] > nums [i]
largest = nums[idx]
else
largest = nums[idx]
end
i += 1
end
arr.push(largest)
idx += 1
i += idx
end
return arr[2]
end
puts(third_greatest([4, 3, 2, 1]) == 2)
#should equal true
I'm trying to get the third largest number out of the array but I keep getting four for any value of the array that returns data.
Any help would be great!
Here is an easier solution for finding the third greatest number in an array:
def third_greatest(nums)
nums.sort!
nums[-3]
end
third_greatest([4, 3, 2, 1])
=> 2
puts(third_greatest([4, 3, 2, 1]) == 2)
=> true
at the end of the first time to the loop, i will be nums.length.
afterwards you increase i with idx so it is now bigger than nums.length.
In the next loops you will never enter the inner loop again, so the largest is never updated anymore.
That's why you always get largest in the result.
to fix it do something like :
end
arr.push(largest)
idx += 1
i = idx + 1
end
so that i is reset to one higher than idx.
But the real solution is to leverage the rich standard library as Alex suggests.
Recent versions of Enumerable#max have allowed a parameter:
(0..9).to_a.max(3).last #=> 7
max(n) returns the three largest values, in decreasing magnitude. This could be expected to be more efficient than sort (unless n == arr.size, of course). Related Enumerable methods (max_by, min, min_by) also have this functionality.

Using Ruby convert numbers to words?

How to convert numbers to words in ruby?
I know there is a gem somewhere. Trying to implement it without a gem. I just need the numbers to words in English for integers. Found this but it is very messy. If you have any idea on how to implement a cleaner easier to read solution please share.
http://raveendran.wordpress.com/2009/05/29/ruby-convert-number-to-english-word/
Here is what I have been working on. But having some problem implementing the scales. The code is still a mess. I hope to make it more readable when it functions properly.
class Numberswords
def in_words(n)
words_hash = {0=>"zero",1=>"one",2=>"two",3=>"three",4=>"four",5=>"five",6=>"six",7=>"seven",8=>"eight",9=>"nine",
10=>"ten",11=>"eleven",12=>"twelve",13=>"thirteen",14=>"fourteen",15=>"fifteen",16=>"sixteen",
17=>"seventeen", 18=>"eighteen",19=>"nineteen",
20=>"twenty",30=>"thirty",40=>"forty",50=>"fifty",60=>"sixty",70=>"seventy",80=>"eighty",90=>"ninety"}
scale = [000=>"",1000=>"thousand",1000000=>" million",1000000000=>" billion",1000000000000=>" trillion", 1000000000000000=>" quadrillion"]
if words_hash.has_key?(n)
words_hash[n]
#still working on this middle part. Anything above 999 will not work
elsif n>= 1000
print n.to_s.scan(/.{1,3}/) do |number|
print number
end
#print value = n.to_s.reverse.scan(/.{1,3}/).inject([]) { |first_part,second_part| first_part << (second_part == "000" ? "" : second_part.reverse.to_i.in_words) }
#(value.each_with_index.map { |first_part,second_part| first_part == "" ? "" : first_part + scale[second_part] }-[""]).reverse.join(" ")
elsif n <= 99
return [words_hash[n - n%10],words_hash[n%10]].join(" ")
else
words_hash.merge!({ 100=>"hundred" })
([(n%100 < 20 ? n%100 : n.to_s[2].to_i), n.to_s[1].to_i*10, 100, n.to_s[0].to_i]-[0]-[10])
.reverse.map { |num| words_hash[num] }.join(" ")
end
end
end
#test code
test = Numberswords.new
print test.in_words(200)
My take on this
def in_words(int)
numbers_to_name = {
1000000 => "million",
1000 => "thousand",
100 => "hundred",
90 => "ninety",
80 => "eighty",
70 => "seventy",
60 => "sixty",
50 => "fifty",
40 => "forty",
30 => "thirty",
20 => "twenty",
19=>"nineteen",
18=>"eighteen",
17=>"seventeen",
16=>"sixteen",
15=>"fifteen",
14=>"fourteen",
13=>"thirteen",
12=>"twelve",
11 => "eleven",
10 => "ten",
9 => "nine",
8 => "eight",
7 => "seven",
6 => "six",
5 => "five",
4 => "four",
3 => "three",
2 => "two",
1 => "one"
}
str = ""
numbers_to_name.each do |num, name|
if int == 0
return str
elsif int.to_s.length == 1 && int/num > 0
return str + "#{name}"
elsif int < 100 && int/num > 0
return str + "#{name}" if int%num == 0
return str + "#{name} " + in_words(int%num)
elsif int/num > 0
return str + in_words(int/num) + " #{name} " + in_words(int%num)
end
end
end
puts in_words(4) == "four"
puts in_words(27) == "twenty seven"
puts in_words(102) == "one hundred two"
puts in_words(38_079) == "thirty eight thousand seventy nine"
puts in_words(82102713) == "eighty two million one hundred two thousand seven hundred thirteen"
Have you considered humanize ?
https://github.com/radar/humanize
Simple answer use humanize gem and you will get desired output
Install it directly
gem install humanize
Or add it to your Gemfile
gem 'humanize'
And you can use it
require 'humanize'
1.humanize #=> 'one'
345.humanize #=> 'three hundred and forty-five'
1723323.humanize #=> 'one million, seven hundred and twenty-three thousand, three hundred and twenty-three'
If you are using this in rails you can directly use this
NOTE: As mentioned by sren in the comments below. The humanize method provided by ActiveSupport is different than the gem humanize
You can also use the to_words gem.
This Gem converts integers into words.
e.g.
1.to_words # one ,
100.to_words # one hundred ,
101.to_words # one hundred and one
It also converts negative numbers.
I can see what you're looking for, and you may wish to check out this StackOverflow post: Number to English Word Conversion Rails
Here it is in summary:
No, you have to write a function yourself. The closest thing to what
you want is number_to_human, but that does not convert 1 to One.
Here are some URLs that may be helpful:
http://codesnippets.joyent.com/posts/show/447
http://raveendran.wordpress.com/2009/05/29/ruby-convert-number-to-english-word/
http://deveiate.org/projects/Linguistics/
I am not quite sure, if this works for you. Method can be called like this.
n2w(33123) {|i| puts i unless i.to_s.empty?}
Here is the method ( I have not tested it fully. I think it works upto million. Code is ugly, there is a lot of room for re-factoring. )
def n2w(n)
words_hash = {0=>"zero",1=>"one",2=>"two",3=>"three",4=>"four",5=>"five",6=>"six",7=>"seven",8=>"eight",9=>"nine",
10=>"ten",11=>"eleven",12=>"twelve",13=>"thirteen",14=>"fourteen",15=>"fifteen",16=>"sixteen",
17=>"seventeen", 18=>"eighteen",19=>"nineteen",
20=>"twenty",30=>"thirty",40=>"forty",50=>"fifty",60=>"sixty",70=>"seventy",80=>"eighty",90=>"ninety"}
scale = {3=>"hundred",4 =>"thousand",6=>"million",9=>"billion"}
if words_hash.has_key?n
yield words_hash[n]
else
ns = n.to_s.split(//)
while ns.size > 0
if ns.size == 2
yield("and")
yield words_hash[(ns.join.to_i) - (ns.join.to_i)%10]
ns.shift
end
if ns.size > 4
yield(words_hash[(ns[0,2].join.to_i) - (ns[0,2].join.to_i) % 10])
else
yield(words_hash[ns[0].to_i])
end
yield(scale[ns.size])
ns.shift
end
end
end
def subhundred number
ones = %w{zero one two three four five six seven eight nine
ten eleven twelve thirteen fourteen fifteen
sixteen seventeen eighteen nineteen}
tens = %w{zero ten twenty thirty **forty** fifty sixty seventy eighty ninety}
subhundred = number % 100
return [ones[subhundred]] if subhundred < 20
return [tens[subhundred / 10]] if subhundred % 10 == 0
return [tens[subhundred / 10], ones[subhundred % 10]]
end
def subthousand number
hundreds = (number % 1000) / 100
tens = number % 100
s = []
s = subhundred(hundreds) + ["hundred"] unless hundreds == 0
s = s + ["and"] unless hundreds == 0 or tens == 0
s = s + [subhundred(tens)] unless tens == 0
end
def decimals number
return [] unless number.to_s['.']
digits = number.to_s.split('.')[1].split('').reverse
digits = digits.drop_while {|d| d.to_i == 0} . reverse
digits = digits.map {|d| subhundred d.to_i} . flatten
digits.empty? ? [] : ["and cents"] + digits
end
def words_from_numbers number
steps = [""] + %w{thousand million billion trillion quadrillion quintillion sextillion}
result = []
n = number.to_i
steps.each do |step|
x = n % 1000
unit = (step == "") ? [] : [step]
result = subthousand(x) + unit + result unless x == 0
n = n / 1000
end
result = ["zero"] if result.empty?
result = result + decimals(number)
result.join(' ').strip
end
def words_from_numbers(number)
ApplicationHelper.words_from_numbers(number)
end
Its been quite a while since the question was asked. Rails has something inbuilt for this now.
https://api.rubyonrails.org/classes/ActionView/Helpers/NumberHelper.html
number_to_human(1234567) # => "1.23 Million"
number_to_human(1234567890) # => "1.23 Billion"
number_to_human(1234567890123) # => "1.23 Trillion"
number_to_human(1234567890123456) # => "1.23 Quadrillion"
number_to_human(1234567890123456789) # => "1230 Quadrillion"

How to generate a human readable time range using ruby on rails

I'm trying to find the best way to generate the following output
<name> job took 30 seconds
<name> job took 1 minute and 20 seconds
<name> job took 30 minutes and 1 second
<name> job took 3 hours and 2 minutes
I started this code
def time_range_details
time = (self.created_at..self.updated_at).count
sync_time = case time
when 0..60 then "#{time} secs"
else "#{time/60} minunte(s) and #{time-min*60} seconds"
end
end
Is there a more efficient way of doing this. It seems like a lot of redundant code for something super simple.
Another use for this is:
<title> was posted 20 seconds ago
<title> was posted 2 hours ago
The code for this is similar, but instead i use Time.now:
def time_since_posted
time = (self.created_at..Time.now).count
...
...
end
If you need something more "precise" than distance_of_time_in_words, you can write something along these lines:
def humanize(secs)
[[60, :seconds], [60, :minutes], [24, :hours], [Float::INFINITY, :days]].map{ |count, name|
if secs > 0
secs, n = secs.divmod(count)
"#{n.to_i} #{name}" unless n.to_i==0
end
}.compact.reverse.join(' ')
end
p humanize 1234
#=>"20 minutes 34 seconds"
p humanize 12345
#=>"3 hours 25 minutes 45 seconds"
p humanize 123456
#=>"1 days 10 hours 17 minutes 36 seconds"
p humanize(Time.now - Time.local(2010,11,5))
#=>"4 days 18 hours 24 minutes 7 seconds"
Oh, one remark on your code:
(self.created_at..self.updated_at).count
is really bad way to get the difference. Use simply:
self.updated_at - self.created_at
There are two methods in DateHelper that might give you what you want:
time_ago_in_words
time_ago_in_words( 1234.seconds.from_now ) #=> "21 minutes"
time_ago_in_words( 12345.seconds.ago ) #=> "about 3 hours"
distance_of_time_in_words
distance_of_time_in_words( Time.now, 1234.seconds.from_now ) #=> "21 minutes"
distance_of_time_in_words( Time.now, 12345.seconds.ago ) #=> "about 3 hours"
chronic_duration parses numeric time to readable and vice versa
If you want to show significant durations in the seconds to days range, an alternative would be (as it doesn't have to perform the best):
def human_duration(secs, significant_only = true)
n = secs.round
parts = [60, 60, 24, 0].map{|d| next n if d.zero?; n, r = n.divmod d; r}.
reverse.zip(%w(d h m s)).drop_while{|n, u| n.zero? }
if significant_only
parts = parts[0..1] # no rounding, sorry
parts << '0' if parts.empty?
end
parts.flatten.join
end
start = Time.now
# perform job
puts "Elapsed time: #{human_duration(Time.now - start)}"
human_duration(0.3) == '0'
human_duration(0.5) == '1s'
human_duration(60) == '1m0s'
human_duration(4200) == '1h10m'
human_duration(3600*24) == '1d0h'
human_duration(3600*24 + 3*60*60) == '1d3h'
human_duration(3600*24 + 3*60*60 + 59*60) == '1d3h' # simple code, doesn't round
human_duration(3600*24 + 3*60*60 + 59*60, false) == '1d3h59m0s'
Alternatively you may be only interested in stripping the seconds part when it doesn't matter (also demonstrating another approach):
def human_duration(duration_in_seconds)
n = duration_in_seconds.round
parts = []
[60, 60, 24].each{|d| n, r = n.divmod d; parts << r; break if n.zero?}
parts << n unless n.zero?
pairs = parts.reverse.zip(%w(d h m s)[-parts.size..-1])
pairs.pop if pairs.size > 2 # do not report seconds when irrelevant
pairs.flatten.join
end
Hope that helps.
There is problem with distance_of_time_in_words if u ll pass there 1 hour 30 min it ll return about 2 hours
Simply add in helper:
PERIODS = {
'day' => 86400,
'hour' => 3600,
'minute' => 60
}
def formatted_time(total)
return 'now' if total.zero?
PERIODS.map do |name, span|
next if span > total
amount, total = total.divmod(span)
pluralize(amount, name)
end.compact.to_sentence
end
Basically just pass your data in seconds.
Rails has a DateHelper for views. If that is not exactly what you want, you may have to write your own.
#Mladen Jablanović has an answer with good sample code. However, if you don't mind continuing to customize a sample humanize method, this might be a good starting point.
def humanized_array_secs(sec)
[[60, 'minutes '], [60, 'hours '], [24, 'days ']].inject([[sec, 'seconds']]) do |ary, (count, next_name)|
div, prev_name = ary.pop
quot, remain = div.divmod(count)
ary.push([remain, prev_name])
ary.push([quot, next_name])
ary
end.reverse
end
This gives you an array of values and unit names that you can manipulate.
If the first element is non-zero, it is the number of days. You may want to write code to handle multiple days, like showing weeks, months, and years. Otherwise, trim off the leading 0 values, and take the next two.
def humanized_secs(sec)
return 'now' if 1 > sec
humanized_array = humanized_array_secs(sec.to_i)
days = humanized_array[-1][0]
case
when 366 <= days
"#{days / 365} years"
when 31 <= days
"#{days / 31} months"
when 7 <= days
"#{days / 7} weeks"
else
while humanized_array.any? && (0 == humanized_array[-1][0])
humanized_array.pop
end
humanized_array.reverse[0..1].flatten.join
end
end
The code even finds use for a ruby while statement.

Resources