[#358392] Increase significant digits in Float — Jason Lillywhite <jason.lillywhite@...>

If I want to increase my significant digits beyond 15 in a result of a

12 messages 2010/03/02

[#358431] A gem for handling temporary file(s)? — Albert Schlef <albertschlef@...>

I'm writing a program that needs to generate two or three temporary

21 messages 2010/03/03
[#358432] Re: A gem for handling temporary file(s)? — Paul Harrington <xenogenesis@...> 2010/03/03

Albert Schlef wrote:

[#358443] Re: A gem for handling temporary file(s)? — Albert Schlef <albertschlef@...> 2010/03/03

Paul Harrington wrote:

[#358486] Re: A gem for handling temporary file(s)? — Caleb Clausen <vikkous@...> 2010/03/03

On 3/2/10, Albert Schlef <albertschlef@gmail.com> wrote:

[#358485] Test::Unit::Omission - Unable to omit tests — Champak Ch <champaka@...>

I am trying to omit some tests while using the test unit framework. My

12 messages 2010/03/03

[#358551] Shared hosting recommendation? — Rafael Vega <email.rafa@...>

Hello!

10 messages 2010/03/04

[#358559] Limit number of concurrent running threads in pool — Joe Martin <jm202@...>

Hi

14 messages 2010/03/04

[#358576] A good portable text editor/IDE for Ruby? — Reiichi Tyrael <xxreiichixx@...>

I am searching for a good portable text editor or IDE for Ruby to use on

19 messages 2010/03/05

[#358586] Base-64 encoding--Just for the fun of it! — "Aaron D. Gifford" <astounding@...>

Yes, there's always:

10 messages 2010/03/05

[#358611] On what of these books is better to start to study Ruby? — Vlad Gerasimov <refermaker@...>

I have 3 books:

12 messages 2010/03/05

[#358634] Conditional keys in hash - out of the box? — "Sven S." <svoop@...>

Hi

12 messages 2010/03/05

[#358661] Why no TextMate for Linux? — thunk <gmkoller@...>

I spent some happy development time in "VisualAge" for Smalltalk +

42 messages 2010/03/06

[#358702] win32console 1.3.0.beta2 Released — Luis Lavena <luislavena@...>

win32console version 1.3.0.beta2 has been released!

17 messages 2010/03/07

[#358757] Shortest code — Prasanth Ravi <dare.take@...>

hi i'm a newbie in ruby and was test out some interesting problems in

18 messages 2010/03/08

[#358885] reading an UTF-8 encoded file — unbewusst.sein@... (Une B騅ue)

13 messages 2010/03/10

[#359008] Dir.glob problem — David Vlad <cluny_gisslaren@...>

In the program Im making I need to read some wma files into a variable

21 messages 2010/03/12

[#359031] Newbie Help : Object — Jerome David Sallinger <imran.nazir@...>

Hello,

14 messages 2010/03/13

[#359090] Overriding new? — Andrea Dallera <andrea@...>

Hi everybody,

19 messages 2010/03/15
[#359091] Re: Overriding new? — Chuck Remes <cremes.devlist@...> 2010/03/15

[#359093] Re: Overriding new? — Andrea Dallera <andrea@...> 2010/03/15

Hei Chuck,

[#359130] Recommended way to install Rubygems — Leslie Viljoen <leslieviljoen@...>

Hi!

64 messages 2010/03/16
[#359175] Re: Recommended way to install Rubygems — Eric Hodel <drbrain@...7.net> 2010/03/17

On Mar 16, 2010, at 03:22, Leslie Viljoen wrote:

[#359176] Re: Recommended way to install Rubygems — Lucas Nussbaum <lucas@...> 2010/03/17

(Please Cc me when replying, I don't follow ruby-talk@ closely enough to

[#359183] Re: Recommended way to install Rubygems — Nick Brown <nick@...> 2010/03/18

Lucas: Thanks for maintaining the Ruby package in Ubuntu!

[#359187] Re: Recommended way to install Rubygems — Lucas Nussbaum <lucas@...> 2010/03/18

On 18/03/10 at 13:36 +0900, Nick Brown wrote:

[#359200] Re: Recommended way to install Rubygems — Nick Brown <nick@...> 2010/03/18

Lucas Nussbaum wrote:

[#359204] Re: Recommended way to install Rubygems — Lucas Nussbaum <lucas@...> 2010/03/18

On 18/03/10 at 23:05 +0900, Nick Brown wrote:

[#359210] Re: Recommended way to install Rubygems — Rick DeNatale <rick.denatale@...> 2010/03/18

On Thu, Mar 18, 2010 at 10:21 AM, Lucas Nussbaum

[#359215] Re: Recommended way to install Rubygems — Lucas Nussbaum <lucas@...> 2010/03/18

On 18/03/10 at 23:45 +0900, Rick DeNatale wrote:

[#359230] Re: Recommended way to install Rubygems — Aldric Giacomoni <aldric@...> 2010/03/18

Lucas Nussbaum wrote:

[#359233] Re: Recommended way to install Rubygems — Lucas Nussbaum <lucas@...> 2010/03/18

On 19/03/10 at 02:49 +0900, Aldric Giacomoni wrote:

[#359171] Replace Text at Specific Positions Across Files — Shiny Hydra <slotriof@...>

Hello everyone,

12 messages 2010/03/17
[#359192] Re: Replace Text at Specific Positions Across Files — Robert Klemme <shortcutter@...> 2010/03/18

2010/3/17 Shiny Hydra <slotriof@guerrillamailblock.com>:

[#359198] Re: Replace Text at Specific Positions Across Files — Shiny Hydra <slotriof@...> 2010/03/18

> So your file has fixed width records? This is important to know,

[#359255] Grouping elements of an array — Steve Wilhelm <steve@...831.com>

I have an array of records that contain timestamps at random intervals.

24 messages 2010/03/18

[#359354] Living with a Swarm of Boids - A report from the front — thunk <gmkoller@...>

Hi,

15 messages 2010/03/20

[#359388] A plugin system using extend — Jean-denis Vauguet <jd@...>

Hi.

17 messages 2010/03/21
[#359394] Re: A plugin system using extend — Josh Cheek <josh.cheek@...> 2010/03/21

On Sat, Mar 20, 2010 at 9:46 PM, Jean-denis Vauguet <jd@vauguet.fr> wrote:

[#359398] Re: A plugin system using extend — Jean-denis Vauguet <jd@...> 2010/03/21

Thank you Josh. Actually I've already tested what you wrote and that's

[#359402] Re: A plugin system using extend — Jean-denis Vauguet <jd@...> 2010/03/21

Another idea I had is the following:

[#359410] Re: A plugin system using extend — James Edward Gray II <james@...> 2010/03/21

On Mar 21, 2010, at 2:13 AM, Jean-denis Vauguet wrote:

[#359420] Reading contents of all files from a Directory — Hawksury Gear <blackhawk_932@...>

Hello,

23 messages 2010/03/21
[#359422] Re: Reading contents of all files from a Directory — Jonathan Nielsen <jonathan@...> 2010/03/21

> I am trying to "Read Content" of all the files from a Directory. So far

[#359423] Re: Reading contents of all files from a Directory — Jonathan Nielsen <jonathan@...> 2010/03/21

> arr =3D Dir.open("K:/test").entries

[#359464] Re: Reading contents of all files from a Directory — Robert Klemme <shortcutter@...> 2010/03/22

2010/3/21 Jonathan Nielsen <jonathan@jmnet.us>:

[#360368] Re: Reading contents of all files from a Directory — Hawksury Gear <blackhawk_932@...> 2010/04/04

> If it is only for output purposes, we can actually do it in one line:

[#360370] Re: Reading contents of all files from a Directory — Jes俍 Gabriel y Gal疣 <jgabrielygalan@...> 2010/04/04

On Sun, Apr 4, 2010 at 3:06 PM, Hawksury Gear <blackhawk_932@hotmail.com> w=

[#360373] Re: Reading contents of all files from a Directory — Hawksury Gear <blackhawk_932@...> 2010/04/04

Thanks for replying ,when I am doing

[#360374] Re: Reading contents of all files from a Directory — Hassan Schroeder <hassan.schroeder@...> 2010/04/04

On Sun, Apr 4, 2010 at 8:16 AM, Hawksury Gear <blackhawk_932@hotmail.com> wrote:

[#360375] Re: Reading contents of all files from a Directory — Hawksury Gear <blackhawk_932@...> 2010/04/04

Hassan Schroeder wrote:

[#359662] index of string from beginning of line vs beginning of file — "Jesse B." <jessebos@...>

I am trying to write a basic script to implement "silent comments"

10 messages 2010/03/25
[#359663] Re: index of string from beginning of line vs beginning of file — Jes俍 Gabriel y Gal疣 <jgabrielygalan@...> 2010/03/25

On Thu, Mar 25, 2010 at 5:19 PM, Jesse B. <jessebos@aol.com> wrote:

[#359684] Ruby Summer of Code 2010 — Jeremy Kemper <jeremy@...>

Fellow Rubyists, I'm proud to announce the first annual Ruby Summer of Code.

20 messages 2010/03/26
[#359985] Re: [ANN] Ruby Summer of Code 2010 — Roger Pack <rogerpack2005@...> 2010/03/30

Jeremy Kemper wrote:

[#359697] Ruby and user documentation — Michel Demazure <michel@...>

Hi all,

20 messages 2010/03/26

[#359749] Boid writeup idea — thunk <gmkoller@...>

30 messages 2010/03/26

[#359909] return number of spaces at the beginning of a line — "Jesse B." <jessebos@...>

How would I find the number of spaces at the beginning of a line before

28 messages 2010/03/30
[#359925] Re: return number of spaces at the beginning of a line — Josh Cheek <josh.cheek@...> 2010/03/30

On Mon, Mar 29, 2010 at 8:41 PM, Jesse B. <jessebos@aol.com> wrote:

[#359941] Re: return number of spaces at the beginning of a line — Robert Klemme <shortcutter@...> 2010/03/30

2010/3/30 Josh Cheek <josh.cheek@gmail.com>:

[#359945] Re: return number of spaces at the beginning of a line — "Jesse B." <jessebos@...> 2010/03/30

This second post with the "spaces only" fix seems to meet all the needs

[#359961] Re: return number of spaces at the beginning of a line — Robert Klemme <shortcutter@...> 2010/03/30

2010/3/30 Jesse B. <jessebos@aol.com>:

[#360011] RubyDictionary - First Try — Max Schmidt <max.schmidt.privat@...>

Hello folks,

12 messages 2010/03/30
[#360035] Re: RubyDictionary - First Try — Jes俍 Gabriel y Gal疣 <jgabrielygalan@...> 2010/03/31

On Wed, Mar 31, 2010 at 12:40 AM, Max Schmidt

[#360033] Playing Games with "Ruids" — thunk <gmkoller@...>

46 messages 2010/03/31

Re: RubyDictionary - First Try

From: Jes俍 Gabriel y Gal疣 <jgabrielygalan@...>
Date: 2010-03-31 07:47:33 UTC
List: ruby-talk #360035
On Wed, Mar 31, 2010 at 12:40 AM, Max Schmidt
<max.schmidt.privat@googlemail.com> wrote:
> Hello folks,
>
> Last week I started learning Ruby by the book The Ruby Programming
> Language if it's relevant. Today I decided to practise a little bit with
> my new knowledge by trying to develop my first "serious" application in
> Ruby.

Welcome to Ruby and very good job. You achieved very nice things.

> The idea was to create a small virtual dictionary based on CLI
> with a few self-explaining functions like inserting/removing/search for
> a data set or save/load it in a *.txt file.

One question: when I think dictionary, I think this:
http://en.wikipedia.org/wiki/Associative_array, in which what is
unique is the keys, not the pairs. Your implementation allows to
insert two pairs with the same key:

irb(main):159:0* d = Dictionary.new
=> #<Dictionary:0xb7be8db0 @entries=[]>
irb(main):161:0> d.insert_words "aa", "bb"
=> [#<DictionaryEntry:0xb7bdd8fc @word2="bb", @word1="aa">]
irb(main):162:0> d.insert_words "aa", "cc"
=> [#<DictionaryEntry:0xb7bdd8fc @word2="bb", @word1="aa">,
#<DictionaryEntry:0xb7bd6494 @word2="cc", @word1="aa">]

and this looks a bit strange. But, anyway, I'll continue with the
assumption that this is what you really want.
If this is not the case, and you want unique keys, you will have to
change many things.

>
> Since this is my first attempt to a script language I wanted to know
> what you think of my program. Are there any passages which could be
> written more efficient in performance? In general, is the coding
> style/commenting OK? What can I improve regarding OOP? Furthermore,
> there are some commented in-code questions on line 26,93 and 99 - can
> you answer them maybe?

Some comments, mostly idiomatic:

1.-
	def initialize(entries=nil)
		if entries==nil
			@entries = Array.new
		else
			@entries = entries
		end
	end

this could be done like this:

	def initialize(entries=nil)
	   @entries = entries || []   # or Array.new if you prefer
	end

2.- def insert_words: I would call the method:

def []=(word1, word2) which allows you to say d["word"] = "second word"
Nice syntactic sugar, although this goes more in line of what I said
about the dictionary being a key-value set.

3.-
	def self.is_word? word
		if (word.match(/[a-zA-Z]{2,15}/)[0].length == word.length) then true
		else false
		end
		# (word.match(/[a-zA-Z]{2,15}/)[0].length == word.length)
		# easier way?
	end

Why don't you allow one letter words? There's a problem with this
implementation though: if the string doesn't match you get a

irb(main):160:0> d.insert_words "a", "b"
NoMethodError: undefined method `[]' for nil:NilClass
	from (irb):94:in `is_word?'

because match will return nil, and you are calling [0] on that.

def self.is_word? word
  word.match(/\A[:alpha:][:alpha:]+\z/) #you can remove the first
[:alpha:] to allow 1-letter words
end

4.- 	def insert_dict_entry(entry)
		insert_words(entry.word1, entry.word2) # any better solution?
	end

Another way, not necesarily better:

def insert_dict_entry(entry)
  @entries << entry.dup
end

5.- 	def insert_array(words1, words2)
		# arrays have to have same length
		raise ArgumentError, "Invalid array arguments (not same length)"
unless words1.length==words2.length
		
		# iterate on words1 and try to insert every pair
		words1.each_index {|x| insert_words(words1[x], words2[x])}
	end

I would do:

def insert_array(words1, words2)
  words1.zip(words2).each {|first,second| insert_words(first,second)
if first && second}
end

this way, even if the arrays have different size, you store as much as
you can. If you still want the check you can add it.

6.- def remove(r_word1, r_word2)

def remove(word1, word2)
  @entries.delete_if {|entry| entry == [word1,word2]} # if you keep
the enumerable equality check, see below
end

7.- def remove_at(index)

This one doesn't make sense for the public interface of a dictionary.
If you implement remove as above, you don't need it anymore.

8.- def search s_word

def search word
  @entries.select {|entry| entry.word1.include?(word) ||
entry.word2.include?(word)}
end

9.- def has_word_pair?(word1, word2)

Why do you need the index as a return value?

def has_word_pair?(word1,word2)
  @entries.find {|entry| entry == [word1,word2]}
end

10.- to_s

def to_s
  s = "Current state of Dictionary (#{self.object_id})\n"
  @entries.each_with_index {|entry, i| s << sprintf("%3d %15s |
%15s\n", i, entry.word1 , entry.word2)
  s
end

DictionaryEntry

When you create a dictionary entry, you might want to dup the words to
avoid aliasing:

def initialize (word1, word2)
  @word1 = word1.dup unless word1.frozen?
  @word2 = word2.dup unless word2.frozen?
end

11.-
	def ==(other)
		if (other.is_a?(Enumerable) && @word1 == other[0] && @word2 == other[1])
			true
		elsif (other.is_a?(DictionaryEntry) && @word1 == other.word1 &&
@word2 == other.word2) # not tested
			puts "dict entry"
			true
		else
			false
		end
	end

Enumerable doesn't provide the method [], so if you are allowing a
equality test against an Enumerable you should only use Enumerable
methods. For example:

def ==(other)
  case other
  when Enumerable
    @word1 == other.to_a[0] && @word2 == other.to_a[1]
  when DictionaryEntry
    @word1 == other.word1 && @word2 == other.word2
  else
    false
  end
end

I'm not sure you want to keep the equality comparison with an
Enumerable, because I think you added it more as a convenience than as
a real requirement. If you remove it, some of my suggestions above
should be reviewed.

I don't have time now to comment on the CLI interface, but I think
that could be refactored a little bit too.


Jesus.

In This Thread