Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm a beginner & just started to learn ruby through Codecademy. I understand this will be a very simple question. Why are we declaring answer2 in this code? Wouldn't they both be equal?
print "This is my question?"
answer = gets.chomp
answer2 = answer.capitalize
answer.capitalize!
Thanks.
They're trying to teach you the difference between capitalize and capitalize!. The first doesn't modify the string but rather returns a modified copy of it. While the latter (with a bang ! mark) modifies the string itself.
So to further explain the example above, consider the following:
print "This is my question?"
answer = gets.chomp
answer2 = answer.capitalize
puts "answer= " + answer + ", while answer2= " + answer2
answer.capitalize!
puts "now answer= " + answer
So if we execute the previous code and enter foobar when it prompts, the output will be:
This is my question?foobar
answer= foobar, while answer2= Foobar
now answer= Foobar
Generally, methods in ruby don't modify the objects that called them, but rather return a modified copy. So in some_object.some_method, the some_method method is not going to change some_object but will return a copy of it with some modifications. Alternatively, methods with at bang ! mark change the objects that called them. So some_object.some_method! will change some_object itself.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
In Pragmatic Programmer's Ruby book, they're using to_s to clean up the output. Their output ends up looking like:
Name: UserName, Address: UserAddress, Phone: UserPhone, EMail: UserEmail
They don't have any line breaks, and I'm attempting to add them to mine using \n within the to_s definition:
class PersonalInformation
def initialize (fullname, address, phonenumber, emailaddress)
#fullname = fullname
#address = address
#phonenumber = phonenumber
#emailaddress = emailaddress
end
def to_s
"Name: #{#fullname}\n, Address: #{#address}\n, Phone: #{#phonenumber}\n, EMail: #{#emailaddress}\n"
end
end
info1 = PersonalInformation.new('UserName', 'UserAddress', 'UserPhone', 'UserEmail')
p info1
but I'm not having any luck getting the output to display the way I want, and it doesn't appear that to_s is doing what I expect it to anyway. Mine looks like:
#<PersonalInformation:0x00000002d0e600 #fullname="UserName", #address="UserAddress", #phonenumber="UserPhone", #emailaddress="UserEmail">
A) What do I need to do to make to_s display the way I expect?
B) How do I go about getting line breaks into the output message?
p calls Object#inspect method, not to_s.
You need to call to_s explicitly, or use methods that call to_s. For example, print, puts, printf with %s format.
A) You don't need anything extra. It already displays the way you want.
B) As you did, putting "\n" works.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Hi I'm trying to get the program to return all three lines on top of one another when a 5 is entered. It's only returning the third line. This is Ruby. (My first time trying it)
moveOne = gets.to_i
if moveOne == 5
puts = "1,2,X"
"4,O,6"
"X,8,9"
puts is a method that accepts one or more arguments and writes them (their #inspect-ed value) to an IO object separated by a newline. As written you are trying to assign puts a value rather than passing the values as parameters.
Try this
puts "1, 2, X",
"4, 0, 6",
"X, 8, 9"
That's passing three strings to puts and preserves your desired readability.
moveOne = gets.to_i
if moveOne == 5
puts "1,2,X"
puts "4,O,6"
puts "X,8,9"
end
P.S: Please try to learn more before posting a question.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm trying to print key : value
Currently I keep getting errors when I try to run my codes.
The code:
output.each do |key, value|
puts key + ' : ' + value
end
I can not figure out a way to do this on the same line. I've tried various implementations, like using the << symbol. I've also played around with print, using multiple puts statements, and appending both values into a string and printing that.
Depending on the contents of your Hash, you might need to convert the key to a string since it might be a symbol.
puts key.to_s + ' : ' + value
Or, what I would suggest doing, use string interpolation:
puts "#{key}:#{value}"
The reason you are getting an error, if key is indeed not a string, is because it is trying to call the method + on whatever key is. If it does not have a + method, you will get an error.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have the following code
animals=['lion','tiger','zebra']
animals.each{|a| puts a}
I wanted to print only tiger in this array for that I wrote something like this
animals.each{|a| if a==1 puts animals[a]}
But it's not working why?
You can play with enumerable like this:
animals.select{ |a| a == 'tiger' }.each{ |a| puts a }
The wrong you did in your case:-
animals.each{|a| if animals[a]==2 puts a}
inline if statement you put in a wrong way.
#each passes element of the array,not the index. So animals[a] will not work. It will throw error as no implicit conversion of String into Integer (TypeError).
Do this as below using Array#each_index
animals=['lion','tiger','zebra']
animals.each_index{|a| puts animals[a] if animals[a] == 'tiger' }
# >> tiger
Maybe you are looking for this
animals.each_with_index{|animal, index| puts animal if index==1}
Please not that "tiger" occurs at index 1 and not 2.
you can simply do this
animals.fetch(animals.index('tiger')) if animals.include? 'tiger'
or
animals[animals.index('tiger')] if animals.include? 'tiger'
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I often do the following:
#value = #value.some_method
But isn't there a shorter syntax for that in ruby? Some methods offer bang equivalents, but sadly not all...
For iterations one can use:
i += 1
Is that, or something similar, also available for my code snippet above?
There's nothing that does this in a shorter way.
To be fair, it is an unusual pattern, and while not especially rare, would lead to confusion if there was an operator like:
#value .= some_method
How is that even supposed to be parsed when reading?
As the Tin Man points out, in-place operators are really what are best here.
You cannot do that taking the actual variable as the receiver because there is no way to get to the name of the variable. Instead, you need to use the name of the variable like this:
class A
attr_accessor :value
def change_to_do_something name
instance_variable_set(name, "do something")
end
end
a = A.new
a.value = "Hello"
p a.value
# => "Hello"
a.change_to_do_something(:#value)
p a.value
# => "do something"