[RESOLVED]Limiting values in RUBY

Started by Fantasist, November 14, 2008, 02:07:20 pm

Previous topic - Next topic

Fantasist

November 14, 2008, 02:07:20 pm Last Edit: November 15, 2008, 08:46:33 am by Fantasist
I need the Ruby code for the following pseudo-code.


def bind(variable_pointer, min, max)
  if given_var < min
    given_var = min
  elsif given_var > max
    given_var = max
  end
end


Intended application:


a = 1000
b = 1

bind(a, 200, 800)
p a # Output => 800

bind(b, 200, 800)
p b # Output => 200


Any tips? :)
Do you like ambient/electronic music? Then you should promote a talented artist! Help out here. (I'm serious. Just listen to his work at least!)


The best of freeware reviews: Gizmo's Freeware Reviews




Blizzard

Try with * in the argument definition of the method like "def bind(*variable_pointer, min, max)".
Basically in Ruby there are Reference Type and Value Type variables. The former are always passed as references because they are objects while the latter are always passed by value. If I am not wrong, in Ruby the value types are Numerics, Strings and some other which I don't know from memory right now. Check my e-book, I explained it there.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

Fantasist

QuoteTry with * in the argument definition of the method like "def bind(*variable_pointer, min, max)".

Tried doing that, gives me a syntax error. And I did some superficial reading and *args is used to pass multiple arguments. So "def bind(*args)" would work, but not "def bind(*args, arg2)".

QuoteBasically in Ruby there are Reference Type and Value Type variables.

By Valut Types, do you mean 'literals'?

QuoteIf I am not wrong, in Ruby the value types are Numerics, Strings and some other which I don't know from memory right now. Check my e-book, I explained it there.

I will.

So you know why I want this, right? I did some testing and using [[a, 1].max, 99].min is WAY slower than using conditional branches. I wrote a method and tested some operations. When the "method" way was faster than the "direct" way, I took a closer lok and thus I found out I don't know how to pass pointers.
Anyway, I tried this in the Numeric class:

class Numeric

  def bind(min, max)
    if self < min
      self = min
    elsif self > max
      self = max
    end
  end

end


I got a syntax error at "self = min" line...
Do you like ambient/electronic music? Then you should promote a talented artist! Help out here. (I'm serious. Just listen to his work at least!)


The best of freeware reviews: Gizmo's Freeware Reviews




Blizzard

November 15, 2008, 08:38:35 am #3 Last Edit: November 15, 2008, 08:40:48 am by Blizzard
Quote from: Fantasist on November 15, 2008, 08:29:52 am
QuoteTry with * in the argument definition of the method like "def bind(*variable_pointer, min, max)".

Tried doing that, gives me a syntax error. And I did some superficial reading and *args is used to pass multiple arguments. So "def bind(*args)" would work, but not "def bind(*args, arg2)".


._. *failed*

Quote from: Fantasist on November 15, 2008, 08:29:52 am
QuoteBasically in Ruby there are Reference Type and Value Type variables.

By Valut Types, do you mean 'literals'?


Yeah, basically the same thing.

Quote from: Fantasist on November 15, 2008, 08:29:52 am
So you know why I want this, right? I did some testing and using [[a, 1].max, 99].min is WAY slower than using conditional branches.


I know. I use if conditions as well when there are only 2 to 3 values to compare or when I need to limit the value.

Quote from: Fantasist on November 15, 2008, 08:29:52 am
I wrote a method and tested some operations. When the "method" way was faster than the "direct" way, I took a closer lok and thus I found out I don't know how to pass pointers.
Anyway, I tried this in the Numeric class:

class Numeric

  def bind(min, max)
    if self < min
      self = min
    elsif self > max
      self = max
    end
  end

end


I got a syntax error at "self = min" line...


Because it's a literal. Just use this. It's a cleaner calling code anyway.


class Numeric

  def limit(min, max)
    return self if min > max
    return min if self < min
    return max if self > max
    return self
  end

end

a = 1000
a = a.limit(200, 800)

Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

Fantasist

That's just perfect, and you're awesome! *powers up*
Do you like ambient/electronic music? Then you should promote a talented artist! Help out here. (I'm serious. Just listen to his work at least!)


The best of freeware reviews: Gizmo's Freeware Reviews