Skip to content

Instantly share code, notes, and snippets.

@dom96
Created August 5, 2012 09:59
Show Gist options
  • Save dom96/3263560 to your computer and use it in GitHub Desktop.
Save dom96/3263560 to your computer and use it in GitHub Desktop.
type
TPerson = object # I prepended a T because that is the Nimrod convention. It also gets rid of possible clashes, like in `$` :)
name*: string
age*: int
proc newPerson(name: string, age: int): TPerson =
result.name = name
result.age = age
proc `<=>`(person1: TPerson, person2: TPerson): bool =
return person1.age <=> person2.age
proc `$`(person: TPerson): string =
return person.name & " " & person.age
var group: seq[TPerson] = @[
newPerson("Bob", 33),
newPerson("Chris", 16),
newPerson("Ash", 23)
]
# Won't bother with sorting. :P
class Person
attr_reader :name, :age
def initialize(name, age)
@name, @age = name, age
end
def <=>(person) # Comparison operator for sorting
age <=> person.age
end
def to_s
"#{name} (#{age})"
end
end
group = [
Person.new("Bob", 33),
Person.new("Chris", 16),
Person.new("Ash", 23)
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment