This is a simple Ruby code, but I am not getting how to understand and use it.
def string_length_interpolater(incoming_string)
"The string you just gave me has a length of #{incoming_string.length}"
end
Could somebody help me the process?
It returns a string with the phrase shown ("The string you...") plus the length of the string passed in, e.g.
string_length_interpolater('Hi')
=> "The string you just gave me has a length of 2"
string_length_interpolater('Hi There')
=> "The string you just gave me has a length of 8"
string_length_interpolater('123456789')
=> "The string you just gave me has a length of 9"
string_length_interpolater('Hello Ruby')
=> "The string you just gave me has a length of 10"
As you can see the method is just returning that text - "The string you just gave me has a length" with the length of the parameter that is passed in. The #{} within the DOUBLE quotes means work out the ruby value and then use that in the string.
It is also the same as
def string_length_interpolater(incoming_string)
"The string you just gave me has a length of " + incoming_string.length
end
When the output becomes comples - a string with 'ruby output at various points it becomes easier to use the doubles qoutes ('interpolation') method, for example this:
"From #{start} to #{end} the #{person} used a #{tool}"
is often easier to read/write/maintain than
"From " + start + " to " + end + " the " + person + " used a " + tool
Added: If you ALSO want to show the value of the string itself you could use:
def string_and_length(nm)
"Hello there #{nm}, did you know your name is #{nm.length} letters long?"
end
Related
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},"
Trying to solve one of the problems in Chris Pine's book. Asks users to input their first and last names, then it should display the total number of characters of both.
Here's one of many solutions I've come up with:
puts "First name?"
first_name = gets.chomp
puts "Last name?"
last_name = gets.chomp
total = first_name.length.to_i + last_name.length.to_i
puts 'Did you know you have ' + total + ' characters in your name ' + first_name + last_name + '?'
Ruby is pretty strict about the difference between a String and an Integer, it won't convert for you automatically. You have to ask, but you can ask politely:
puts "Did you know you have #{total} characters in your name #{first_name} #{last_name}?"
You can also do the math this way:
puts "Did you know you have #{first_name.length + last_name.length} characters in your name?"
The #{...} interpolation only works inside "double-quoted" strings, but it does convert to a string whatever the result of that little block is. Single quoted ones avoid interpolation, which is sometimes handy.
If you do want to concatenate strings you have to convert manually:
"this" + 2.to_s
Length returns an integer (no need to convert to_i) so change your code to this:
total = first_name.length + last_name.length
I wrote the basic codes below
puts ' Hi there , what is your favorite number ? '
number = gets.chomp
puts number + ' is beautiful '
puts 1 + number.to_i + 'is way better'
But when I run it,I get the error "String can't be coerced into Fixnum (TypeError)". How do I correct this error please?
You cannot add a String to a number. You can add a number to a String, since it is coerced to a String:
'1' + 1
# => "11"
1 + 1
# => 2
1 + '1'
# TypeError!
Since I suspect you want to show the result of adding 1 to your number, you should explicitly cast it to string:
puts (1 + number.to_i).to_s + ' is way better'
or, use string interpolation:
puts "#{1 + number.to_i} is way better"
String can't be coerced into Integer usually happens, when you try to add a string to a number. since you want to add 1 to your number and concatenate it with a string "is way better". you have to explicitly cast the result you got from adding 1 to your number to a string and concatenate it with your string "is way better".
you can update your code to this:
puts (1 + number.to_i).to_s + " " + 'is way better'
Assuming that the numbers are natural numbers:
number = gets.chomp
puts "#{number} is beautiful ", "#{number.succ} is way better"
You might find the results of entering 'xyz' as an input surprising.
This discussion for determining if your input string is a number may be helpful.
this is my first entry to StackOverflow and I'm a newbie coder.
So I'm making a simple addition calc and I added commas in the last 2 lines to print out integers ...
What am I missing? The error says
C:/Ruby193/rubystuff/ex1.rb:13: syntax error, unexpected ',' print
("The result of the addition is " +,result)
I thought this was the right thing to do ... i must have missed something simple.
print ("Please enter your name: ")
name = gets
puts ("Hello, " + name)
print ("Enter a number to add: ")
num1 = gets
print ("Enter a second number to add: ")
num2 = gets
result = Integer(num1) + Integer(num2)
print result
print ("The result of the addition is ",result)
print ("So the result of adding " + num1.chomp + " plus " + num2.chomp + " equals: ",result)
Ruby has string interpolation and I think most would argue that's the most idiomatic way of doing things. RubyMonk does a great job explaining it here
by changing the 'print' call to the puts method you can do:
puts "The result of the additions is #{result}"
There are two ways to pass arguments to a method:
in parentheses directly after the method name
without parentheses with whitespace after the method name
You have white space after the method, ergo you are using option #2 and are passing a single argument ("The result of the addition is ",result) to the method, but ("The result of the addition is ",result) is not legal syntax.
Just trying to write a simple Ruby program here. I'm trying to get the program to ask for the user's favorite number and then suggest a new favorite number that's one greater. What's wrong with the code below?
puts "hey, whats your favorite number?"
favnumber = gets.chomp
newfavnumber = favnumber.to_i + 1
puts "how about " + newfavnumber "?"
puts "how about " + newfavnumber "?"
First of all, you probably wanted a + before the "?" there. The way this is written now, it parses as puts("how about " + newfavnumber("?")), i.e. you're calling a function called newfavnumber, which is obviously not what you want.
However if you change it to puts "how about " + newfavnumber + "?", which you presumably intended, it still won't work: newfavnumber is a number and "how about " is a string. In ruby you can't add numbers to strings. To fix this you can call to_s on newfavnumber to convert it to a string.
A better way to write this would be using string interpolation: puts "how about #{newfavnumber}?". This way you don't need to call to_s because you can use any type inside #{}.
You're missing a + after newfavnumber and a conversion to string.
puts "how about " + newfavnumber.to_s + "?"