is there an ordinal to cardinal function in ruby or rails?
Use the short-form, easily-parsable ordinal:
When /^I fill up the (\d+)(?:st|nd|rd|th) passenger field$/ do |n|
# etc...
end
Ruby DateTime format: How can I get 1st, 2nd, 3rd, 4th?
You might want to take a look here.
To summarize
time = DateTime.now
time.strftime("%A, %B #{time.day.ordinalize} %Y")
Note that you are running in plain Ruby (2.0) you'll need to call:
require 'active_support/core_ext/integer/inflections'
English words (ONE, TWO, THREE) to number(1,2,3) in ruby
I created a hash up-to twenty for all words and only for tens(thirty, forty, fifty etc.). Using Reg-ex took off two words and added them for example twenty two is addition of 20+2=22; right now my script only works till hundred, but it can be extended for numbers over 100 and so on.
How do I get the second integer in a Ruby string with to_i?
How about something like:
text = "unkowntext60moreunknowntext25something"
@width, @height = text.scan(/\d+/).map { |n| n.to_i } #=> 60, 25
Replace the second integer/number in a string
gsub
is the easiest way to achieve this job.
Input
a='We are there from 2 to 5 oclock'
Code
p a.gsub(/\D*\d+\D+\K\d+/,"10000")
Output
"We are there from 2 to 10000 oclock"
Generate letters to represent number using ruby?
class Numeric
Alph = ("a".."z").to_a
def alph
s, q = "", self
(q, r = (q - 1).divmod(26)); s.prepend(Alph[r]) until q.zero?
s
end
end
3.alph
# => "c"
26.alph
# => "z"
27.alph
# => "aa"
4123.alph
# => "fbo"
Related Topics
Typeerror: Object Doesn't Support This Property or Method
Git Bash Chcp Windows7 Encoding Issue
Scraping a Site That Requires Login Username and Password on Two Separate Pages
Advice on How to Validate Names and Surnames Using Regex
Does Anyone Have Parsing Rules for the Notepad++ Function List Plugin for Ruby and Rake
Do I Need to Indent My Code in Ruby
Marshal Ruby Hash with Default Proc - Remove the Default Proc
Https Request Using Net::Http's Block Form -- Is It Possible
Cattr_Accessor Outside of Rails
Building Gem, Executable Not Found
How to Change Ruby to Version 1.9.3 (Again) with Rvm
Rails 3 and PDFkit. How to Specify Page Size
Cancan Abilities in Separate File
Get All Keys in Hash with Same Value
Ruby Spreadsheet Row Background Color
Should I Be Using Rails or Ruby for This Website Application? How