[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: beginner Q.

John Pywtorak

7/26/2007 11:55:00 PM

I don't know about hh's implementation but how about

irb(main):003:0> print "foo:"; gets().split
foo:this is a test
=> ["this", "is", "a", "test"]

Also, look at highline at http://highline.ruby...
--
Johnny P

--- Original Message ---
> So, when i first started to learn ruby *about a week or two
> ago*, my
> cousin *who showed me ruby* had me start on a program called
> hackety-hack, which is i type of tutorial thing, it kind of
> gives
> programs a sort of gui also. But i have noticed you cant do
> some of the
> same commands as on the irb main. In hackety-hack there was
> an
> *ask* command, so you could do things like
> var = ask("how old are you")...
>
> Im wondering if/ or what is the acctual *ask* command?? Because
> im
> trying to write a program for my dad *a chef* which vendors
> have the
> cheapest prices, and its getting annoying writing out all their
> prices
> into 2 different arrays. it would be nice if it could be like
> Array_1 = ask("put in the 1st vendors prices)
> Array_2 = ask("put in 2nd vendors prices) *and then i could
> go on and
> subtract the two arrays*
>
> Sorry if this is a totaly idiotic question :(!?
> Much thanx for anyones help!
> --
> Posted via http://www.ruby-....
>


9 Answers

Gabe Boyer

7/27/2007 12:47:00 AM

0

> Although I'd have to point out that 'ask' is an awful name for a method.

I'd say that depends entirely on context. In a context like Hackety
Hack, I think it's a perfectly good name.

-Gabriel Boyer

Erik Boling

7/27/2007 3:47:00 AM

0

Gabe Boyer wrote:
>> Although I'd have to point out that 'ask' is an awful name for a method.
>
> I'd say that depends entirely on context. In a context like Hackety
> Hack, I think it's a perfectly good name.
>
> -Gabriel Boyer

humm ok so i tried out my *program* but at the very end i get an error..
irb(main):001:0> def ask(question)
irb(main):002:1> puts "#{question}:\n"
irb(main):003:1> gets.split
irb(main):004:1> end
=> nil
irb(main):005:0> Var_1 = ask("Enter 1st Vendors prices")
Enter 1st Vendors prices:
14.50 16.50 14
=> ["14.50", "16.50", "14"]
irb(main):006:0> Var_2 = ask("Enter 2nd vendors prices")
Enter 2nd vendors prices:
13 17.50 14
=> ["13", "17.50", "14"]
irb(main):007:0> Var_1.zip(Var_2)
=> [["14.50", "13"], ["16.50", "17.50"], ["14", "14"]]
irb(main):008:0> Var_1.zip(Var_2).map{ |pair| pair[0] - pair[1] }
NoMethodError: undefined method `-' for "14.50":String
from (irb):8
from (irb):8:in `map'
from (irb):8
from :0
irb(main):009:0>

could it because im using floats, and not intergers?
--
Posted via http://www.ruby-....

Todd Benson

7/27/2007 4:10:00 AM

0

On 7/26/07, Erik Boling <schmode93@yahoo.com> wrote:
> Gabe Boyer wrote:
> >> Although I'd have to point out that 'ask' is an awful name for a method.
> >
> > I'd say that depends entirely on context. In a context like Hackety
> > Hack, I think it's a perfectly good name.
> >
> > -Gabriel Boyer
>
> humm ok so i tried out my *program* but at the very end i get an error..
> irb(main):001:0> def ask(question)
> irb(main):002:1> puts "#{question}:\n"
> irb(main):003:1> gets.split
> irb(main):004:1> end
> => nil
> irb(main):005:0> Var_1 = ask("Enter 1st Vendors prices")
> Enter 1st Vendors prices:
> 14.50 16.50 14
> => ["14.50", "16.50", "14"]
> irb(main):006:0> Var_2 = ask("Enter 2nd vendors prices")
> Enter 2nd vendors prices:
> 13 17.50 14
> => ["13", "17.50", "14"]

Your elements are Strings! That's what you get back from the gets
method. Let's change them to Floats. But first, let's not use
starting capital letters for our variables...

irb> var_1, var_2 = Var_1, Var_2
=> [["14.50", "16.40", 14"], ["13","17.50", "14"]]
irb> [var_1, var_2].each { |array| array.map! { |elem| elem.to_f } }
=> [[14.5, 16.5, 14.0], [13.0, 17.5, 14.0]]

> irb(main):007:0> Var_1.zip(Var_2)
You don't need this ^^^^^^^^ line

irb> difference = var_1.zip(var_2).map { |pair| pair[0] - pair[1] }
=> [1.5, -1.0, 0.0]

Todd

Erik Boling

7/27/2007 4:17:00 AM

0

Oh, wow ok should have thought of that, now instead of doing the whole
1.5 and -1.0 thing. It would be neat if it could just print *buy from
vendor #1" or "buy from vendor #2" *saying that you always want to buy
the cheapest, positives = vendor 2 is cheapest, negitaves, obviosly
vendor #1 is the cheapest*

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

Peña, Botp

7/27/2007 4:43:00 AM

0

From: Erik Boling [mailto:schmode93@yahoo.com]
# 1.5 and -1.0 thing. It would be neat if it could just print *buy from
# vendor #1" or "buy from vendor #2" *saying that you always
# want to buy the cheapest, positives = vendor 2 is cheapest, negitaves,
# obviosly vendor #1 is the cheapest*

why don't you just compare their averages?

kind regards -botp

Erik Boling

7/27/2007 4:51:00 AM

0

Peña, Botp wrote:
> From: Erik Boling [mailto:schmode93@yahoo.com]
> # 1.5 and -1.0 thing. It would be neat if it could just print *buy from
> # vendor #1" or "buy from vendor #2" *saying that you always
> # want to buy the cheapest, positives = vendor 2 is cheapest, negitaves,
> # obviosly vendor #1 is the cheapest*
>
> why don't you just compare their averages?
>
> kind regards -botp

you mean like the avarage of all the items together from each vendor, if
so... Well im not sure how much you know about the food industry, but,
overtime if you dont find the cheapest prices, it adds up quick,
epecially when your running a big catering company like my dad :P. But i
was thinking.. couldn't i put if and elsifs for it to work???
--
Posted via http://www.ruby-....

Todd Benson

7/27/2007 5:33:00 AM

0

On 7/26/07, Erik Boling <schmode93@yahoo.com> wrote:
> Peña, Botp wrote:
> > From: Erik Boling [mailto:schmode93@yahoo.com]
> > # 1.5 and -1.0 thing. It would be neat if it could just print *buy from
> > # vendor #1" or "buy from vendor #2" *saying that you always
> > # want to buy the cheapest, positives = vendor 2 is cheapest, negitaves,
> > # obviosly vendor #1 is the cheapest*
> >
> > why don't you just compare their averages?
> >
> > kind regards -botp
>
> you mean like the avarage of all the items together from each vendor, if
> so... Well im not sure how much you know about the food industry, but,
> overtime if you dont find the cheapest prices, it adds up quick,
> epecially when your running a big catering company like my dad :P. But i
> was thinking.. couldn't i put if and elsifs for it to work???

Also look at case-when and <=>
For example,

v1, v2 = [10, 9, 12], [8, 9, 13]
best = a.zip(b).map do |pair|
case pair[0] <=> pair[1]
when -1 then 'vendor a'
when 0 then 'same'
when 1 then 'vendor b'
end
end

Eventually, you might want to use Hashes...
v1 = { :onions => 0.70, :carrots => 0.88 }
v2 = { :onions => 0.78, :carrots => 0.87, :celery => 0.61 }

And other cool stuff like that :)

Todd

Erik Boling

7/27/2007 1:00:00 PM

0

hummmmm, well it just kinda of needs to be clean like
*vendor1-vendro1-vendor2*
for prices.. also where do you enter things in the db?
--
Posted via http://www.ruby-....

Erik Boling

7/27/2007 8:14:00 PM

0

Erik Boling wrote:
> hummmmm, well it just kinda of needs to be clean like
> *vendor1-vendro1-vendor2*
> for prices.. also where do you enter things in the db?

Sorry, i was a bit short on my last quote, I had leave for work and
didn't really read all of your post.
The hash is nice, but it takes too long to enter each individual price,
the vendors update prices every day, so entering individual prices 3
times for each product takes longer than just highlighting like normal.
Although, im not sure how databases work.. maybe its faster than hashes.
You idea is pretty good, I would ajust it a bit to make it easier to
read. Instead of listing all the prices and things just simply put *for
example* strawberries: vendor#1. What if you saped the vendor and the
pricing in you hash? that way you could just copy ans paste the prices
into it, as long as the prices are in the correct order.

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