Learning the "Ruby way"

Hobby-programmer alarm

Hello!
I am trying to learn Ruby, but with the goal of understanding new
elegant solutions, not to repeat the same messy solutions I usually
come up with.
Here is an example I would like to use to improve my understanding.
Any pointers would be great.

Problem:
I have a sorted list (‘alfred’, ‘boris’, ‘bruce’, ‘claire’, ‘dean’, ‘donald’)
and would like to generate or return (’-a-’, ‘alfred’, ‘-b-’, ‘boris’,
‘bruce’, ‘-c-’, ‘claire’, ‘-d-’, ‘dean’, ‘donald’).

Here’s my solution:
lst = [‘alfred’, ‘boris’, ‘bruce’, ‘claire’, ‘dean’, ‘donald’]
prev = ''
newlst = Array.new
lst.each { |cur|
if cur[0…0] != prev
newlst.push("-#{cur[0…0]}-")
end
newlst.push(cur)
prev = cur[0…0] }

Is this the “Ruby way” of solving this problem? I doubt it, defining
"helper variables" like this is what had to be done in Basic, and my
solution doesn’t demonstrate the high degree of readability Ruby is often
praised for.

Any useful suggestions for me? I’d be very grateful!
Mark

I’m sure there are many ways of doing this, but here’s one:

lst = [‘alfred’, ‘boris’, ‘bruce’, ‘claire’, ‘dean’, ‘donald’]

frob = lst.map do |val|
["-#{val[0…0]}-", val]
end.flatten.uniq

p frob

I’m not sure if #uniq is defined to leave the first occurrence and
delete all the rest or not,
but that’s what it currently does, so it works.

  • Dan

My first thought was to collect the values into a hash

{ ‘a’ => [‘alfred’],
‘d’ => [‘dean’, ‘donald’],
… }

then render that hash into the array that you want.

It’s unlikely to be all that efficient, but both steps are very easy.

Cheers,
Gavin

···

On Thursday, November 20, 2003, 8:07:14 PM, Mark wrote:

[…]

Problem:
I have a sorted list (‘alfred’, ‘boris’, ‘bruce’, ‘claire’, ‘dean’, ‘donald’)
and would like to generate or return (‘-a-’, ‘alfred’, ‘-b-’, ‘boris’,
‘bruce’, ‘-c-’, ‘claire’, ‘-d-’, ‘dean’, ‘donald’).

Here’s my solution:
[…]

Is this the “Ruby way” of solving this problem? I doubt it, defining
“helper variables” like this is what had to be done in Basic, and my
solution doesn’t demonstrate the high degree of readability Ruby is often
praised for.

Any useful suggestions for me? I’d be very grateful!
Mark

One more way to do it…

require ‘enum/cluster’ # Enumerable tools

lst = [‘alfred’, ‘boris’, ‘bruce’, ‘claire’, ‘dean’, ‘donald’]

a = [“–#{lst[0][0…0]}–”]
lst.each_cluster(2) {|i, j|
a << i
a << “–#{j[0…0]}–” if i[0] != j[0]
}

p a

martin

···

Mark Wirdnam mark.wirdnam@stud.unibas.ch wrote:

Problem:
I have a sorted list (‘alfred’, ‘boris’, ‘bruce’, ‘claire’, ‘dean’, ‘donald’)
and would like to generate or return (‘-a-’, ‘alfred’, ‘-b-’, ‘boris’,
‘bruce’, ‘-c-’, ‘claire’, ‘-d-’, ‘dean’, ‘donald’).

Is this the “Ruby way” of solving this problem? I doubt it, defining
“helper variables” like this is what had to be done in Basic, and my
solution doesn’t demonstrate the high degree of readability Ruby is often
praised for.

Any useful suggestions for me? I’d be very grateful!
Mark

How about this dirty solution ?

ruby a.rb
[“-a-”, “alfred”, “-b-”, “boris”, “bruce”, “-c-”, “claire”, “-d-”, “dean”, “donald”]
expand a.rb
names = %w(alfred boris bruce claire dean donald)
letters = names.map{|name| n=name[0…0]; [n, “-#{n}-”]}.uniq
p (letters + names.zip(names)).sort.transpose[1]

···

On Thu, 20 Nov 2003 01:06:25 -0800, Mark Wirdnam wrote:


Simon Strandgaard

Hi –

Hobby-programmer alarm

Hello!
I am trying to learn Ruby, but with the goal of understanding new
elegant solutions, not to repeat the same messy solutions I usually
come up with.
Here is an example I would like to use to improve my understanding.
Any pointers would be great.

Problem:
I have a sorted list (‘alfred’, ‘boris’, ‘bruce’, ‘claire’, ‘dean’, ‘donald’)
and would like to generate or return (‘-a-’, ‘alfred’, ‘-b-’, ‘boris’,
‘bruce’, ‘-c-’, ‘claire’, ‘-d-’, ‘dean’, ‘donald’).

This may be a bit ill-advised, because it changes the array as it iterates
through it (though in such a way that I think it can actually be shown
to be safe – ? ). Anyway, just for fun:

a =[‘alfred’, ‘boris’, ‘bruce’, ‘claire’, ‘dean’, ‘donald’]
a.map {|s| s[0]}.uniq.each {|c|
a.insert(a.index(a.detect {|t| t[0] == c}), “-#{c.chr}-”)
}
a # => [“-a-”, “alfred”, “-b-”, “boris”, “bruce”, “-c-”, “claire”, “-d-”, “dean”, “donald”]

David

···

On Thu, 20 Nov 2003, Mark Wirdnam wrote:


David Alan Black
home: dblack@wobblini.net # New email address
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

Hobby-programmer alarm

···

On Thu, 20 Nov 2003, Mark Wirdnam wrote:

May excellent suggestions have been made, so I won’t bore you with my
attempt. But, I wanted to say, “Welcome Hobby Programmer!”

I’m paid to do IT work, but I have increasingly moved my programming
activities into the land of hobby-dom, where I tend to enjoy them more. I
find Ruby to be an excellent language for hobbyists (as well as
enterprise users).

Are there other strict hobbyists on the list?

Chad

ok, I’m not quite there yet, but your helpful examples have given me
an idea how to go about things in Ruby. I think I see a main theme
being to use the power of the pre-defined methods of the
array/string/whatever.
Thanks for your help!

Mark

“Dan Doel” djd15@po.cwru.edu schrieb im Newsbeitrag
news:3FBC87DA.1090803@po.cwru.edu…

I’m sure there are many ways of doing this, but here’s one:

lst = [‘alfred’, ‘boris’, ‘bruce’, ‘claire’, ‘dean’, ‘donald’]

frob = lst.map do |val|
[“-#{val[0…0]}-”, val]
end.flatten.uniq

p frob

I’m not sure if #uniq is defined to leave the first occurrence and
delete all the rest or not,
but that’s what it currently does, so it works.

There must be an inject way of doing this…

lst = [‘alfred’, ‘boris’, ‘bruce’, ‘claire’, ‘dean’, ‘donald’]

frob = lst.inject( [, nil] ) do |(res, key), word|
k = “-#{word[0].chr}-”
res << k unless k == key
res << word
[res, k]
end[0]

But this really cries for a Hash, because we have a typical key value
relationship here:

lst = [‘alfred’, ‘boris’, ‘bruce’, ‘claire’, ‘dean’, ‘donald’]

lst.inject( Hash.new {|h,k| h[k]=} ) {|res,w| res[“-#{w[0].chr}-”] << w;
res}.sort.flatten

In fact, I’d just return the Hash. Then you have the appropriate data
structur. Simply omit “.sort.flatten” from the line above.

Regards

robert

Hi –

···

On Fri, 21 Nov 2003 dblack@wobblini.net wrote:

This may be a bit ill-advised, because it changes the array as it iterates

Just to clarify: “This” means my solution, not your question :slight_smile:

David


David Alan Black
home: dblack@wobblini.net # New email address
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

Hi –

···

On Fri, 21 Nov 2003, Chad Fowler wrote:

On Thu, 20 Nov 2003, Mark Wirdnam wrote:

Hobby-programmer alarm

May excellent suggestions have been made, so I won’t bore you with my
attempt. But, I wanted to say, “Welcome Hobby Programmer!”

I’m paid to do IT work, but I have increasingly moved my programming
activities into the land of hobby-dom, where I tend to enjoy them more. I
find Ruby to be an excellent language for hobbyists (as well as
enterprise users).

Are there other strict hobbyists on the list?

How strict is strict? :slight_smile:

David


David Alan Black
home: dblack@wobblini.net # New email address
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

I am a programming hobbyist I suppose.

I’m the Manager of the IS Group at work and I generally stick to networking,
server maintenance/configuration(s), etc…but I have moved into using
Ruby( as well as other languages, but Ruby is the most recent addition ) to
make my duties at the office easier.

Being a competent hobbyist( or i’d like to think i am ) I often spend a few
hours up to a few days making my own mini-apps and scripts to perform
certain duties instead of purchasing software or having me do them manually.

Don’t worry all you real programmers out there we have real programmers at
the office to. =)

Zach

···

-----Original Message-----
From: Chad Fowler [mailto:chad@chadfowler.com]
Sent: Thursday, November 20, 2003 1:29 PM
To: ruby-talk ML
Subject: Re: learning the “Ruby way”

On Thu, 20 Nov 2003, Mark Wirdnam wrote:

Hobby-programmer alarm

May excellent suggestions have been made, so I won’t bore you with my
attempt. But, I wanted to say, “Welcome Hobby Programmer!”

I’m paid to do IT work, but I have increasingly moved my programming
activities into the land of hobby-dom, where I tend to enjoy them more. I
find Ruby to be an excellent language for hobbyists (as well as
enterprise users).

Are there other strict hobbyists on the list?

Chad

I’m an administrator rather than a programmer by trade, although as with
any admin job that also entails a fair amount of scripting, and my own
position also includes some C debugging and packaging.

On the other hand I learned Ruby scritly for hobby use, since at work
we’re stuck pretty firmly with Perl and POSIX shell. Now I’m spending
what hours I can spare working on Ruby-GNOME2 (mostly docs and bugfixes)
and a little media player which may actually be worth regular end users
looking at some day. :wink:

···

On Thu, 2003-11-20 at 13:28, Chad Fowler wrote:

On Thu, 20 Nov 2003, Mark Wirdnam wrote:

Hobby-programmer alarm

May excellent suggestions have been made, so I won’t bore you with my
attempt. But, I wanted to say, “Welcome Hobby Programmer!”

I’m paid to do IT work, but I have increasingly moved my programming
activities into the land of hobby-dom, where I tend to enjoy them more. I
find Ruby to be an excellent language for hobbyists (as well as
enterprise users).

Are there other strict hobbyists on the list?


Matthew Berg galt@gothpoodle.com

“Hobbyist”? What happened to “hacker”?

Hopefully I’ll graduate in March with a Bachelor of Software
Engineering, however until then you can call me a hobbyist if you must
but I prefer the term hacker because I’m one of those snooty people who
insists on “real” definitions even when the mainstream ones get changed.
:slight_smile:

In Ruby I’ve only written small programs which transform data and
interesting little toy scripts. But I skim this list every day and I
have a vapourware Ruby roguelike game in the works which is fun to
design and code random small bits when I have time away from
assignments, thesis and (hopefully soon) work.

···

Chad Fowler chad@chadfowler.com wrote:

Are there other strict hobbyists on the list?


Greg McIntyre ======[ greg@puyo.cjb.net ]===[ http://puyo.cjb.net ]===

I get by by just implementing a few methods from Hobby::Programmer :slight_smile:

Gavin

···

On Friday, November 21, 2003, 5:34:06 AM, dblack wrote:

Hi –
On Fri, 21 Nov 2003, Chad Fowler wrote:

On Thu, 20 Nov 2003, Mark Wirdnam wrote:

Hobby-programmer alarm

May excellent suggestions have been made, so I won’t bore you with my
attempt. But, I wanted to say, “Welcome Hobby Programmer!”

I’m paid to do IT work, but I have increasingly moved my programming
activities into the land of hobby-dom, where I tend to enjoy them more. I
find Ruby to be an excellent language for hobbyists (as well as
enterprise users).

Are there other strict hobbyists on the list?

How strict is strict? :slight_smile:

In Ruby I’ve only written small programs which transform data and
interesting little toy scripts. But I skim this list every day and I
have a vapourware Ruby roguelike game in the works which is fun to
design and code random small bits when I have time away from
assignments, thesis and (hopefully soon) work.

A roguelike game written in Ruby would be very interesting to see.
Especially if it was easy to add new elements to the game in ruby. If
you ever get something releasable, be sure to let us know.

···


Zachary P. Landau kapheine@hypa.net
GPG: gpg --recv-key 0x24E5AD99 | http://kapheine.hypa.net/kapheine.asc

I think Ruby’s a great language for it. I chose it because I wanted
others, possibly non-professional programmers, to be able to “write”
for it (create levels, dialogue, etc.). Ruby’s so intuitive and
easy that I thought it would be good.

That’s why I find the long raging discussion on interfaces interesting
– novice programmers are going to get things wrong so it’d be nice if I
could make their code adhere to an interface, or at least warn them when
it doesn’t. I mucked about with doing this in Ruby 1.8 and ended up with
modules which assert certain methods exist in the classes in which they
are included, at the point of their inclusion. It’s a simple and
not-perfect check but it is transparent check from the point of the
“writer” but it does combat forgetfulness.

Here’s a taste…

This is a map definition. Do I store it in XML? No. Do I store it as my
own special file format? No. It’s in Ruby! Yey! :slight_smile: Doing this makes map
definitions very flexible (e.g. it could be fully or partially randomly
generated upon definition).

The map definition is a class with a constant for layout and a constant
for a “key” (in the cartography sense). Each tile on the map contains a
bunch of stuff with no certain arrangement (I’m currently unsure if
that’s desirable… still thinking about it). Map::inherited is defined
to collect map definitions into a list. I have a demo in which you can
walk around this map, bump into things and set off the trap. Very
simple, but I’m still thinking about the design of the fundamentals, so
I didn’t want to get carried away (and because I’m a hobbyist/hacker).

Anyway, I’m posting it because it’s a nice example of Ruby’s power and
flexibility being used. :slight_smile:

···

“Zachary P. Landau” kapheine@hypa.net wrote:

In Ruby I’ve only written small programs which transform data and
interesting little toy scripts. But I skim this list every day and I
have a vapourware Ruby roguelike game in the works which is fun to
design and code random small bits when I have time away from
assignments, thesis and (hopefully soon) work.

A roguelike game written in Ruby would be very interesting to see.
Especially if it was easy to add new elements to the game in ruby. If
you ever get something releasable, be sure to let us know.


class DemoMap < Map

class SpecialTrap
def triggered_by(actor)
end

include Triggerable # asserts triggered_by is defined

end

LAYOUT =<<END_LAYOUT
###########
0…=…t.#
#…1
#…####
#…#
####…#
#…#
#…#
###########
END_LAYOUT

KEY = {
‘.’ => Ground,
‘#’ => BorderWall,
‘=’ => [
Ground,
Table,
],
‘0’ => [
Ground,
Marker.new(0),
Teleport.new(DemoMap, 1),
Door.new(open=true),
],
‘t’ => [
Ground,
SpecialTrap,
],
‘1’ => [
Ground,
Marker.new(1),
Teleport.new(DemoMap, 0),
Door.new(open=false),
],
}
end


Greg McIntyre ======[ greg@puyo.cjb.net ]===[ http://puyo.cjb.net ]===

> LAYOUT =< ########### > 0...=...t.# > #.........1 > #.........#### > #............# > ####.........# > #.........# > #.........# > ########### > END_LAYOUT

Very cool! I’ve been poking around the Doom WAD spec a bit, try to see
how feasible it would be to generate new levels with something like:

l = Level.new(10,10)
l.add(Wall.new(0,0,5,5))
l.set_spawn_point(5,0)

and so forth. But this looks like a much nicer way to do it.

Yours,

Tom

···

On Thu, 2003-11-20 at 20:37, Greg McIntyre wrote:

Wow, Nethack is being reinvented, again.

– Dossy

···

On Thu, 2003-11-20 at 20:37, Greg McIntyre wrote:

LAYOUT =<<END_LAYOUT
###########
0…=…t.#
#…1
#…####
#…#
####…#
#…#
#…#
###########
END_LAYOUT


Dossy Shiobara mail: dossy@panoptic.com
Panoptic Computer Network web: http://www.panoptic.com/
“He realized the fastest way to change is to laugh at your own
folly – then you can let go and quickly move on.” (p. 70)

To follow up on this, I’ve been working on a little utility to generate
DOOM maps programmatically (using Ruby, of course):

http://rubyforge.org/projects/ruby-doom/

One of its current features is a “Nethack renderer”, i.e.:

http://ruby-doom.rubyforge.org/

I’ve released a couple of versions and things seem to be working out
well so far…

Yours,

tom

···

On Fri, 2003-11-21 at 10:23, Tom Copeland wrote:

On Thu, 2003-11-20 at 20:37, Greg McIntyre wrote:

LAYOUT =<<END_LAYOUT
###########
0…=…t.#
#…1
#…####
#…#
####…#
#…#
#…#
###########
END_LAYOUT

Very cool! I’ve been poking around the Doom WAD spec a bit, try to see
how feasible it would be to generate new levels with something like:

l = Level.new(10,10)
l.add(Wall.new(0,0,5,5))
l.set_spawn_point(5,0)

and so forth. But this looks like a much nicer way to do it.