# Ruby function

**URL:** <https://forums.sketchup.com/t/ruby-function/106711>\
**Category:** Ruby API\
**Tags:** advice\
**Created:** [October 18, 2019, 8:39am UTC](https://forums.sketchup.com/t/ruby-function/106711 "2019-10-18T08:39:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![skyfire](https://avatars.discourse-cdn.com/v4/letter/s/6a8cbe/32.png) [@skyfire](https://forums.sketchup.com/u/skyfire)\
**Post date:** [October 18, 2019, 8:39am UTC](https://forums.sketchup.com/t/ruby-function/106711/1 "2019-10-18T08:39:08Z")

</div>

I set up a function that extracts the values from the 4-dimensional array by changing the Y value, and I put the values into a new array, the array should be [0,16/3,0,4/3],but the array is not what I want, and I want to know what’s wrong with my code

```ruby
# 
edge=[[0,1,1,1],[3,-14,4,1],[0,6,4,4],[3,-2,4,1]]
num=4
Infinity=1.0/0
def get_point(y,num,edge)
  k=0
  b=1
  y_max=2
  y_min=3
  x=[]
  num.times do |i|
    if (y<=edge[i][y_max])&&(y>=edge[i][y_min])
      if edge[i][k]==Infinity
        x[i]=edge[i][k]
      elsif edge[i][k]==0
        x[i]=edge[i][b]  
      else
        x[i]=(y-edge[i][b])/edge[i][k]
      end
    else 
     x[i]=0
    end
  end
  p x
end
get_point(2,num,edge)

```

---

<div class="post-metadata">

**Author:** ![sWilliams](https://avatars.discourse-cdn.com/v4/letter/s/838e76/32.png) [@sWilliams](https://forums.sketchup.com/u/sWilliams)\
**Post date:** [October 18, 2019, 2:06pm UTC](https://forums.sketchup.com/t/ruby-function/106711/2 "2019-10-18T14:06:16Z")

</div>

You were using integer math.

```ruby
#
edge=[[0.0,1.0,1.0,1.0],[3.0,-14.0,4.0,1.0],[0.0,6.0,4.0,4.0],[3.0,-2.0,4.0,1.0]]
num=4
Infinity=1.0/0

def get_point(y,num,edge)
  k=0
  b=1
  y_max=2
  y_min=3
  x=[]
  
  num.times do |i|
    if (y <= edge[i][y_max])&&(y >= edge[i][y_min])

      if edge[i][k]==Infinity
        x[i]=edge[i][k]

      elsif edge[i][k]==0
        x[i]=edge[i][b]  

      else
        x[i]=Rational(y-edge[i][b], edge[i][k])
      end
      
      
    else 
     x[i]=0
    end
  end
  return x
end

result = get_point(2,num,edge)

```

---

<div class="post-metadata">

**Author:** ![Aerilius](https://avatars.discourse-cdn.com/v4/letter/a/e19b73/32.png) [@Aerilius](https://forums.sketchup.com/u/Aerilius)\
**Post date:** [October 18, 2019, 6:49pm UTC](https://forums.sketchup.com/t/ruby-function/106711/3 "2019-10-18T18:49:07Z")

</div>

To expand on sWilliam’s excellent answer,  
Ruby is a dynamically typed language, every data object has a type even if you have not _explicitely_ specified whether to use an [Integer](https://ruby-doc.org/core-2.6.5/Integer.html) of a [Float](https://ruby-doc.org/core-2.6.5/Float.html). It can be confusing that `1.0` and `1` are shorthand literals for `Float(1.0)` and `Integer(1)`.

> [@skyfire](#):
>
> (y-edge[i][b])/edge[i][k]

If you cannot rely on that the input is correctly typed (e.g. input provided by someone else), and if you don’t want to convert and sanitize the input first (like `edge = edge.map{ |list| list.map{ |n| n.to_f }}`), you can at least force the quotient to be a float (`edge[i][k].` **`to_f`** ). This should avoid errors of integer division in place of floating point division.

> [@sWilliams](#):
>
> Rational(y-edge[i][b], edge[i][k])

The [Rational](https://ruby-doc.org/core-2.6.5/Rational.html) class allows to do accurate math with _fractional_ (rational) numbers and avoid [accuracy problems of floating point math](https://en.wikipedia.org/wiki/Floating-point_arithmetic#Accuracy_problems). Use it if you either want to display results later again in fractional form in the user interface or if you need so high accuracy that `(1/10.0)**2` cannot be treated (almost) equal to `0.010000000000000002`.

---

<div class="post-metadata">

**Author:** ![skyfire](https://avatars.discourse-cdn.com/v4/letter/s/6a8cbe/32.png) [@skyfire](https://forums.sketchup.com/u/skyfire)\
**Post date:** [October 19, 2019, 6:00am UTC](https://forums.sketchup.com/t/ruby-function/106711/4 "2019-10-19T06:00:21Z")

</div>

Thank you for your wonderful comments. I have solved the problem through your Suggestions
