Chaos Project

General => Electronic and Computer Section => Programming / Scripting / Web => Topic started by: Fantasist on November 14, 2008, 02:07:20 pm

Title: [RESOLVED]Limiting values in RUBY
Post by: Fantasist on November 14, 2008, 02:07:20 pm
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? :)
Title: Re: Using variable pointers in Ruby?
Post by: Blizzard on November 15, 2008, 08:02:26 am
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.
Title: Re: Using variable pointers in Ruby?
Post by: 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)".

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...
Title: Re: Using variable pointers in Ruby?
Post by: Blizzard on November 15, 2008, 08:38:35 am
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)

Title: Re: Using variable pointers in Ruby?
Post by: Fantasist on November 15, 2008, 08:44:57 am
That's just perfect, and you're awesome! *powers up*