Ruby wish-list

Chad P. wrote:

On Thu, Jan 31, 2008 at 04:34:49AM +0900, Roger P. wrote:

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
referent to
‘a’ + 3
which, if I had my wish, would automatically be ‘a’ + 3.to_s

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

On Jan 30, 2008, at 6:00 PM, Chad P. wrote:

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:

a = Object.new
=> #Object:0x15a2bd8

class <<a
def to_str
‘object’
end
end
=> nil

puts ‘text+’ + a
text+object
=> nil

Another approach is to use Array#join instead of String#+:

[‘a’, Object.new, ‘b’].join
=> “a#Object:0x1588224b”

Gary W.

On Thu, Jan 31, 2008 at 04:34:49AM +0900, Roger P. wrote:

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 :slight_smile:

irb
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?

On 1/30/08, Chad P. [email protected] wrote:

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


Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

Hi,

In message “Re: ruby wish-list”
on Thu, 31 Jan 2008 04:34:49 +0900, Roger P.
[email protected] writes:

|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 :slight_smile:

It used to be behave like that. But it deferred the error detection,
so I changed.

          matz.

On Jan 30, 12:34 pm, Roger P. [email protected] wrote:

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 :slight_smile:

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.

Not to be “naive” - but a posting by the Yukihiro M.? … 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

On Thu, Jan 31, 2008 at 09:06:05AM +0900, Roger P. 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 :slight_smile:
Take care.
-Roger

On 04.02.2008 20:52, Roger P. wrote:

On Thu, Jan 31, 2008 at 09:06:05AM +0900, Roger P. 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 {}

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 :slight_smile:

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

Cheers

robert

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

Chad P. wrote:

On Thu, Jan 31, 2008 at 04:34:49AM +0900, Roger P. wrote:

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
referent to
‘a’ + 3
which, if I had my wish, would automatically be ‘a’ + 3.to_s

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

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

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

Then you wouldn’t have to remember syntax again, ever :slight_smile:

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

Hi,

At Tue, 5 Feb 2008 05:44:58 +0900,
Robert K. 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

On Tue, Feb 05, 2008 at 06:21:56AM +0900, Roger P. wrote:

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.

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

Nobuyoshi N. wrote:

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 :slight_smile: Now I’ve had two come true. Thank you.
-Roger

Roger P. wrote:

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

On Wed, Feb 06, 2008 at 05:30:06AM +0900, Siep K. wrote:

Thanks. Sorry to ramble on.
-Roger

“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
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>

Chad P. wrote:

On Wed, Feb 06, 2008 at 05:30:06AM +0900, Siep K. wrote:

Thanks. Sorry to ramble on.
-Roger

“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.:
(…)
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

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

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 :slight_smile:
-Roger