On 29 November 2016 at 09:56, Micky Scandal <mickyscandal / gmail.com> wrote:

> hello, question. I don't know if this is even possible but is there a way
> to give arguments in a case statement? I currently have something like this:
>
>       def seek_foreward(amt = 3)
>         @player.seek(amt)
>       end
>
>       when "SEEKF"
>         puts "Enter amount to seek forward: "
>         amt = STDIN.gets.chomp
>         player.seek_foreward(amt)
>
> but what I was hoping for is to be able to input something like this:
>  :> SEEKF 10
> and have it give 10 to the seek_foreward method. is something like that
> possible with a case statement? or is there another way to implement this?
>

Do you mean you want to be able to match a string that starts with 'SEEKF'
in the `when` clause? Regular expressions might help:

~~~
irb(main):001:0> case STDIN.gets.chomp
irb(main):002:1> when /^SEEKF (\d+)$/
irb(main):003:1>   puts ">>> SEEKING #{$1.to_i}"
irb(main):004:1> else
irb(main):005:1*   puts "???"
irb(main):006:1> end
SEEKF 10
>>> SEEKING 10
~~~

Cheers
-- 
  Matthew Kerwin
  http://matthew.kerwin.net.au/
(supressed text/html)
Unsubscribe: <mailto:ruby-talk-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>