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

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 4964 Views - Last Post: 16 March 2019 - 03:20 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: 15310
  • View blog
  • Posts: 61,392
  • 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? 1
  • +
  • -

#3 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2754
  • View blog
  • Posts: 4,414
  • 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: 2754
  • View blog
  • Posts: 4,414
  • 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: 4572
  • View blog
  • Posts: 19,277
  • 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: 1453
  • 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? 1
  • +
  • -

#8 BobRodes   User is offline

  • Product Manager
  • member icon

Reputation: 603
  • View blog
  • Posts: 3,085
  • Joined: 19-May 09

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

Posted 12 March 2019 - 05:08 PM

Lemur's answer notwithstanding (yes, you should look at the documentation for the `String` object), you aren't looking at this the right way. Whether a method is a "mutating method" or not is important, but you also need to think of methods as things that are called and return values. All methods return values (even if the value is `nil`) in Ruby. So `join` returns the value that you're looking for, rather than directly altering the caller (your `Rev` variable). Meanwhile, `puts` is a method, and you're passing it a value. These four calls to 'puts' are identical:

puts '7'
puts('7')
Kernel.puts '7'
Kernel.puts('7')


So, what you really need to do is to send the result of your call to 'join' to 'puts':
puts Rev.join


Note that `""` is the default argument for `join` so you don't have to use it.

Another cute way to do what you want:
puts("Type in a word and I will print it backwards.")
a_word = gets.chomp

rev = []
a_word.chars.each { |c| rev.unshift(c) }
puts rev

# A one-liner
puts a_word.chars.each_with_object([]) { |c, a| a.unshift(c) }

# and:
puts a_word.esrever


This post has been edited by BobRodes: 26 March 2019 - 05:48 PM

Was This Post Helpful? 0
  • +
  • -

#9 andrewsw   User is online

  • never lube your breaks
  • member icon

Reputation: 6819
  • View blog
  • Posts: 28,250
  • Joined: 12-December 12

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

Posted 13 March 2019 - 02:25 AM

I am not a Rubyist but I am curious about something in Bob's code.

puts a_word.chars.each_with_object([]) { |c, a| a.unshift(c) }


unshift is yielding each character (from the front) in turn so that puts is still outputting each to a separate line. (I'm using https://repl.it/languages/ruby .)

Does Ruby have some kind of 'piping' or aggregation so that the characters would be accumulated in this single line of code, before puts receives them?
Was This Post Helpful? 0
  • +
  • -

#10 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2754
  • View blog
  • Posts: 4,414
  • Joined: 21-June 11

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

Posted 13 March 2019 - 03:37 AM

Each character is output in a single line because puts prints arrays one element per line. It's the same thing with puts rev.

This post has been edited by sepp2k: 13 March 2019 - 03:37 AM

Was This Post Helpful? 0
  • +
  • -

#11 andrewsw   User is online

  • never lube your breaks
  • member icon

Reputation: 6819
  • View blog
  • Posts: 28,250
  • Joined: 12-December 12

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

Posted 13 March 2019 - 04:06 AM

Thanks @sepp2k. I realise that. I was thinking of something like this (non-working):

result = ""
# A one-liner
puts a_word.chars.each_with_object([]) { |c, a| result += a.unshift(c) }


where instead of puts dealing with an array, a string is accumulated, before puts receives it.

Or this, which runs but still prints on separate lines

result = ""
# A one-liner
puts a_word.chars.each_with_object([]) { |c, a| result = result + a.unshift(c).to_s } ; result

Was This Post Helpful? 0
  • +
  • -

#12 andrewsw   User is online

  • never lube your breaks
  • member icon

Reputation: 6819
  • View blog
  • Posts: 28,250
  • Joined: 12-December 12

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

Posted 13 March 2019 - 04:28 AM

Or this,

result = ""
# A one-liner
puts a_word.chars.each_with_object([]) { |c, a| result = Array(result) + a.unshift(c) } ; result.last(a_word.length).join

puts "result:"
puts result.last(a_word.length).join


Maybe I'm losing the plot ;)
Was This Post Helpful? 0
  • +
  • -

#13 BobRodes   User is offline

  • Product Manager
  • member icon

Reputation: 603
  • View blog
  • Posts: 3,085
  • Joined: 19-May 09

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

Posted 14 March 2019 - 04:33 PM

View Postandrewsw, on 13 March 2019 - 04:25 AM, said:

I am not a Rubyist but I am curious about something in Bob's code.

puts a_word.chars.each_with_object([]) { |c, a| a.unshift(c) }


unshift is yielding each character (from the front) in turn so that puts is still outputting each to a separate line. (I'm using https://repl.it/languages/ruby .)

Does Ruby have some kind of 'piping' or aggregation so that the characters would be accumulated in this single line of code, before puts receives them?


I'd say yes. That's what join does.

The way that each_with_object works is that it returns the result of the block iterations (the stuff inside the curly brackets) in whatever was passed to the each_with_object method, in this case an empty array. So, the result is an array full of the results of the block iteration.

You can put it all on one line by calling the join method on the resulting array, so:

puts a_word.chars.each_with_object([]) { |c, a| a.unshift(c) }.join


This will concatenate all the array elements into a single string, which puts will then print. So, you were on the right track with the join method, but you didn't quite get around to tacking it onto the end of the each_with_object block.

This post has been edited by BobRodes: 14 March 2019 - 05:00 PM

Was This Post Helpful? 1
  • +
  • -

#14 BobRodes   User is offline

  • Product Manager
  • member icon

Reputation: 603
  • View blog
  • Posts: 3,085
  • Joined: 19-May 09

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

Posted 14 March 2019 - 05:17 PM

For added fun, try wrapping your brain around this one:

a_word = 'Now is the time for all good people to come forth'
(a_word.size/2).times { |i| a_word[i], a_word[-(i + 1)] = a_word[-(i + 1)], a_word[i] }
p a_word


This post has been edited by BobRodes: 14 March 2019 - 05:18 PM

Was This Post Helpful? 0
  • +
  • -

#15 andrewsw   User is online

  • never lube your breaks
  • member icon

Reputation: 6819
  • View blog
  • Posts: 28,250
  • Joined: 12-December 12

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

Posted 15 March 2019 - 02:06 AM

@Bob thank you. I see what you did there.

[off-topic]
Is join a monad? Please respond within 24 words.

Quote

A monad is just a monoid in the category of endofunctors, what's the problem?

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2