[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Newbie ahead!: Ruby-Demo

Meino Christian Cramer

1/20/2006 4:15:00 PM

6 Answers

Bill Welch

1/20/2006 4:25:00 PM

0

Try running the tk demo, widget. It shows GUI screens and has buttons
to show the code that made the examples work.

Bill Welch

1/20/2006 4:26:00 PM

0

Should have mentioned where the widget is:

C:\ruby\samples\tk\demos-en\widget.rbw

Amr Malik

1/20/2006 4:52:00 PM

0

Meino Christian Cramer wrote:
> Hi,
>
> can someone point me to a ruby demo-script?
>
> I want to show a friend the beauty of ruby and am myself fear if
> would try my best would acchieve the opposite of what I want.
>
> It dont need to do anything sophisticated it only should show the
> Pros of programming in ruby...
>
> Thanks a lot in advance for any help!
> Ruby!
> mcc

You might want to try:

http://tryruby....

HTH,

Amr

--
Posted via http://www.ruby-....


Jules

1/20/2006 5:01:00 PM

0

Show them: blocks, OOP, "the Ruby way".

(0..10).map{|x| x*x}.select{|x| x < 50}

Take a Java program and translate it to ruby step-by-step by stripping
things ;-)

-----

class Person
def initialize(newName)
@name = newName
end

def getName()
return(@name);
end

def setName(newName)
@name = newName;
end
end

=>

class Person
def initialize(new_name)
@name = new_name
end

def name
@name
end

def name=(new_name)
@name = new_name
end
end

=>

class Person
attr_accessor :name
end

=>

Person = Struct.new(:name)

...

people = [Person.new('David'), Person.new('Amy'), Person.new('Paul')]
people.sort_by{|p| p.name}

...

# Struct.new returns a class, so we can subclass it :)

class Person < Struct.new(:name)
include Comparable

def <=> other
@name <=> other.name
end
end

people = [Person.new('David'), Person.new('Amy'), Person.new('Paul')]

# A person has comparison methods now. > == < <= >= etc. can be used
now.
# The people Array has includes?, max, min etc.
# Everything just by adding include Comparable and def <=>.

if people[0] < people[1] && people.includes? Person.new('David')
people.sort!
end

-----

All untested.

Jules

--
Posted via http://www.ruby-....


matthew.moss.coder

1/20/2006 5:28:00 PM

0

> people = [Person.new('David'), Person.new('Amy'), Person.new('Paul')]

people = %w{David Amy Paul}.map { |name| Person.new(name) }


cadience

1/21/2006 5:15:00 PM

0

Here is a class that I just whipped up while I am also learning (just
grabbed the pickaxe book about an hour ago). This code was inspired by
the fib_up_tp(max) function on page 50. While I can't claim it takes
full advantage of Ruby''s power it nonetheless demonstrates many
facets of the language.

Imagine doing this in C! Even C++ this would be less "clean".

If anyone has any suggestions on taking more advantage of Ruby in the
code below, let me know! Of course, Fibs should inherit an array, and
so fourth (hey, I'm only on page fifty :-p ) , but I believe this gives
a great first look at the power of Ruby's semantics to someone curious
about Ruby.

class Fibs
@foundFibs
def initialize
@foundFibs = Array.new(2, 1)
end
def lastIndex
@foundFibs.size - 1
end
def nextFib
@foundFibs.push(@foundFibs[-2] + @foundFibs[-1])
self.largestFib
end
def largestFib
@foundFibs.last
end
def up_to_num(max)
self.nextFib while @foundFibs.last < max
@foundFibs.find_all{|x| x <= max}
end
def up_to_index(index)
(self.lastIndex + 1).upto(index) {self.nextFib} if index >
self.lastIndex
@foundFibs.first(index + 1)
end
def to_s
s = "["
@foundFibs.each {|name| s += name.to_s + ", "}
s[0..s.size-3] + "]"
end
def length
@foundFibs.length
end
def [](index)
self.up_to_index(index) if index > self.lastIndex
@foundFibs[index]
end
end