Ask a Question related to Ruby, Design and Development.
-
Ben Giddings #1
Re: What's New and Shiny in Ruby 1.8.0?
On Monday, August 4, 2003, at 11:33 PM, why the lucky stiff wrote:
Thanks for doing that. I like that you've not only mentioned the> Since there were a number of requests around for a more detailed
> explanation of 1.8.0 changes, I figured I'd start into my favorites and
> cover as many as I can.
changes but also given some background on why they're interesting and
how they might be used.
Duck typing is an interesting idea -- but I think to be used well it
will require more careful planning. I think the example of testing for
the method "read" is a good example. I can imagine a large number of
classes that would have a "read" method, but wouldn't return what's
expected from an IO type read.
Looking at the page, I also have another question concerning
Hash.merge. It does essentially the same thing is the set-union for
Arrays, right (Array#|)? Would anybody besides me like to see Hash#|
become an alias for Hash#merge? To me it would be more obvious that
{'key' => 'val'} | {'otherkey' => 'otherval'} returns a new hash and
doesn't modify the original one.
Either way, the presence of this method is great for some HTTP stuff
I'm doing where the only difference between a GET and a POST is that
the POST requires a Content-type header.
Ben
Ben Giddings Guest
-
Ruby/Ruby on Rails Syntax Highlight/Code completion
Hoping someone has, or is working on, an extension that adds Ruby and Ruby on Rails syntax/code coloring/code completion to Dreamweaver! -
[ba-rb] BA-rb ( Bay Area Ruby Users Group ) - 'Generating Code in Ruby' by Jack Herrington.
Count me in again, too. -- Jos Backus _/ _/_/_/ Sunnyvale, CA _/ _/ _/ _/ _/_/_/ _/ _/ _/ _/ jos at... -
BA-rb ( Bay Area Ruby Users Group ) - 'Generating Code in Ruby' by Jack Herrington.
BA-rb (Bay Area Ruby language Users Group) is pleased to announce that it will begin meeting again. The topic for the first meeting will be a... -
Shiny areas on lady's faces in portraits
I have several candid shots which will make excellent portraits if I can deal with shiny areas on the lady’s faces. Does anyone have any Idea how I... -
[ANN] ruby-freedb, ruby-serialport, ruby-mp3info moved to Rubyforge
http://ruby-freedb.rubyforge.org/ http://ruby-serialport.rubyforge.org/ http://ruby-mp3info.rubyforge.org/ bye! --... -
Ben Giddings #2
Re: What's New and Shiny in Ruby 1.8.0?
On Tuesday, August 5, 2003, at 01:50 AM, Yukihiro Matsumoto wrote:
Hi Matz,> The | operator is not suitable for the cases like
>
> {"key" => "val1"} | {"key" => "val2}
>
> That's the reason I chose "merge" for the name of the method.
Thanks for the clarification. I hadn't thought of which hash "wins"
when both contain a key. Using "merge" does make it more clear.
Ben
Ben Giddings Guest
-
Harry Ohlsen #3
Re: What's New and Shiny in Ruby 1.8.0?
why the lucky stiff wrote:
One thing mentioned there is the new sort_by in Enumerable. A simple example I tried is:> My 1.8.0 summary is up at:
>
> [url]http://whytheluckystiff.net/articles/2003/08/04/rubyOneEightOh[/url]
>
> I'm about halfway done. I'll post a link again when I feel like I'm
> finished.
text = ["the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"]
puts text.sort_by { |x| x.length }.inspect
#=> ["the", "the", "fox", "dog", "over", "lazy", "quick", "brown", "jumps"]
Something that's not mentioned, but I figured I'd try it, because I thought it would be nifty, is sorting by multiple values ...
text = ["the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"]
puts text.sort_by { |x| [x.length, x] }.inspect
#=> ["dog", "fox", "the", "the", "lazy", "over", "brown", "jumps", "quick"]
Ie, we now have the values sorted first by their length, and when the length is the same, they're sub-sorted by the text itself. I think that's great!
This would be particularly useful if the data items in the enumeration were instances of more complex objects. Then, you could do things like:
sorted = people.sort_by { |p| [p.last_name, p.first_name, p.salary] }
I just love Ruby!
Harry Ohlsen Guest
-
Hal E. Fulton #4
Re: What's New and Shiny in Ruby 1.8.0?
----- Original Message -----
From: "Harry Ohlsen" <harryo@qiqsolutions.com>
To: "ruby-talk ML" <ruby-talk@ruby-lang.org>
Sent: Tuesday, August 05, 2003 8:17 PM
Subject: Re: What's New and Shiny in Ruby 1.8.0?
thought it would be nifty, is sorting by multiple values ...> Something that's not mentioned, but I figured I'd try it, because I"dog"]>
> text = ["the", "quick", "brown", "fox", "jumps", "over", "the", "lazy","quick"]>
> puts text.sort_by { |x| [x.length, x] }.inspect
>
> #=> ["dog", "fox", "the", "the", "lazy", "over", "brown", "jumps",length is the same, they're sub-sorted by the text itself. I think that's>
> Ie, we now have the values sorted first by their length, and when the
great!were instances of more complex objects. Then, you could do things like:>
> This would be particularly useful if the data items in the enumerationAgreed. I'll file the multi-key sort under T for Things That>
> sorted = people.sort_by { |p| [p.last_name, p.first_name, p.salary] }
>
> I just love Ruby!
Make Me Go Ahhh...
Hal
--
Hal Fulton
[email]hal9000@hypermetrics.com[/email]
Hal E. Fulton Guest
-
Harry Ohlsen #5
Re: What's New and Shiny in Ruby 1.8.0?
why the lucky stiff wrote:
Certainly! That's the first time someone's called something I wrote "readable" :-).> If you don't mind, I'm going to add this tiny example to the page. This is
> incredibly readable and demonstrates a number of Ruby's brilliant features at
> once.
Great. I'll go have another read.> I've also just finished a section on Procs, StringIO, -run, and fixed the
> Class.new and Module.new section. Just now starting on marshal_load,
> marshal_dump, String#[], String#to_i, and Array#zip.
Yes, there certainly is. I've been avoiding using the 1.8 features until we got to preview releases, so it's all new (and fantastic!) to me.> Whew! There's so much!
H.
Harry Ohlsen Guest
-
daz #6
[OT] Re: What's New and Shiny in Ruby 1.8.0?
"Harry Ohlsen" <harryo@qiqsolutions.com> wrote:
It doesn't roll off the tongue like the original IMHO :)>
> #=> ["dog", "fox", "the", "the", "lazy", "over", "brown", "jumps", "quick"]
>
but you may be the first person to discover programmatically
that there's a redundant word in there.
In response to this crisis, I offer:
'Quick dogs jump over the lazy brown fox'
Agreed. That *is/will be* bl**dy useful.> Ie, we now have the values sorted first by their length,
> and when the length is the same, they're sub-sorted by the
> text itself. I think that's great!
It's okay :)>
> I just love Ruby!
>
daz
--
text = ['the quick brown fox jumped over the lazy dog',
'the quick brown fox jumps over the lazy dog',
'quick dogs jump over the lazy brown fox']
text.each do |phr|
puts; p phr
z = ('a'..'z').to_a - phr.split('')
puts " ... doesn't contain \"#{z}\"" unless z.empty?
end
#->"the quick brown fox jumped over the lazy dog"
#-> ... doesn't contain "s"
daz Guest
-
daz #7
Re: [OT] Re: What's New and Shiny in Ruby 1.8.0?
I wrote:Arghh, there's a redundant variable in that sig.>
> that there's a redundant word in there.
>
['the quick brown fox jumped over the lazy dog',
'the quic brown ox jumps over the lazy dog',
'quick dogs jump over the lazy brown fox'].
each do |phr|
puts; p phr
z = ('a'..'z').to_a - phr.split('')
puts " ... doesn't contain \"#{z}\"" unless z.empty?
end
daz Guest
-
Marko Schulz #8
Re: [OT] Re: What's New and Shiny in Ruby 1.8.0?
On Wed, Aug 06, 2003 at 02:01:23PM +0900, daz wrote:
I would've left it in. (For me) it makes a nice snippet more>
> Arghh, there's a redundant variable in that sig.
readable.
--
marko schulz
Marko Schulz Guest
-
Hugh Sasse Staff Elec Eng #9
Re: [OT] Re: What's New and Shiny in Ruby 1.8.0?
On Wed, 6 Aug 2003, daz wrote:
ISBN: 0385722435 :-)> In response to this crisis, I offer:
>
> 'Quick dogs jump over the lazy brown fox'
>
Hugh
Hugh Sasse Staff Elec Eng Guest
-
Brett H. Williams #10
Re: What's New and Shiny in Ruby 1.8.0?
On Aug 6, Harry Ohlsen wrote:
I really like this. But is there a simple easy way to get a descending> sorted = people.sort_by { |p| [p.last_name, p.first_name, p.salary] }
sort here without reverting to sorted = people.sort { ... } ?
Brett H. Williams Guest
-
Mark J. Reed #11
Re: What's New and Shiny in Ruby 1.8.0?
On Thu, Aug 07, 2003 at 01:35:40AM +0900, Brett H. Williams wrote:
How about> On Aug 6, Harry Ohlsen wrote:>> > sorted = people.sort_by { |p| [p.last_name, p.first_name, p.salary] }
> I really like this. But is there a simple easy way to get a descending
> sort here without reverting to sorted = people.sort { ... } ?
sorted = people.sort_by { |p| [p.last_name, p.first_name, p.salary] }.reverse
I haven't looked, but I suspect you can't selectively make individual
criteria ascending/descending. Now, if we had a #stable_sort_by,
then we could chain them together to get the desired
results without crafting complex comparisons . . . :)
-Mark
Mark J. Reed Guest
-
Jason Creighton #12
Re: What's New and Shiny in Ruby 1.8.0?
On Thu, 7 Aug 2003 02:26:35 +0900
Ian Macdonald <ian@caliban.org> wrote:
^^^^^^^^^^^^^^^^^^> On Wed 06 Aug 2003 at 10:17:44 +0900, Harry Ohlsen wrote:
>>> > This would be particularly useful if the data items in the enumeration were
> > instances of more complex objects. Then, you could do things like:
> >
> > sorted = people.sort_by { |p| [p.last_name, p.first_name, p.salary] }
> How does one use this with one's own objects, though?
>
> I just cooked up an example Person class to play with and then tried to
> sort it as above:
>
> class Person
> include Enumerable
Person doesn't implement #each, why mixin Enumerable?
~/prog/ruby$ ruby -v> attr_accessor :first_name, :last_name, :age
>
> def initialize(f,l,a)
> @first_name = f
> @last_name = l
> @age = a.to_i
> end
> end
>
> p [d, g, i, j, p, j2, j3].sort_by { |x| [x.last_name, x.first_name, x.age] }
>
> and I get:
>
> /sort_by:23: undefined method `[]' for #<Person:0x4014e37c>
> (NoMethodError)
>
> I'm fine with implementing Person#[], but I can't imagine how it should
> look.
ruby 1.8.0 (2003-08-04) [i686-linux]
~/prog/ruby$ cat person.rb
class Person
attr_accessor :first_name, :last_name
def initialize(f,l)
@first_name = f
@last_name = l
end
def inspect
"#{@first_name} #{@last_name}"
end
end
ary = [ Person.new("Joe", "Blow"),
Person.new("John", "Doe"),
Person.new("Bill", "Blow"),
Person.new("Jane", "Doe") ]
p ary.sort_by { |x| [x.last_name, x.first_name ] }
~/prog/ruby$ ruby person.rb
[Bill Blow, Joe Blow, Jane Doe, John Doe]
BTW, your original example worked fine on my version of Ruby. Another
option, of course, is to implement <=> for Person like so (untested)
class Person
def <=>(other)
[ @last_name, @first_name ] <=> [ other.last_name, other.first_name ]
end
end
Jason Creighton
Jason Creighton Guest
-
Harry Ohlsen #13
Re: What's New and Shiny in Ruby 1.8.0?
On Thu, 7 Aug 2003 03:43, Mark J. Reed wrote:
For any numeric fields, you could just use -x, rather than x. So, to sort as> On Thu, Aug 07, 2003 at 01:35:40AM +0900, Brett H. Williams wrote:>> > On Aug 6, Harry Ohlsen wrote:> >> > > sorted = people.sort_by { |p| [p.last_name, p.first_name, p.salary] }
> > I really like this. But is there a simple easy way to get a descending
> > sort here without reverting to sorted = people.sort { ... } ?
> How about
>
> sorted = people.sort_by { |p| [p.last_name, p.first_name, p.salary]
> }.reverse
>
>
> I haven't looked, but I suspect you can't selectively make individual
> criteria ascending/descending. Now, if we had a #stable_sort_by,
> then we could chain them together to get the desired
> results without crafting complex comparisons . . . :)
above, but in descending salary ...
sorted = people.sort_by { |p| [p.last_name, p.first_name, -p.salary] }
but I can't think of a way to get it to work for strings, for example. Maybe
if strings had a unary minus that changed every character to the character
with code (255 - ascii-code) ... but that sounds like a real hack :-). Of
course, it's not general enough, either.
Harry Ohlsen Guest
-
Martin DeMello #14
Re: What's New and Shiny in Ruby 1.8.0?
Harry Ohlsen <harryo@zip.com.au> wrote:
Still hacky, but general:> but I can't think of a way to get it to work for strings, for example. Maybe
> if strings had a unary minus that changed every character to the character
> with code (255 - ascii-code) ... but that sounds like a real hack :-). Of
> course, it's not general enough, either.
class Test
attr_accessor :foo, :bar
def initialize(f, b)
@foo, @bar = f, b
end
def inspect
"[#{foo}, #{bar}]"
end
end
def rev(obj)
a = obj.dup
class << a
alias old_cmp <=>
def <=>(other)
-old_cmp(other)
end
end
a
end
a = [
Test.new("Hello", 1),
Test.new("World", -1),
Test.new("Foo", 5),
Test.new("bar", 4)
]
p a
p a.sort_by {|x| x.foo}
p a.sort_by {|x| rev(x.foo)}
martin
Martin DeMello Guest
-
why the lucky stiff #15
Re: What's New and Shiny in Ruby 1.8.0?
Ruby/DL is now covered. Prototyping functions, casting into structs, and
pointer math! Everything you never wanted to do in Ruby but can! :)
Thanks for reading, Srijit.
_why
On Tuesday 05 August 2003 11:01 pm, [email]srijit@yahoo.com[/email] wrote:> Hi,
> Thanks for the detailed explanation.
> I am interested to know more about ext/dl (an interface to the dynamic
> linker) as mentioned in [url]http://dev.faeriemud.org/changes-1.8.0.html[/url].
>
> Regards,
> Srijit
>
> why the lucky stiff <ruby-talk@whytheluckystiff.net> wrote in message
> news:<20030805033123.GA73854@rysa.inetz.com>...
>> > Since there were a number of requests around for a more detailed
> > explanation of 1.8.0 changes, I figured I'd start into my favorites and
> > cover as many as I can.
> >
> > My 1.8.0 summary is up at:
> >
> > [url]http://whytheluckystiff.net/articles/2003/08/04/rubyOneEightOh[/url]
> >
> > I'm about halfway done. I'll post a link again when I feel like I'm
> > finished.
> >
> > _why
why the lucky stiff Guest
-
Ian Macdonald #16
Re: What's New and Shiny in Ruby 1.8.0?
On Thu 07 Aug 2003 at 05:19:32 +0900, Harry Ohlsen wrote:
I assigned to the variable 'p', which is also a method in Ruby. That's> On Thu, 7 Aug 2003 03:26, Ian Macdonald wrote:
>>> > How does one use this with one's own objects, though?
> >
> > I just cooked up an example Person class to play with and then tried to
> > sort it as above:
> >
> > class Person
> > include Enumerable
> >
> > attr_accessor :first_name, :last_name, :age
> >
> > def initialize(f,l,a)
> > @first_name = f
> > @last_name = l
> > @age = a.to_i
> > end
> > end
> >
> > p [d, g, i, j, p, j2, j3].sort_by { |x| [x.last_name, x.first_name, x.age]
> > }
> >
> > and I get:
> >
> > ./sort_by:23: undefined method `[]' for #<Person:0x4014e37c>
> > (NoMethodError)
> I would guess that there's something in the code you didn't show (ie, that
> defines d, g, i, j, p, j2 and j3) that's causing this error message.
bitten me before.
Ian
--
Ian Macdonald | I've already told you more than I know.
System Administrator |
[email]ian@caliban.org[/email] |
[url]http://www.caliban.org[/url] |
|
Ian Macdonald Guest
-
Harry Ohlsen #17
Re: What's New and Shiny in Ruby 1.8.0?
Martin DeMello wrote:
However, if we try> Still hacky, but general:
>
> class Test
> attr_accessor :foo, :bar
> def initialize(f, b)
> @foo, @bar = f, b
> end
>
> def inspect
> "[#{foo}, #{bar}]"
> end
> end
>
> def rev(obj)
> a = obj.dup
> class << a
> alias old_cmp <=>
> def <=>(other)
> -old_cmp(other)
> end
> end
> a
> end
>
> a = [
> Test.new("Hello", 1),
> Test.new("World", -1),
> Test.new("Foo", 5),
> Test.new("bar", 4)
> ]
>
> p a
> p a.sort_by {|x| x.foo}
> p a.sort_by {|x| rev(x.foo)}
p a.sort_by { |x| rev(x.bar) }
we get ...
sort_by.rb:20:in `dup': can't dup Fixnum (TypeError)
I tried sticking in an
if obj.respond_to? :dup
but that didn't help. It would appear that, while Fixnum responds to #dup, its response is "I'm sorry, Dave, I'm afraid I can't do that." :-(.
That doesn't seem to make a lot of sense to me. I would have thought it made more sense for Fixnum#dup to just return self. Maybe there's some good reason for not doing that.
Alternatively, maybe we need a way for classes to disown methods they inherit that they don't want to (or logically shouldn't) implement? Then, Fixnum could disown dup and the respond_to? test would work the way I was expecting.
Harry O.
Harry Ohlsen Guest
-
Harry Ohlsen #18
Re: What's New and Shiny in Ruby 1.8.0?
Jason Creighton wrote:
That's probably something one should do, anyway. Ie, define the logical default ordering for objects of that type.> BTW, your original example worked fine on my version of Ruby. Another
> option, of course, is to implement <=> for Person like so (untested)
>
> class Person
> def <=>(other)
> [ @last_name, @first_name ] <=> [ other.last_name, other.first_name ]
> end
> end
The thing I like about the sort_by approach is that it allows you to easily change the ordering on the fly.
It's also just such a nice example of POLS!
H.
Harry Ohlsen Guest
-
Pit Capitain #19
Re: What's New and Shiny in Ruby 1.8.0?
On 7 Aug 2003 at 8:30, Harry Ohlsen wrote:
Very nice idea. I like it.> Martin DeMello wrote:
>> > Still hacky, but general:
> >
> > class Test
> > attr_accessor :foo, :bar
> > def initialize(f, b)
> > @foo, @bar = f, b
> > end
> >
> > def inspect
> > "[#{foo}, #{bar}]"
> > end
> > end
> >
> > def rev(obj)
> > a = obj.dup
> > class << a
> > alias old_cmp <=>
> > def <=>(other)
> > -old_cmp(other)
> > end
> > end
> > a
> > end
> >
> > a = [
> > Test.new("Hello", 1),
> > Test.new("World", -1),
> > Test.new("Foo", 5),
> > Test.new("bar", 4)
> > ]
> >
> > p a
> > p a.sort_by {|x| x.foo}
> > p a.sort_by {|x| rev(x.foo)}
This could be fixed with something like> However, if we try
>
> p a.sort_by { |x| rev(x.bar) }
>
> we get ...
>
> sort_by.rb:20:in `dup': can't dup Fixnum (TypeError)
def rev(obj)
-obj
rescue NoMethodError
a = obj.dup
class << a
alias old_cmp <=>
def <=>(other)
-old_cmp(other)
end
end
a
end
Not beautiful, but working.
But then you couldn't do things like Martin's code, where you want to> I tried sticking in an
>
> if obj.respond_to? :dup
>
> but that didn't help. It would appear that, while Fixnum responds to
> #dup, its response is "I'm sorry, Dave, I'm afraid I can't do that."
> :-(.
>
> That doesn't seem to make a lot of sense to me. I would have thought
> it made more sense for Fixnum#dup to just return self. Maybe there's
> some good reason for not doing that.
modify a duplicate and not the original object.
Since dup is defined in Object (Kernel), you'd expect to find it in> Alternatively, maybe we need a way for classes to disown methods they
> inherit that they don't want to (or logically shouldn't) implement?
> Then, Fixnum could disown dup and the respond_to? test would work the
> way I was expecting.
every object, wouldn't you? But if you really want you can undefine
dup for Fixnums:
p 5.respond_to?( :dup ) # => true
p 5.dup rescue puts $! # => can't dup Fixnum
Fixnum.send :undef_method, :dup
p 5.respond_to?( :dup ) # => false
p 5.dup rescue puts $! # => undefined method `dup' for 5:Fixnum
Regards,
Pit
Pit Capitain Guest
-
Martin DeMello #20
Re: What's New and Shiny in Ruby 1.8.0?
Harry Ohlsen <harryo@qiqsolutions.com> wrote:
This fixes it:> However, if we try
>
> p a.sort_by { |x| rev(x.bar) }
>
> we get ...
>
> sort_by.rb:20:in `dup': can't dup Fixnum (TypeError)
>
> I tried sticking in an
>
> if obj.respond_to? :dup
>
> but that didn't help. It would appear that, while Fixnum responds to
> #dup, its response is "I'm sorry, Dave, I'm afraid I can't do that."
> :-(.
def rev(obj)
a = obj.dup rescue (return obj.cmp_inv)
class << a
alias old_cmp <=>
def <=>(other)
-old_cmp(other)
end
end
a
end
class Fixnum
def cmp_inv
-self
end
end
where for any class not supporting dup, we explicitly define a
'comparative inverse' such that a<=>b = b.cmp_inv<=>a.cmp_inv for all a,
b belonging to our class.
In this instance, we definitely don't want Fixnum#dup to return self -> That doesn't seem to make a lot of sense to me. I would have thought
> it made more sense for Fixnum#dup to just return self. Maybe there's
> some good reason for not doing that.
the call to dup is to avoid the object itself having its comparator
reversed. We could avoid the call to dup altogether by using a
delegator, I suppose
class RevCmp
attr_reader :this
def initialize(obj)
@this = obj
end
def <=>(other)
other.this <=> @this
end
# not delegating anything else because this is explicitly a throwaway
# object used only inside a sort_by block
end
def rev(obj)
RevCmp.new(obj)
end
This breaks some sort of OO principle, I think.> Alternatively, maybe we need a way for classes to disown methods they
> inherit that they don't want to (or logically shouldn't) implement?
> Then, Fixnum could disown dup and the respond_to? test would work the
> way I was expecting.
martin
Martin DeMello Guest



Reply With Quote

