ruby wish-list

G

Gary Wright

irb(main):001:0> obj = Object.new
=> #<Object:0x8064048>
irb(main):002:0> puts obj
#<Object:0x8064048>
=> nil
irb(main):003:0> puts obj.to_s
#<Object:0x8064048>
=> nil

Seems like it already does what you want. What am I missing?

I think Roger is saying that String#+ should call to_s on its argument.

String#+ already calls to_str on its argument:
text+object
=> nil


Another approach is to use Array#join instead of String#+:
['a', Object.new, 'b'].join
=> "a#<Object:0x1588224>b"


Gary Wright
 
R

Rick DeNatale

irb(main):001:0> obj = Object.new
=> #<Object:0x8064048>
irb(main):002:0> puts obj
#<Object:0x8064048>
=> nil
irb(main):003:0> puts obj.to_s
#<Object:0x8064048>
=> nil

Seems like it already does what you want. What am I missing?

Not that I necessarily agree with the OPs wish, but that wasn't his use case:

irb(main):001:0> "abc" + 1
TypeError: can't convert Fixnum into String
from (irb):1:in `+'
from (irb):1
irb(main):002:0> "abc" + Object.new
TypeError: can't convert Object into String
from (irb):2:in `+'
from (irb):2
 
P

Phrogz

I know this is controversial, but I wish that if you did
string_1 + object_2 (or any object) that it would just call .to_s on
object_2 (instead of having to write it explicitly).  I hate having to
write extra .to_s's (even if it avoids ambiguity).  That's just me, but
hey :)

Having provided support on a #javascript IRC channel for years, I can
say that one of the most common errors and questions among users
(particularly when they were working with input from forms) was: "Why
is 1 + 1 == 11???"

JavaScript and Ruby are both dynamically typed. JavaScript, however,
is somewhat loosely typed; it performs some automatic 'helpful'
conversion from one data type to another. Ruby is more strongly typed,
requiring you to be explicit about type conversion, most of the time.

I like Ruby's balance. It's not so strong that you have to do things
like "Hello #{person.to_str}", but not so loose that you see many
1+"1" == "11" types of errors.
 
A

Ashley Wharton

[Note: parts of this message were removed to make it a legal post.]

Not to be "naive" - but a posting by the Yukihiro Matsumoto? ... wow! I have
only just discovered Ruby (in the last several weeks) - I have become
addicted!!! A truly amazing mind ... a great (and fun) language.

Sincere and humble regards, naive

Ashley
 
C

Chad Perrin

referent to
'a' + 3
which, if I had my wish, would automatically be 'a' + 3.to_s

Duh. Sorry about that -- somehow the fact you were using a + operator in
there slipped my mind.
 
R

Roger Pack

On Thu, Jan 31, 2008 at 09:06:05AM +0900, Roger Pack wrote:

Another wish--I wish that the block style stuff was invertable.
programs.each_with_index {|program, index|...}

would work with

for program, index in programs.each_with_index {}

Then you wouldn't have to remember syntax again, ever :)
Take care.
-Roger
 
R

Robert Klemme

Another wish--I wish that the block style stuff was invertable.
programs.each_with_index {|program, index|...}

would work with

for program, index in programs.each_with_index {}

Like

$ irb -r enumerator
irb(main):001:0> programs = %w{foo bar}
=> ["foo", "bar"]
irb(main):002:0> for p,i in programs.to_enum:)each_with_index)
irb(main):003:1> puts p, i
irb(main):004:1> end
foo
0
bar
1
=> ["foo", "bar"]

?
Then you wouldn't have to remember syntax again, ever :)

I don't see the difference - I mean, you have to remember both ways,
don't you?

Cheers

robert
 
R

Roger Pack

irb(main):002:0> for p,i in programs.to_enum:)each_with_index)
irb(main):003:1> puts p, i
irb(main):004:1> end
nice


I don't see the difference - I mean, you have to remember both ways,
don't you?

perhaps with this way you can 'always' use one way, if you have any
question of which would work.
Just thinking out loud.
Thanks for your timely reply.
-Roger
 
C

Chad Perrin

perhaps with this way you can 'always' use one way, if you have any
question of which would work.
Just thinking out loud.
Thanks for your timely reply.

This only really works if you are a purely solitary coder.
 
N

Nobuyoshi Nakada

Hi,

At Tue, 5 Feb 2008 05:44:58 +0900,
Robert Klemme wrote in [ruby-talk:289837]:
$ irb -r enumerator
irb(main):001:0> programs = %w{foo bar}
=> ["foo", "bar"]
irb(main):002:0> for p,i in programs.to_enum:)each_with_index)
irb(main):003:1> puts p, i
irb(main):004:1> end

FYI, in 1.9 each_with_index returns an Enumerator (which is
built-in now) if no block is given, so you don't need to_enum
here.

for p, i in programs.each_with_index
puts p, i
end
 
R

Roger Pack

Nobuyoshi said:
FYI, in 1.9 each_with_index returns an Enumerator (which is
built-in now) if no block is given, so you don't need to_enum
here.

for p, i in programs.each_with_index
puts p, i
end

Wow my wish come true :) Now I've had two come true. Thank you.
-Roger
 
R

Roger Pack

Is it possible to easily strip arbitrary characters off a string?
a la "abc:".strip ':'
?
it looks like it's possible with gsub
abc:'.gsub(/:$/, '')
however the wish is a simpler function for it.

If not then it goes on the wish list.

Thanks. Sorry to ramble on.
-Roger
 
S

Siep Korteling

Roger said:
Is it possible to easily strip arbitrary characters off a string?
a la "abc:".strip ':'
?
it looks like it's possible with gsub
abc:'.gsub(/:$/, '')
however the wish is a simpler function for it.

If not then it goes on the wish list.

Thanks. Sorry to ramble on.
-Roger

"abc:".chomp(":")
or
"abc:".chomp!(":")

Regards,

Siep
 
C

Chad Perrin

"abc:".chomp(":")
or
"abc:".chomp!(":")

Note: That only works if the character you want to strip away is the last
character in the string. When you don't supply an argument, it removes
only any newline character at the end of the string, e.g.:
irb(main):001:0> foo = 'abc
irb(main):002:0' '
=> "abc\n"
irb(main):003:0> foo
=> "abc\n"
irb(main):004:0> foo.chomp
=> "abc"
irb(main):005:0>
 
S

Siep Korteling

Chad said:
Note: That only works if the character you want to strip away is the
last
character in the string. When you don't supply an argument, it removes
only any newline character at the end of the string, e.g.:
(...)
Yes. Well you can chop off any substring with chomp("any string").
If "strip" means delete all occurances of a substring, there is uhm,
delete.
"a:bc:".delete(":")
=> "abc"


Regards,

Siep
 
R

Roger Pack

Now my own wish. I wish ruby didn't need ,'s for method parameters.
Why are they always necessary?
foo a b c # as long as a, b, and c aren't functions, and aren't
operators, this could parse!
Or could it? Still wishing for this one sigh :)
-Roger
 
R

Roger Pack

All right magic genie--my next request! (from Aladdin movie)
Better syntax and expanded coolness for hashes.
Like multi-map hashes, ordered hashes (in 1.9?),
better normal hash syntax for
hash['a'] = true
hash -= 'a' # instead of delete
or maybe not even use brackets at all--they confuse with their
similarity to arrays.
Thoughts?
-Roger
 
G

Gary Wright

or maybe not even use brackets at all--they confuse with their
similarity to arrays.
Thoughts?

I tend to define [] and []= whenever possible if the object behaves
like an indexed collection of objects. I think this helps with duck-
typing. If every collection had a different manner of indexing items
it would defeat any duck-typing.

Less common, but also useful is to provide fetch/store in addition to
[] and []=. The fetch API is nice when you want to substitute a
different default value:

fetch(index) # get item or raise exception
fetch(index, miss) # get item or return miss
fetch(index) { } # get item or compute miss

Gary Wright
 
R

Roger Pack

I tend to define [] and []= whenever possible if the object behaves
like an indexed collection of objects. I think this helps with duck-
typing. If every collection had a different manner of indexing items
it would defeat any duck-typing.

Interesting. Is there a common delete api?
Cheers.
=roger
 
J

Joel VanderWerf

Roger said:
hash -= 'a' # instead of delete

That would be the same as

hash = hash - 'a'

so (whatever Hash#- does) it involves making a copy of the hash and
changing that instead, whereas #delete is destructive.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top