Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Not a request, just didn't want to post in Script Submission.

I posted a method to return if a number is odd or not a while ago somewhere, and it got butch... *cough* enhanced :D and I learned a few things.

Now I once again post an almost useless method, except now it works with any number of arguements and will return the result in an array unless there is only one number .... or none. It will also allow any number of arrays and any combination of arrays and singular numbers.

If you use an array as an arguement, then the result will be in -

local_variable[index_of_array,index_of_result]

Here's the method -

Code:
def odd?(*numbers)
   if *numbers.size == 1 and !*numbers[0].is_a?(Array)
      result = (*numbers[0] % 2 = 1)
   elsif *numbers.size == 1 and *numbers[0].is_a?(Array)
      for i in 0...*numbers[0].size
         result[i] = (*numbers[0,i] % 2 = 1)
      end
   elsif *numbers.size == 0
      return p "No numbers provided for 'odd?' method."
   else
      for i in 0...(*numbers.size)
         if *numbers[i].is_a(Array)
            for j in 0...(*numbers[i].size)
               result[i,j] = (*numbers[i,j] % 2 = 1)
            end
         else
            result[i] = (*numbers[i] % 2 = 1)
         end
      end
   end
   return result
end

To use make sure you allays use a variable of some sort.

IE:

number_odd = odd?(some_array,3,2,89,another_array)

EDIT : Realized a small error, fixed now.
 
Code:
def odd?(num)
	case int.class.to_s
	when 'Fixnum'
		return unless (num % 2) == 0
	when 'Array'
		amt = 0
		for i in 0..num.size - 1
			amt + 1 unless (num[i] % 2) == 0
		end
		return amt
	end
end
If used on a fixnum returns if it's odd or not, if used on array returns how many are odd. Don't think it works, never tested it.
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top