eval question ?

Discussion in 'Ruby' started by Ak 756, Jan 15, 2008.

  1. Ak 756

    Ak 756 Guest

    Hi

    I hava a string indicate a hh/mm/ss time and some other strings indicat
    operations on the string. For example , string "+5" means add 5 second.

    Here is my code:

    def modifytime(s1,s2)
    if s2[0,1] == "+"
    (Time.parse(s1)+s2[1,s2.length-1].to_i).to_s.split[3]
    else if s2[0,1] == "-"
    (Time.parse(s1)-s2[1,s2.length-1].to_i).to_s.split[3]
    end
    end
    end

    time = "00:11:20"
    add5second = "+5"
    sub50second = "-50"

    puts modifytime(time,add5second) => "00:11:25"
    puts modifytime(time,sub50second) => "00:10:30"

    The result is what I expected but I think the modifytime is ugly.
    Can I use 'eval' so that I don't need to judge "+" or "-"?
    Or, any elegant solutions?
    Thanks in advance.
    --
    Posted via http://www.ruby-forum.com/.
     
    Ak 756, Jan 15, 2008
    #1
    1. Advertising

  2. Hi,

    At Tue, 15 Jan 2008 11:56:04 +0900,
    Ak 756 wrote in [ruby-talk:287454]:
    > The result is what I expected but I think the modifytime is ugly.
    > Can I use 'eval' so that I don't need to judge "+" or "-"?
    > Or, any elegant solutions?


    "+5".to_i == 5
    "-50".to_i == -50

    (Time.parse("00:11:20")+"+5".to_i).strftime("%T") # => "00:11:25"

    --
    Nobu Nakada
     
    Nobuyoshi Nakada, Jan 15, 2008
    #2
    1. Advertising

  3. On Jan 15, 2008 11:56 AM, Ak 756 <> wrote:
    > Hi
    >
    > I hava a string indicate a hh/mm/ss time and some other strings indicat
    > operations on the string. For example , string "+5" means add 5 second.
    >
    > Here is my code:
    >
    > def modifytime(s1,s2)
    > if s2[0,1] == "+"
    > (Time.parse(s1)+s2[1,s2.length-1].to_i).to_s.split[3]
    > else if s2[0,1] == "-"
    > (Time.parse(s1)-s2[1,s2.length-1].to_i).to_s.split[3]
    > end
    > end
    > end
    >
    > time = "00:11:20"
    > add5second = "+5"
    > sub50second = "-50"
    >
    > puts modifytime(time,add5second) => "00:11:25"
    > puts modifytime(time,sub50second) => "00:10:30"
    >
    > The result is what I expected but I think the modifytime is ugly.
    > Can I use 'eval' so that I don't need to judge "+" or "-"?
    > Or, any elegant solutions?
    > Thanks in advance.


    http://p.ramaze.net/191

    ^ manveru
     
    Michael Fellinger, Jan 15, 2008
    #3
  4. Ak 756

    Ak 756 Guest

    Michael Fellinger wrote:
    > On Jan 15, 2008 11:56 AM, Ak 756 <> wrote:
    >> else if s2[0,1] == "-"
    >> puts modifytime(time,sub50second) => "00:10:30"
    >>
    >> The result is what I expected but I think the modifytime is ugly.
    >> Can I use 'eval' so that I don't need to judge "+" or "-"?
    >> Or, any elegant solutions?
    >> Thanks in advance.

    >
    > http://p.ramaze.net/191
    >
    > ^ manveru


    Cool website, cool solution. Thanks very much :)
    --
    Posted via http://www.ruby-forum.com/.
     
    Ak 756, Jan 15, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eric Newton
    Replies:
    3
    Views:
    9,430
    Brock Allen
    Apr 4, 2005
  2. DataBinder.Eval and Eval.

    , Jun 16, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    552
    Karl Seguin [MVP]
    Jun 16, 2006
  3. Alex van der Spek

    eval('07') works, eval('08') fails, why?

    Alex van der Spek, Jan 8, 2009, in forum: Python
    Replies:
    6
    Views:
    1,469
    Bruno Desthuilliers
    Jan 8, 2009
  4. Liang Wang
    Replies:
    8
    Views:
    137
    Ben Morrow
    Feb 2, 2008
  5. Marc Girod

    to eval or not to eval?

    Marc Girod, Apr 19, 2011, in forum: Perl Misc
    Replies:
    2
    Views:
    161
    Marc Girod
    Apr 19, 2011
Loading...

Share This Page