Grosse MàJ
This commit is contained in:
27
P5B/ruby/exercices/GtkBase.rb
Normal file
27
P5B/ruby/exercices/GtkBase.rb
Normal file
@ -0,0 +1,27 @@
|
||||
require 'gtk2'
|
||||
|
||||
|
||||
#gtk interface
|
||||
class Gui
|
||||
|
||||
def choice_radio
|
||||
|
||||
Gtk.init
|
||||
window = Gtk::Window.new
|
||||
|
||||
window.set_title('lecteur de webradio Gtk')
|
||||
window.set_default_size(200,100)
|
||||
|
||||
window.show_all
|
||||
Gtk.main
|
||||
end
|
||||
|
||||
def initialize
|
||||
configuration = Conf.new
|
||||
@radios = configuration.read
|
||||
self.choice_radio
|
||||
print @radios
|
||||
end
|
||||
end
|
||||
|
||||
Gui.new #launch interface
|
55
P5B/ruby/exercices/class_Carre.rb
Normal file
55
P5B/ruby/exercices/class_Carre.rb
Normal file
@ -0,0 +1,55 @@
|
||||
module Inject
|
||||
def inject(val)
|
||||
each do | element |
|
||||
val = yield(val, element)
|
||||
end
|
||||
val
|
||||
end
|
||||
|
||||
def somme
|
||||
inject(0) do | s, element |
|
||||
s + element
|
||||
end
|
||||
end
|
||||
|
||||
def produit
|
||||
inject(1) do | p, element |
|
||||
p * element
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class Impairs
|
||||
include Inject
|
||||
def initialize(n)
|
||||
@cardinal = n
|
||||
end
|
||||
|
||||
def each
|
||||
i = 1
|
||||
@cardinal.times do
|
||||
yield i
|
||||
i = i +2
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class Integer
|
||||
def premiers_impairs
|
||||
Impairs.new(self)
|
||||
end
|
||||
end
|
||||
|
||||
def carre(n)
|
||||
n.premiers_impairs.somme
|
||||
end
|
||||
|
||||
3.premiers_impairs.each do | i |
|
||||
puts i.to_s
|
||||
end
|
||||
|
||||
puts "Somme : #{3.premiers_impairs.somme}"
|
||||
puts "Produit : #{3.premiers_impairs.produit}"
|
||||
|
||||
puts "Carré de 3 : #{carre(3)}"
|
||||
|
18
P5B/ruby/exercices/class_Impair.rb
Normal file
18
P5B/ruby/exercices/class_Impair.rb
Normal file
@ -0,0 +1,18 @@
|
||||
class Impairs
|
||||
def initialize(n)
|
||||
@cardinal = n
|
||||
end
|
||||
|
||||
def each
|
||||
i = 1
|
||||
@cardinal.times do
|
||||
yield i
|
||||
i = i + 2
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Impairs.new(3).each do | i |
|
||||
puts i.to_s
|
||||
end
|
||||
|
48
P5B/ruby/exercices/class_Inject.rb
Normal file
48
P5B/ruby/exercices/class_Inject.rb
Normal file
@ -0,0 +1,48 @@
|
||||
module Inject
|
||||
def inject(val)
|
||||
each do | element |
|
||||
val = yield(val, element)
|
||||
end
|
||||
val
|
||||
end
|
||||
|
||||
def somme
|
||||
inject(0) do | s, element |
|
||||
s + element
|
||||
end
|
||||
end
|
||||
|
||||
def produit
|
||||
inject(1) do | p, element |
|
||||
p * element
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class Impairs
|
||||
include Inject
|
||||
def initialize(n)
|
||||
@cardinal = n
|
||||
end
|
||||
|
||||
def each
|
||||
i = 1
|
||||
@cardinal.times do
|
||||
yield i
|
||||
i = i +2
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class Integer
|
||||
def premiers_impairs
|
||||
Impairs.new(self)
|
||||
end
|
||||
end
|
||||
|
||||
3.premiers_impairs.each do | i |
|
||||
puts i.to_s
|
||||
end
|
||||
|
||||
puts "Somme : #{3.premiers_impairs.somme}"
|
||||
puts "Produit : #{3.premiers_impairs.produit}"
|
24
P5B/ruby/exercices/class_Integer.rb
Normal file
24
P5B/ruby/exercices/class_Integer.rb
Normal file
@ -0,0 +1,24 @@
|
||||
class Impairs
|
||||
def initialize(n)
|
||||
@cardinal = n
|
||||
end
|
||||
|
||||
def each
|
||||
i = 1
|
||||
@cardinal.times do
|
||||
yield i
|
||||
i = i +2
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class Integer
|
||||
def premiers_impairs
|
||||
Impairs.new(self)
|
||||
end
|
||||
end
|
||||
|
||||
3.premiers_impairs.each do | i |
|
||||
puts i.to_s
|
||||
end
|
||||
|
16
P5B/ruby/exercices/fact_iterareur.rb
Normal file
16
P5B/ruby/exercices/fact_iterareur.rb
Normal file
@ -0,0 +1,16 @@
|
||||
def each_fact(n)
|
||||
res = 1
|
||||
for i in 0 .. n
|
||||
res = res * i unless i == 0
|
||||
yield(res)
|
||||
end
|
||||
res
|
||||
end
|
||||
|
||||
res = 0
|
||||
|
||||
each_fact(10) do | i |
|
||||
res = res + i
|
||||
end
|
||||
|
||||
puts res
|
16
P5B/ruby/exercices/factorielle.rb
Normal file
16
P5B/ruby/exercices/factorielle.rb
Normal file
@ -0,0 +1,16 @@
|
||||
def fact(n)
|
||||
if n == 0
|
||||
then
|
||||
1
|
||||
else
|
||||
res = 1
|
||||
i = n
|
||||
until i == 0
|
||||
res = res * i
|
||||
i = i - 1
|
||||
end
|
||||
res
|
||||
end
|
||||
end
|
||||
|
||||
puts fact(5)
|
Reference in New Issue
Block a user