Skip to content

Instantly share code, notes, and snippets.

@DamirSvrtan
Forked from shime/prijemni.rb
Created February 26, 2012 08:58
Show Gist options
  • Save DamirSvrtan/1915441 to your computer and use it in GitHub Desktop.
Save DamirSvrtan/1915441 to your computer and use it in GitHub Desktop.
### 1. Create user class by implementing
### a. properties
### i. user_id
### ii. username
### iii. password
### iv. email
### b. methods
### i. greet (learn user to say "Hi! My name is <name>, nice to meet you."
###
### 2. Create recursive function that returns result for x^y
def pot(x,y)
y==1 ? x : x*pot(x,y-1)
end
### 3. Write a program that prints the numbers from 1 to 100. But for multiples of three
### print “Fizz” instead of the number and for the multiples of five print “Buzz”. For
### numbers which are multiples of both three and five print “FizzBuzz”.
(1..100).each do |x|
if x%3==0 && x%5==0
print "Fizzbuzz\n"
elsif x%3==0
print "Fizz\n"
elsif x%5==0
print "Buzz\n"
else print "#{x}\n"
end
end
### da, mogao sam upotrjebit puts, znam :))
### 4. Find the largest integer value in array (all values in array are integers)
a=[5,4,3,1]
a.max #=>5
@shime
Copy link

shime commented Feb 27, 2012

what seems to be the problem with the first one, young minion?

@shime
Copy link

shime commented Feb 27, 2012

nice job for the second, but check my shit out for the third.

class Fixnum
  def divisible_by?(args*)
      args.each do |arg|
          return false unless self % arg == 0 rescue return false
      end
  true
  end
end

I present you DIVISABLE_BY?

Check this shit out:

2.divisable_by? 2 #=> true
2.divisable_by? 0 #=> false
21.divisable_by? 21,3,7 #=> true
21.divisable_by? 21,0,2 #=> false

So now you can use x.divisable_by? 3,5 in your code. 🍺

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment