[#345382] Nice algorithm for 'spreading' indexes across an array? — Max Williams <toastkid.williams@...>

Little ruby algorithm puzzle...

13 messages 2009/09/01

[#345437] clogger 0.0.4 - configurable request logging for Rack — Eric Wong <normalperson@...>

* http://clogger.rubyforge.org/

10 messages 2009/09/02
[#345439] Re: [ANN] clogger 0.0.4 - configurable request logging for Rack — Iñaki Baz Castillo <ibc@...> 2009/09/02

2009/9/2 Eric Wong <normalperson@yhbt.net>:

[#345446] rdoc — Oleg Puchinin <ruby_talk@...>

Hello !

17 messages 2009/09/02
[#346260] Ruby 1.9 rdoc never ends (Re: rdoc) — James Britt <james.britt@...> 2009/09/12

Oleg Puchinin wrote:

[#346267] Re: Ruby 1.9 rdoc never ends (Re: rdoc) — Ryan Davis <ryand-ruby@...> 2009/09/12

[#346276] Re: Ruby 1.9 rdoc never ends (Re: rdoc) — Roger Pack <rogerpack2005@...> 2009/09/12

Ryan Davis wrote:

[#345493] What licensing info is needed in code headers? — "Shot (Piotr Szotkowski)" <shot@...>

Disclaimer: I know that some of you live in jurisdictions that do not

10 messages 2009/09/02

[#345535] Simple New Ruby Programmer Problem with $stdin.gets — Mason Kelsey <masonkelsey@...>

I'm having difficulty getting any command to work to pick up input from a

14 messages 2009/09/02

[#345573] Type checking function parameters — Nick Green <cruzmail.ngreen@...>

More or less all my functions look something like

22 messages 2009/09/03
[#345593] Re: Type checking function parameters — Eleanor McHugh <eleanor@...> 2009/09/03

On 3 Sep 2009, at 05:04, Nick Green wrote:

[#345606] Re: Type checking function parameters — Paul Smith <paul@...> 2009/09/03

My first stab at some Ruby started like this too.

[#345667] Re: Type checking function parameters — Nick Green <cruzmail.ngreen@...> 2009/09/03

OK...

[#345676] Re: Type checking function parameters — Eleanor McHugh <eleanor@...> 2009/09/04

On 3 Sep 2009, at 23:47, Nick Green wrote:

[#345687] Re: Type checking function parameters — James Edward Gray II <james@...> 2009/09/04

On Sep 3, 2009, at 7:30 PM, Eleanor McHugh wrote:

[#345745] Re: Type checking function parameters — Eleanor McHugh <eleanor@...> 2009/09/04

On 4 Sep 2009, at 03:56, James Edward Gray II wrote:

[#345828] Re: Type checking function parameters — spiralofhope <spiralofhope@...> 2009/09/06

Along the lines of this thread..

[#345835] Re: Type checking function parameters — "David A. Black" <dblack@...> 2009/09/06

Hi --

[#345773] Rubyscript instead of javascript — Damjan Rems <d_rems@...>

30 messages 2009/09/05
[#345970] Re: Rubyscript instead of javascript — Jg W Mittag <JoergWMittag+Usenet@...> 2009/09/08

David Masover wrote:

[#345774] how to compare two object instances? is "m1.to_yaml.eql?(m2.to_yaml)" a good way? — Greg Hauptmann <greg.hauptmann.ruby@...>

Hi,

8 messages 2009/09/05

[#345848] i need to strip \n and nil — Bigmac Turdsplash <i8igmac@...>

im sending files back and forth form a client and a server using

16 messages 2009/09/06

[#345883] Executing system commands in threads under Ruby 1.8.6 — vhaerun vh <etaern@...>

I tried to write a script that makes use of external binaries. Each

17 messages 2009/09/07
[#345889] Re: Executing system commands in threads under Ruby 1.8.6 — Robert Klemme <shortcutter@...> 2009/09/07

2009/9/7 vhaerun vh <etaern@yahoo.com>:

[#345893] Re: Executing system commands in threads under Ruby 1.8.6 — vhaerun vh <etaern@...> 2009/09/07

Here's a link to the question I asked on SO:

[#345901] Re: Executing system commands in threads under Ruby 1.8.6 — Eleanor McHugh <eleanor@...> 2009/09/07

On 7 Sep 2009, at 09:55, vhaerun vh wrote:

[#345904] Re: Executing system commands in threads under Ruby 1.8.6 — Bertram Scharpf <lists@...> 2009/09/07

Hi,

[#345886] Ruby 1.9, Rubygems, and .gemspec warnings — Rob Sanheim <rsanheim@...>

Hi all

14 messages 2009/09/07

[#346018] Tutorial challenge program help — Chris Logan <t-logan3@...>

Hello all im really new to ruby as in a few days and getting into it. i

20 messages 2009/09/09
[#346023] Re: Tutorial challenge program help — 7stud -- <bbxx789_05ss@...> 2009/09/09

Chris Logan wrote:

[#346027] Re: Tutorial challenge program help — Chris Logan <t-logan3@...> 2009/09/09

7stud -- wrote:

[#346091] How Are Variables Kept Independent of Each Other Yet Pass Values? — Mason Kelsey <masonkelsey@...>

Somewhere in the several books I've been learning Ruby from there was the

14 messages 2009/09/10
[#346096] Re: How Are Variables Kept Independent of Each Other Yet Pass Values? — venkatesh Peddi <venkat.peddi@...> 2009/09/10

[#346106] Asynchronous http POST? — Ivan Shevanski <ocelot117@...>

Hey everyone, I'm new to Ruby and to the mailing list, so go easy.

14 messages 2009/09/10
[#346166] Re: Asynchronous http POST? — Ezra Zygmuntowicz <ezmobius@...> 2009/09/10

[#346193] populating a hash from an array using inject — Glenn Jackman <glennj@...>

I was looking at this problem on Stack Overflow (this one:

12 messages 2009/09/10

[#346324] module to overwrite method defined via define_method — Gaspard Bucher <gaspard@...>

Hi List !

17 messages 2009/09/13
[#346326] Re: module to overwrite method defined via define_method — "David A. Black" <dblack@...> 2009/09/13

Hi --

[#346328] Re: module to overwrite method defined via define_method — Gaspard Bucher <gaspard@...> 2009/09/13

David A. Black wrote:

[#346347] FasterCSV.foreach loop — Dot Baiki <dot_baiki@...>

Hello community,

16 messages 2009/09/13

[#346367] .map.with_object(3){|v|v+3} #=> 3 Is this a bug? — ErMaker <ermaker@...>

At ruby 1.9.2dev (2009-07-18 trunk 24186) [i386-mswin32_90]

15 messages 2009/09/14

[#346383] Pre-allocate large amount of memory? — Carsten Gehling <carsten@...>

I've created a small daemon, that serves certain data very fast to our

15 messages 2009/09/14
[#346404] Re: Pre-allocate large amount of memory? — Robert Klemme <shortcutter@...> 2009/09/14

2009/9/14 Carsten Gehling <carsten@sarum.dk>:

[#346419] whats the best way to package deploy a Ruby app to windows??? (no UI, also standalone if possible) — Greg Hauptmann <greg.hauptmann.ruby@...>

Hi,

8 messages 2009/09/14

[#346452] Command line — Rong <ron.green@...>

Please forgive this stupid newb question but I thought it was possible

13 messages 2009/09/15

[#346500] Array of Hashes in an array of hashes - Complicated! — Matt Brooks <mattbrooks@...>

I have an unique problem that I can't solve. I am sorry this is long,

17 messages 2009/09/15
[#346505] Re: Array of Hashes in an array of hashes - Complicated! — John W Higgins <wishdev@...> 2009/09/15

Morning Matt,

[#346508] Re: Array of Hashes in an array of hashes - Complicated! — Matt Brooks <mattbrooks@...> 2009/09/15

Hi John,

[#346510] Re: Array of Hashes in an array of hashes - Complicated! — John W Higgins <wishdev@...> 2009/09/15

Matt,

[#346515] Re: Array of Hashes in an array of hashes - Complicated! — Aldric Giacomoni <aldric@...> 2009/09/15

+1 on object creation

[#346574] string to array — Re BR <rereis@...>

Hello all,

15 messages 2009/09/16

[#346611] block issues... — Dylan Lukes <revenantphoenix@...>

In the following block, each plugin in the constant hash PLUGINS is

17 messages 2009/09/16

[#346621] Monkey Patching 2 Methods, Overrides One Method, Not The Other — MaggotChild <hsomob1999@...>

I'm monkey patching 2 methods of an existing module: some_method() and

18 messages 2009/09/17

[#346645] Mucking about with dynamically adding methods to objects — Paul Smith <paul@...>

I've been toying with Ruby for a while, but only now am I beginning to

12 messages 2009/09/17
[#346652] Re: Mucking about with dynamically adding methods to objects — Jes俍 Gabriel y Gal疣 <jgabrielygalan@...> 2009/09/17

On Thu, Sep 17, 2009 at 11:44 AM, Paul Smith <paul@pollyandpaul.co.uk> wrote:

[#346665] Re: Mucking about with dynamically adding methods to objects — Paul Smith <paul@...> 2009/09/17

2009/9/17 Jes俍 Gabriel y Gal疣 <jgabrielygalan@gmail.com>:

[#346676] Value isn't appended in puts statement(appears on next line) — Mrmaster Mrmaster <mrsolarlife@...>

Hello,

13 messages 2009/09/17
[#346678] Re: Value isn't appended in puts statement(appears on next line) — Jes俍 Gabriel y Gal疣 <jgabrielygalan@...> 2009/09/17

On Thu, Sep 17, 2009 at 8:51 PM, Mrmaster Mrmaster

[#346759] Newbie: Are Ruby regexp's a subset, superset, or equal to Perl's? — Harry <simonsharry@...>

Hi,

13 messages 2009/09/18

[#346774] Exceptional Rails Developer — Richard Price <richard.price100@...>

Hi all,

32 messages 2009/09/18
[#347451] Re: Exceptional Rails Developer — Ilan Berci <ilan.berci@...> 2009/09/30

Richard Price wrote:

[#347452] Re: Exceptional Rails Developer — Zundra Daniel <zundra.daniel@...> 2009/09/30

At least he didn't say "Rockstar" or "Ninja"

[#347476] Re: Exceptional Rails Developer — David Masover <ninja@...> 2009/09/30

On Wednesday 30 September 2009 01:45:27 pm Zundra Daniel wrote:

[#347477] Re: Exceptional Rails Developer — Greg Donald <gdonald@...> 2009/09/30

On Wed, Sep 30, 2009 at 6:44 PM, David Masover <ninja@slaphack.com> wrote:

[#347482] Re: Exceptional Rails Developer — David Masover <ninja@...> 2009/10/01

On Wednesday 30 September 2009 06:54:16 pm Greg Donald wrote:

[#347514] Re: Exceptional Rails Developer — Eleanor McHugh <eleanor@...> 2009/10/01

On 1 Oct 2009, at 01:32, David Masover wrote:

[#347551] Re: Exceptional Rails Developer — David Masover <ninja@...> 2009/10/01

On Thursday 01 October 2009 08:20:26 am Eleanor McHugh wrote:

[#347592] Re: Exceptional Rails Developer — Eleanor McHugh <eleanor@...> 2009/10/02

On 1 Oct 2009, at 19:15, David Masover wrote:

[#347596] Re: Exceptional Rails Developer — Aldric Giacomoni <aldric@...> 2009/10/02

[#346775] Determining if a file is binary or text — James Masters <james.d.masters@...>

Hi all,

15 messages 2009/09/18

[#346891] Incrementing variable names in a loop? — Matt Brooks <mattbrooks@...>

I have a function write_log that takes in a string and it prints to

10 messages 2009/09/21

[#347044] the great ruby editor and ide roundup — Martin DeMello <martindemello@...>

https://spreadsheets.google.com/ccc?key=0Al_hzYODcgxwdG9tUFhqcVVoUDVaLTlqT2YtNjV1N0E&hl=en

26 messages 2009/09/23
[#347045] Re: the great ruby editor and ide roundup — Rajinder Yadav <devguy.ca@...> 2009/09/23

On Wed, Sep 23, 2009 at 4:02 PM, Martin DeMello <martindemello@gmail.com> wrote:

[#347058] How do you limit the line length of the output commands? Where is pqueue library documented? — Mason Kelsey <masonkelsey@...>

There must be an easy way to solve the problem of controlling the length of

12 messages 2009/09/23

[#347156] Roulette & rand — Semih Ozkoseoglu <ozansemih@...>

Hi,

22 messages 2009/09/25
[#347161] Re: Roulette & rand — Stefano Crocco <stefano.crocco@...> 2009/09/25

On Friday 25 September 2009, Semih Ozkoseoglu wrote:

[#347164] Re: Roulette & rand — Semih Ozkoseoglu <ozansemih@...> 2009/09/25

Hi again Stefano,

[#347171] Re: Roulette & rand — Stefano Crocco <stefano.crocco@...> 2009/09/25

On Friday 25 September 2009, Semih Ozkoseoglu wrote:

[#347173] Re: Roulette & rand — Semih Ozkoseoglu <ozansemih@...> 2009/09/25

Stefano, Paul,

[#347179] Re: Roulette & rand — Semih Ozkoseoglu <ozansemih@...> 2009/09/25

Hi again,

[#347193] How to remove duplicate elements in a 2D array — Li Chen <chen_li3@...>

Hi all,

20 messages 2009/09/25

[#347202] Backporting Enumerator.new { ... } to Ruby 1.8.7 — "Shot (Piotr Szotkowski)" <shot@...>

Hello, good people of ruby-talk.

12 messages 2009/09/25

[#347260] handling of regexp objects that aren't referenced by variables, arrays, tables or objects — ThomasW <x.zupftom@...>

Hi,

12 messages 2009/09/27

[#347354] How do I use nitpick — "Michael W. Ryder" <_mwryder@...>

I was looking for a program like lint in C and came across nitpick. I

23 messages 2009/09/29
[#347366] Re: How do I use nitpick — Hassan Schroeder <hassan.schroeder@...> 2009/09/29

On Mon, Sep 28, 2009 at 9:25 PM, Michael W. Ryder

[#347397] Re: How do I use nitpick — "Michael W. Ryder" <_mwryder@...> 2009/09/29

Hassan Schroeder wrote:

[#347398] Re: How do I use nitpick — Ryan Davis <ryand-ruby@...> 2009/09/29

[#347364] Group by unique entries of a hash — Ne Scripter <stuart.clarke@...>

I have two data sets loaded into a hash to give the following output

15 messages 2009/09/29

[#347443] Get current working copy version in subversion/git — Anthony Metcalf <anthony.metcalf@...>

Hi,

11 messages 2009/09/30

[#347456] SystemStackError: stack level too deep > how make it deeper? — Joshua Muheim <forum@...>

Hi all

15 messages 2009/09/30
[#347459] Re: SystemStackError: stack level too deep > how make it deeper? — Jason Roelofs <jameskilton@...> 2009/09/30

On Wed, Sep 30, 2009 at 3:47 PM, Joshua Muheim <forum@josh.ch> wrote:

Getting to one attribute of my marshalled array of objects

From: Brian Tickler <btickler@...>
Date: 2009-09-18 18:00:37 UTC
List: ruby-talk #346756
I have looked around for an answer to this on the mailing list and
Google searches, but I am not finding anything, so sorry if this
question seems too basic but...

I am just learning Ruby, and one of my class assignments is writing a
command line app that tracks an inventory of cars, each car having 6
attributes:  VIN, year, make, model, color, and price.  We are supposed
to use 2 objects for this exercise, a Car class and a "List of Cars"
class.  Then a menu that allows adding a car to the list, deleting a car
from the list, and showing the list.

I could probably done this assignment as-is :)...but I wanted to go a
little bit further.  It did not seem to make sense that this app did not
call for any persistence and once you made your inventory of cars and
then left the app they were gone.  So, armed with pickaxe, I tried to
add file handling, but...that didn't make sense either:  since I was
using Car objects and a CarInventory object that is just an array of Car
objects, converting the objects all the time into a format where I could
put them into a delimited text file and then read them out again seemed
crazy.

Then I read farther in pickaxe (past what my class goes into,
unfortunately), and I found Marshalling.  That seemed like exactly what
I wanted...I could just dump the CarInventory object with it's whole
array of Cars (into a file called cars.data in this case) and then read
it right back in when needed.

It is almost working, too :).  But, I am having a problem figuring out
how to connect all the dots from A to D.  A couple of things are
nagging:

- The first time the app is run, there is no cars.data file, so I have
to handle that, which I am doing now but it's far from elegant...the app
will decide you don't have any car inventory, initialize the inventory
with a Car that has all nil attributes, and ask you to enter the first
car, then you can Save.  This has some issues in that even though I
tried to write the code to later overwrite the "nil car", it is still
there somehow :).

- More importantly, once I have a Saved cars.data file and I use
Marshal.load on it, I don't want that "enter the first car" routine to
kick in, so I try to check the first Car object in CarInventory and see
if its VIN number is nil or not.  Somehow I am missing the boat here,
and I am sure it's something simple.  When I try to get from Marshalled
object -> CarInventory instance -> first Car object -> first Car.vin I
get the following:

cars.rb:106: undefined method `[]' for #<CarInventory:0x3fb97c8>
(NoMethodError)

I am including the entire app below, it's not too big though I am sure
it is much bigger and clunkier than it needs to be ;)...like I said I am
jsut learning Ruby...any help would be appreciated in fixing my
immediate and dire issues, and/or anything about making the whole app
work more smoothly.  You can ignore some of the other
also-not-actually-required-for-this-assignment bells and whistles I have
partially implemented for now.  Sorry, I realize it's a bit messy.

----SOURCE

class Car

  def initialize(vin, year, make, model, color, price)
    @vin = vin
    @year = year
    @make = make
    @model = model
    @color = color
    @price = price
  end

  def to_s
    if !self.vin
      @displayVIN = "(No VIN)"
    else
      @displayVIN = self.vin
    end
    if !self.color
      @displayColor = ""
    else
      @displayColor = "(#@color)"
    end
    "#@displayVIN\t#@year #@make #@model #@displayColor\t#@price\n"
  end

  def <=> (compareThisCar)
    self.year  <=> compareThisCar.year
  end

  attr_reader :vin, :year, :make, :model, :color, :price
  attr_writer :vin, :year, :make, :model, :color, :price

end

class CarInventory

  @@inventoryFile = "cars.data"

  def initialize
    if File.exists?(@@inventoryFile)
      File.open(@@inventoryFile) do |file|
        @carArray = Marshal.load(file)
      end
    else
        @carArray = Array.new
        @carArray[0] = Car.new(nil, nil, nil, nil, nil, nil)
    end
  end

  def save
    File.open(@@inventoryFile, "w+") do |file|
      Marshal.dump(self, file)
    end
  end

  def addCar(car)
    if car.vin == nil
      @carArray[0] = car
    else
      @carArray << car
    end
  end

  def updateCar(car)
    updateIndex = @carArray.find {|vinMatch| car.vin == vinMatch}
    if updateIndex
      @carArray[updateIndex]  = car
    end
  end

  def delCar(car)
    @carArray = @carArray - car
  end

  def to_s
    @carArray.each {|car| car.to_s if car.vin}
  end

  def sort(sortBy)
    case sortBy
      when "V":
      when "M":
      when "P":
      when "Y":
    else
    end
  end

  def filter(filterArray)

  end

end

puts "Car Inventory 1.0\n"

cars = CarInventory.new
firstCar = cars.carArray
firstCar = firstCar[0]
if !firstCar.vin
  puts "\nNo car inventory found, please enter the first car:"
  puts "\nVIN:"
  vin = gets.chomp
  puts "\nYear:"
  year = gets.chomp
  puts "\nMake:"
  make = gets.chomp
  puts "\nModel:"
  model = gets.chomp
  puts "\nColor:"
  color = gets.chomp
  puts "\nPrice:"
  price = gets.chomp
  newCar = Car.new(vin, year, make, model, color, price)
  cars.addCar(newCar)
else
 cars.sort("V")
 cars.filter(nil)
end

loop do
  puts "\nVIN#\t\tCar:\t\t\tPrice:\n"
  puts "------------------------------------------------\n"
  puts cars.to_s
  puts "------------------------------------------------\n"
  puts "(A)dd a new car\n"
  puts "(E)dit a car\n"
  puts "(D)elete a car\n"
  puts "(L)ist cars by VIN\n"
  puts "   by (M)ake and Model\n"
  puts "   by (P)rice\n"
  puts "   by (Y)ear\n"
  puts "(S)ave changes\n"
  puts "(Q)uit\n"
  puts "\nCommand: "
  command = gets.chomp.upcase
  case
    when command =~ /^A/ : puts "\nAdd a new car:\n"
      puts "\nVIN:"
      vin = gets.chomp
      puts "\nYear:"
      year = gets.chomp
      puts "\nMake:"
      make = gets.chomp
      puts "\nModel:"
      model = gets.chomp
      puts "\nColor:"
      color = gets.chomp
      puts "\nPrice:"
      price = gets.chomp
      newCar = Car.new(vin, year, make, model, color, price)
      cars.addCar(newCar)
    when command =~ /^E/ : puts "\nEdit a car:\n"
    when command =~ /^D/ : puts "\nDelete a car:\n"
    when command =~ /^L/ : puts "\nList car inventory by VIN:\n"
    when command =~ /^M/ : puts "\nList car inventory by Make and
Model:\n"
    when command =~ /^P/ : puts "\nList car inventory by Price range:\n"
    when command =~ /^Y/ : puts "\nList car inventory by Year\n"
    when command =~ /^S/ : cars.save
    when command =~ /^Q/ : puts "Are you sure? [Y/N]"
      confirm = gets.chomp.upcase
      exit if confirm =~ /^Y/
      next
    else puts "Illegal Command: #{command}"
  end
end
-- 
Posted via http://www.ruby-forum.com/.

In This Thread

Prev Next