I want to get a string of the current time in Ruby:
"Time is " + Time.new.month + "/" + Time.new.day + "/" + Time.new.year
But it says "can't convert Fixnum into String". How can I fix this?
Or you could just use right tool for the job: time formatting.
Time.new.strftime "Time is %m/%d/%Y" # => "Time is 11/13/2012"
You could use to_s
"Time is " + Time.new.month.to_s + "/" + Time.new.day.to_s + "/" + Time.new.year.to_s
But event better is to use strftime
Time.new.strftime("Time is %-m/%e/%Y")
Ruby can only add string to string, so conversion is required. As a note, elements interpolated in double-quoted strings are automatically converted:
now = Time.new
"Time is #{now.month}/#{now.day}/#{now.year}"
It's also possible to combine them from an array where they are also automatically converted:
now = Time.new
"Time is " + [ now.month, now.day, now.year ].join('/')
You can also use sprintf-style interpolation:
now = Time.new
"Time is %d/%d/%d" % [ now.month, now.day, now.year ]
The second one gives you more control over formatting. For example %02d will pad with 0 to two places.
As Sergio points out, there's a special-purpose function for this formatting that is probably a better idea. Also Time.now is the traditional method for now, whereas Time.new is for creating arbitrary times.
Whenever possible, prefer string interpolation over concatenation. As you can clearly see in that (thread), using string interpolation would have automatically called to_s for you.
Using string interpolation :
"Time is #{Time.new.month}/#{Time.new.day}/#{Time.new.year}"
Related
Theoretical question
I'm trying to find new practical ways to convert integers into strings and the other way around.
I only know the .to_s ; .to_i ; .to_f methods and would like to know if there are other ways to do to it without writing + to put together the variables. For example:
var1 = 16
puts 'I\'m ' + var1.to_s + ' years old.'
In longer codes is getting tiring writing all this to just convert a integer to a string.
By the way I also found this Timer program here on Stack and the #{ is an example of what I'm trying to do. Adding an integer to a string without + and .to_s But I don't know how it works.
30.downto(0) do |i|
puts "00:00:#{'%02d' % i}"
sleep 1
end
Thank you in advance for the suggestions!
Ruby has a pretty powerful string interpolator feature using #{...} where that can contain fairly arbitrary Ruby code. The end result is always converted to a string using, effectively, to_s.
That is you can do this:
puts "00:00:#{'%02d' % i}"
Where that gets stringified and then interpolated.
This is roughly the same as:
i_str = '%02d' % i
puts "00:00:#{i_str}"
Where that is effectively:
i_str = '%02d' % i
puts "00:00:%s" % i_str
You could also combine that into a single operation:
puts "00:00:%02d" % i
Where you generally use interpolation or sprintf-style template strings, not both at the same time. It keeps your code cleaner since only one mechanism is in play.
The only reason .to_s is needed when doing concatenation is Ruby is very particular about "adding" together two things. x + y has a completely different outcome depending on what x and y are.
Consider:
# Integer + Integer (Integer#+)
1 + 2
# => 3
# Array + Array (Array#+)
[ 1 ] + [ 2 ]
# => [1,2]
# String + String (String#+)
"1" + "2"
# => "12"
Note that in each case it's actually a different method being called, and the general form of x + y is:
x.send(:+, y)
So it's actually a method call, and as such, each method may impose restrictions on what it can operate on by emitting exceptions if it can't or won't deal.
It's called string interpolation. For example:
puts "I\'m #{var1} years old."
The way it works is this:
You have to enclose the string in double quotes, not single quotes.
You put your variable inside this: #{}, e.g. "#{variable}".
This will always convert non-string variables into strings, and plug (i.e. interpolate) them into the surrounding string.
If I want a string of both words and numbers in ruby, such as "worda, wordb, 12, wordc, 10,"
do I need to first convert the number to a string ie.
a = 12.to_s?
Possible ways to mix strings and integers
It depends how you want to do it :
["worda", 10].join(', ')
"worda, #{10}"
"worda, %d" % 10
"worda" + ", " + 10.to_s
"worda" << ", " << 10.to_s
all return "worda, 10"
join and string interpolation will both call .to_s implicitely.
String + Integer
"worda" + 10
Is a TypeError, though, because there's no implicit conversion with +.
Otherwise "1" + 2 could be either "12" or 3. Javascript accepts it and returns "12", which is a mess IMHO.
String << Integer
Finally:
"worda, " << 10
is a valid Ruby syntax, but it appends the ASCII code 10 (a newline), not the number 10:
"worda, \n"
Ruby requires (approx.) strings to be of the same type, like most reasonable programming languages.
You have 1 solution.
"word" + 12.to_s
or
"word #{12}"
The second example is called string interpolation, and will call the method .to_s on any object passed in.
Yes, but you can do
"worda, wordb, #{num_1}, wordc, #{num_2},"
I have a class called Fractions that takes in a numerator and a denominator. It has three methods- frac, redfrac, dec- that print out the fraction, its reduced version, and its decimal form respectively.
As an example, if the numerator is 12 and the denominator is 4, I want to print out the result as so:
Fraction: 12/4
Reduced Fraction: 3
Fraction as Decimal: 3.0
My current code after my Fractions class is as follows
a = Fractions.new(numer, denom)
puts "Fraction:"
a.frac
puts "Reduced Fraction:"
a.redfrac
puts "Fraction as a Decimal:"
a.dec
which prints out
Fraction:
12/4
Reduced Fraction:
3
Fraction as a Decimal:
3.0
How do I print the text as I want it? I tried doing
puts "Fraction:" + a.frac
but this does not work because I cannot interpolate a string with my calling of the method.
"Fraction:" + a.frac is not using string interpolation - it is using the + operator and trying to add a Fixnum to a String, which is impossible.
To use String interpolation, you can do it like this:
puts "Fraction: #{a.frac}"
You could also do this:
puts "Fraction: " + a.frac.to_s
Which converts the Fixnum to a string before adding them together
Do the same for the reduced version and the decimal form respectively.
See https://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Literals#Interpolation for more information (RubyMonk also has a good explanation of string interpolation, but appears to be down).
You couldn't do string concatenation with + operator because the return value of a.frac is not a string.
You could explicitly convert it to string with .to_s and change your line:
puts "Fraction:" + a.frac.to_s
or, you could use Ruby's string interpolation like this:
puts "Fraction: #{a.frac}"
I can print a raw number with this code:
puts 'Please enter your favorite number'
favNumber = gets.chomp
betterNumber = favNumber.to_i
puts betterNumber + 1
but I need to set a message including the number. I changed the last two lines to this, but it's wrong.
betterNumber = favNumber.to_i + 1
puts 'Your favorite number sucks, a better number is '+ betterNumber + '!'
Help me.
betterNumber is of class Fixnum and your string is of course of class String. You can't add a String and a Fixnum, you need to cast your Fixnum into a String using to_s.
"Your favorite number sucks, a better number is " + betterNumber.to_s + "!"
Also, using interpolation calls to_s on any objects being interpolated. So this works, too (and is more common):
"Your favorite number sucks, a better number is #{betterNumber}!"
Also, in Ruby we usually use snake_case variables as opposed to camelCase variables. So I recommend using better_number
You need to convert betterNumber to a string when printing it, like this: betterNumber.to_s.
I am total begineer in ruby so its very novice question.
I am trying to concatenate a string with a float value like follows and then printing it.
puts " Total Revenue of East Cost: " + total_revenue_of_east_cost
total_revenue_of_east_cost is a variable holding float value, how i can make it print?
This isn't exactly concatenation but it will do the job you want to do:
puts " Total Revenue of East Cost: #{total_revenue_of_east_cost}"
Technically, this is interpolation. The difference is that concatenation adds to the end of a string, where as interpolation evaluates a bit of code and inserts it into the string. In this case, the insertion comes at the end of your string.
Ruby will evaluate anything between braces in a string where the opening brace is preceded by an octothorpe.
Stephen Doyle's answer, using a technique known as "String interpolation" is correct and probably the easiest solution, however there is another way. By calling an objects to_s method that object can be converted to a string for printing. So the following will also work.
puts " Total Revenue of East Cost: " + total_revenue_of_east_cost.to_s
For your example, you might want something more specific than the to_s method. After all, to_s on a float will often include more or less precision than you wish to display.
In that case,
puts " Total Revenue of East Coast: #{sprintf('%.02f', total_revenue_of_east_coast)}"
might be better. #{} can handle any bit of ruby code, so you can use sprintf or any other formatting method you'd like.
I like (see Class String % for details):
puts " Total Revenue of East Coast: " + "%.2f" % total_revenue_of_east_coast
Example bucle
(1..100).each do |i| puts "indice #{i} " end