6 Replies - 3909 Views - Last Post: 27 September 2018 - 11:46 PM Rate Topic: -----

#1 TechFoxx   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 02-July 18

How to take user input and reverse it using unshift and join array?

Posted 03 July 2018 - 12:06 AM

Rev = []
puts("Type in a word and I will print it backwards.")
aWord = gets.chomp

i = 0
while i < aWord.length
Rev.unshift(aWord[i])
i += 1
end

Rev.join("")
puts(Rev)



The console outputs the reverse of all the user's inputed characters vertically. Does this mean the program didn't join the array, or is it a problem with eliminating newlines?
Is This A Good Question/Topic? 0
  • +

Replies To: How to take user input and reverse it using unshift and join array?

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14693
  • View blog
  • Posts: 58,724
  • Joined: 12-June 08

Re: How to take user input and reverse it using unshift and join array?

Posted 03 July 2018 - 06:40 AM

What is the output? What was the output you were expecting?
Was This Post Helpful? 0
  • +
  • -

#3 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2712
  • View blog
  • Posts: 4,325
  • Joined: 21-June 11

Re: How to take user input and reverse it using unshift and join array?

Posted 03 July 2018 - 09:21 AM

join concatenates the elements of the array and returns the result as a string. It does not modify the array in any way or cause any other side-effects. So calling join without doing anything with the return value accomplishes nothing.

This post has been edited by sepp2k: 03 July 2018 - 09:24 AM

Was This Post Helpful? 2
  • +
  • -

#4 TechFoxx   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 02-July 18

Re: How to take user input and reverse it using unshift and join array?

Posted 03 July 2018 - 10:07 PM

View Postsepp2k, on 03 July 2018 - 09:21 AM, said:

join concatenates the elements of the array and returns the result as a string. It does not modify the array in any way or cause any other side-effects. So calling join without doing anything with the return value accomplishes nothing.



Hey! Thanks for the reply. So if I want to join all the elements of the array into a 1 lined string, how could I modify the return value to print as such?

Input: fox
Output: xof

Right now, it's just giving me:
x
o
f
>null
Was This Post Helpful? 0
  • +
  • -

#5 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2712
  • View blog
  • Posts: 4,325
  • Joined: 21-June 11

Re: How to take user input and reverse it using unshift and join array?

Posted 04 July 2018 - 02:01 PM

You don't need to modify the return value, you just need to print it (rather than printing the array).
Was This Post Helpful? 1
  • +
  • -

#6 NeoTifa   User is offline

  • NeoTifa Codebreaker, the Scourge of Devtester
  • member icon





Reputation: 4390
  • View blog
  • Posts: 18,829
  • Joined: 24-September 08

Re: How to take user input and reverse it using unshift and join array?

Posted 09 July 2018 - 08:23 AM

puts Rev.join. I only gave straight out because they were right there. It's not changing the array itself as stated above. It's a by-value type method vs. by-reference type method (how I look at it). Stuff like array.flatten vs array.flatten!. There is no join! method so it's by-value.
Was This Post Helpful? 0
  • +
  • -

#7 Lemur   User is offline

  • Pragmatism over Dogma
  • member icon


Reputation: 1451
  • View blog
  • Posts: 3,633
  • Joined: 28-November 09

Re: How to take user input and reverse it using unshift and join array?

Posted 27 September 2018 - 11:46 PM

View PostTechFoxx, on 03 July 2018 - 02:06 AM, said:

Rev = []
puts("Type in a word and I will print it backwards.")
aWord = gets.chomp

i = 0
while i < aWord.length
  Rev.unshift(aWord[i])
  i += 1
end

Rev.join("")
puts(Rev)



The console outputs the reverse of all the user's inputed characters vertically. Does this mean the program didn't join the array, or is it a problem with eliminating newlines?


They're right that join does not mutate. In Ruby you'll find that many methods lacking a bang (!) do not mutate the original object. There's a reason for this, but that's beyond the scope of this answer.

Puts isn't a single arity function, it's variadic. That means that since it got an array it's going to output every element on a separate line.

Try it out, you can use as many arguments to puts as you want:

puts 1, 2, 3, 4, 5



Now, if you were to want to reverse a string in Ruby, you're probably going to want to look through the String documentation:

https://ruby-doc.org...5.1/String.html

Ruby has an interesting tendency towards names for methods that are quite apropos for what you'd think they'd do. I wonder if there's a reverse method that already exists?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1