
In my new job, it seems I'll be writing ruby (on rails).
I've been trying the language in my spare time, and it seems a fairly good compromise between Perl and Smalltalk.
Except a couple of features or idioms I saw, everything else was quite intuitive or easy to understand (at least coming from Perl/Smalltalk).
+ Sigils do exist, but denote scope oposed to type.
+ __DATA__
+ '=' can be part of a method name. And there's some magic involved there. def method=(attr) can be used as an assignment (sintactically) as parens are optional.
+ Integer class can be expanded or subclassed.
Here are the codes I wrote to grasp the language. These are the usual examples I write in every language I try to learn:
- a program to center paragraphs.
- guessing number
- Network with hosts that pass messages one to the other.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
#http://perl.plover.com/qotw/r/001 | |
def center(arr) | |
max= arr.max{ |a,b| a.length <=> b.length }.length | |
arr.each do |e| | |
puts " " * ((max-e.length)/2) + e | |
end | |
end | |
center(STDIN.read.split("\n")) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
#number guessing game | |
class Guess | |
def initialize() | |
@sol = 1 + rand(100); | |
puts "la sol es #@sol" #yeah, cheating | |
end | |
def try(my_guess) | |
if Integer(my_guess) == @sol | |
puts "bien!" | |
return true; | |
else | |
puts "Too "+ (Integer(my_guess) < @sol ? "small" : "big") | |
return false; | |
end | |
end | |
end | |
a = Guess.new | |
my_guess= STDIN.readline.chomp | |
while not a.try(my_guess) | |
my_guess= STDIN.readline.chomp | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
class Network | |
def initialize | |
@hosts = Array.new() | |
end | |
def attach(node1, node2) | |
node1.next = node2 | |
end | |
def add_new_node(nodeName) | |
add Host.new(nodeName) | |
end | |
def add(node) | |
@hosts.push(node) | |
attach(@hosts[-2],@hosts[-1]) if @hosts.length > 1 | |
attach(@hosts[-1],@hosts[0]) | |
end | |
def to_s | |
@hosts.each do |e| puts e end | |
end | |
end | |
class Host | |
attr_accessor :next , :name | |
def initialize(name) | |
@name = name | |
@next = nil | |
end | |
def pass_message(message) | |
puts "a #{message.to}" | |
puts "jo soc #{self.name}" | |
if message.to.eql?(self.name) | |
puts "he rebut el missatge #{message.text}" | |
else | |
@next.pass_message(message) | |
end | |
end | |
def to_s() | |
"I'm #{@name} and I'm connected to #{@next ? @next.name : 'noone'} " | |
end | |
end | |
class Printer < Host | |
end | |
class Workstation < Host | |
def send(message_text,to) | |
m = Message.new(message_text,to) | |
self.pass_message(m) | |
end | |
end | |
class Message | |
attr_accessor :text, :to | |
def initialize(text,to) | |
@text=text | |
@to=to | |
end | |
end | |
n = Network.new | |
n.add_new_node('first') | |
n.add_new_node('second') | |
n.add(Printer.new('printer')) | |
ws = Workstation.new('ws') | |
n.add(ws) | |
puts n | |
ws.send("hola",'second') |
Here is a link that I found useful about ruby patterns and idioms.
http://scriptlandia.blogspot.com/2009/02/design-patterns-in-ruby.html