0

A snippet of my code below flips a coin and outputs a result of 10 total heads or tails.

(e.g. Heads Tails Heads Tails...)

I'd like to store this into a variable where I can put it into an array and use its strings.

%w[act] only outputs the string "act". How can I get that line of code to output my array of strings from the line act = coin.flip?

Updated and added full code

class Coin

  def flip
    flip = 1 + rand(2)
      if flip == 2 
        then puts "Heads"
      else
        puts "Tails"
      end
  end   

end

array = []

10.times do
  coin = Coin.new
  array << coin.flip 
end

puts array

2 Answers 2

2

This:

10.times do
  coin = Coin.new
  act = coin.flip
end

doesn't produce an array. It simply creates ten coin flips and throws them all away, the result of that expression is, in fact, 10. If you want an array, you'll need to build one.

You could take Douglas's approach or try something a bit more idiomatic.

The Integer#times method returns an enumerator so you can use any of the Enumerable methods on it rather than directly handing it a block. In particular, you could use collect to build an array in one nice short piece of code:

a = 10.times.collect { Coin.new.flip }

That gives you 10 flips in the Array a and then you can puts a or puts a.join(', ') or whatever you want.

The %w[] won't work because that's for generating an Array of whitespace separated words:

%w[] Non-interpolated Array of words, separated by whitespace

So %w[a b c] is just a nicer way of saying ['a', 'b', 'c'] and the words within %w[] are treated as single quoted strings rather than variables or method calls to be evaluated.


Seems that there is some editing going on. You'll also want to modify your flip method to return the flip rather than print it:

def flip
  flip = 1 + rand(2)
  if flip == 2 
    "Heads"
  else
    "Tails"
  end
end

Then you'll get your Heads and Rails in the array.

Sign up to request clarification or add additional context in comments.

4 Comments

Absolutely spot on with the answer. I learned more than just finding a solution. Much appreciated!
@Tony: You could replace your flip implementation with just rand(2).even?? 'Heads' : 'Tails' (or various similar things) as well. And you don't need to create a new Coin each time either, there's no state so you can c = Coin.new; a = 10.times.collect { c.flip }.
Awesome. rand(2).even?? 'Heads' : 'Tails' shortens the code by a lot and indeed works as well.
@Tony: Just don't fall into the trap of thinking that shorter is always better, your first goals should be readable and understandable. If the branches were more complicated I'd drop the ternary for a full if/else to keep things grokkable. The rand(2).even? is a clear readability win though.
1

Put the act results into an array.

arr = []

10.times do
  coin = Coin.new
  arr << coin.flip
end

p arr # => [...]

2 Comments

Great help! But for some reason, puts array outputs 10 blank lines after the ten lines of Heads and Tails. I don't think coin.flip is being put into the array.
10.times.map{ Coin.new.flip } is neater.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.