Case statement with arguments?

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:

···

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?

~~~
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/

ahh, I think that just might work! the method also has a default value for
if no number is given and that won't account for that, but i can just say:
  when /^SEEKF (\d+)$/, "SEEKF"
and maybe add a little logic to only display $1 if it's not equal to 0
otherwise display 3 (which is the default value).

I'm guessing there might be a way to do that by changing that regexp a
little bit, but I'm truly horrible with regexp, lol
thank you very much!

···

On Mon, Nov 28, 2016 at 4:13 PM, Matthew Kerwin <matthew@kerwin.net.au> wrote:

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/

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

If you do not expect other inputs:

input = gets
input.chomp!

seek = input[/SEEK\s+(\d+)/, 1] and seek_foreward(Integer(seek))

Note, there is a spelling error in your method name. :wink:

Cheers

robert

···

On Tue, Nov 29, 2016 at 1:23 AM, Micky Scandal <mickyscandal@gmail.com> wrote:

ahh, I think that just might work! the method also has a default value for
if no number is given and that won't account for that, but i can just say:
  when /^SEEKF (\d+)$/, "SEEKF"
and maybe add a little logic to only display $1 if it's not equal to 0
otherwise display 3 (which is the default value).

I'm guessing there might be a way to do that by changing that regexp a
little bit, but I'm truly horrible with regexp, lol
thank you very much!

--
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can
- without end}
http://blog.rubybestpractices.com/

I was wondering when someone was going to notice that! I'm assuming you're
referring to the 'e' in foreward? I'm just lazy and don't want to go
through and change them all...

on another note, I guess my question is obsolete now. instead of typing
SEEKF followed by a number, I make the seek method a static amount and
mapped it to the left and right arrows so if you want to seek more or less
just hold the arrow key down longer or shorter. I think that's a little
more user friendly.

···

On Tue, Nov 29, 2016 at 1:46 AM, Robert Klemme <shortcutter@googlemail.com> wrote:

Note, there is a spelling error in your method name. :wink:

Note, there is a spelling error in your method name. :wink:

I was wondering when someone was going to notice that! I'm assuming you're
referring to the 'e' in foreward? I'm just lazy and don't want to go through
and change them all...

$ find -type f -name '*.rb' -exec sed -i.bak -e
's#\<seek_foreward\>#seek_forward#g' {} +

Then, if all is well

$ find -type f -name '*.rb.bak' -delete

on another note, I guess my question is obsolete now. instead of typing
SEEKF followed by a number, I make the seek method a static amount and
mapped it to the left and right arrows so if you want to seek more or less
just hold the arrow key down longer or shorter. I think that's a little more
user friendly.

Yeah, sometimes the solution we come up with eventually looks totally
different...

Kind regards

robert

···

On Tue, Nov 29, 2016 at 10:35 PM, Micky Scandal <mickyscandal@gmail.com> wrote:

On Tue, Nov 29, 2016 at 1:46 AM, Robert Klemme <shortcutter@googlemail.com> > wrote:

--
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can
- without end}
http://blog.rubybestpractices.com/