Looking for an array method

Are there any methods which work exactly like []= but return the changed
array, different from []= which returns void?

Sun Park wrote:

Are there any methods which work exactly like = but return the changed
array, different from = which returns void?

Array#fill?

a = [0, 1, 2, 3]
=> [0, 1, 2, 3]

irb(main):006:0> a.fill("x", 2..2)
=> [0, 1, "x", 3]

···

--
Posted via http://www.ruby-forum.com/\.

Thanks :slight_smile: do we have such method in String class, too?

···

2007/6/3, Daniel Lucraft <dan@fluentradical.com>:

Sun Park wrote:
> Are there any methods which work exactly like = but return the changed
> array, different from = which returns void?

Array#fill?

a = [0, 1, 2, 3]
=> [0, 1, 2, 3]

irb(main):006:0> a.fill("x", 2..2)
=> [0, 1, "x", 3]

--
Posted via http://www.ruby-forum.com/\.

irb(main):008:0> a='abc'
=> "abc"
irb(main):009:0> a[1]=?a
=> 97
irb(main):013:0> a[1..1]="a"
=> "a"

thus String#= is behaving differently from Array#=

Cheers
Robert
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw

···

On 6/3/07, Sun Park <geniusleonid@gmail.com> wrote:

Thanks :slight_smile: do we have such method in String class, too?

Sorry, what I meant was :

a = "abc"
=> "abc"
a (some method with arguments of 1 and "d")
=> "adc"

I wanted to find out are there any methods behaving like this.

···

2007/6/3, Robert Dober <robert.dober@gmail.com>:

On 6/3/07, Sun Park <geniusleonid@gmail.com> wrote:
> Thanks :slight_smile: do we have such method in String class, too?
irb(main):008:0> a='abc'
=> "abc"
irb(main):009:0> a[1]=?a
=> 97
irb(main):013:0> a[1..1]="a"
=> "a"

thus String#= is behaving differently from Array#=

Cheers
Robert
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw

Sun Park wrote:

Sorry, what I meant was :

a = "abc"
=> "abc"
a (some method with arguments of 1 and "d")
=> "adc"

I wanted to find out are there any methods behaving like this.

a = "abc"
a[1,1] = "d"
a # => "adc"
a[1] = ?b
a # => "abc"

Enjoy
Stefan

···

2007/6/3, Robert Dober <robert.dober@gmail.com>:

--
Posted via http://www.ruby-forum.com/\.

Sorry, what I meant was :

a = "abc"
=> "abc"
a (some method with arguments of 1 and "d")
=> "adc"

I wanted to find out are there any methods behaving like this.

Yes #=, what is wrong with it? Would you prefer a method with a
different name, I am not sure there is one.
Robert

···

On 6/3/07, Sun Park <geniusleonid@gmail.com> wrote:

2007/6/3, Robert Dober <robert.dober@gmail.com>:
>
> On 6/3/07, Sun Park <geniusleonid@gmail.com> wrote:
> > Thanks :slight_smile: do we have such method in String class, too?
> irb(main):008:0> a='abc'
> => "abc"
> irb(main):009:0> a[1]=?a
> => 97
> irb(main):013:0> a[1..1]="a"
> => "a"
>
> thus String#= is behaving differently from Array#=
>
> Cheers
> Robert
> You see things; and you say Why?
> But I dream things that never were; and I say Why not?
> -- George Bernard Shaw
>

--
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw

None directly that I know of, but ...

a = "abc
a.split('').fill("d",1,1).join

or

(a[1]='d';a)

···

On 6/3/07, Sun Park <geniusleonid@gmail.com> wrote:

Sorry, what I meant was :

a = "abc"
=> "abc"
a (some method with arguments of 1 and "d")
=> "adc"

I wanted to find out are there any methods behaving like this.

Sun Park wrote:
> Sorry, what I meant was :
>
> a = "abc"
> => "abc"
> a (some method with arguments of 1 and "d")
> => "adc"
>
> I wanted to find out are there any methods behaving like this.
>

This was not my post!!
Robert

···

On 6/3/07, Stefan Rusterholz <apeiros@gmx.net> wrote:

> 2007/6/3, Robert Dober <robert.dober@gmail.com>:
Enjoy
Stefan

--
Posted via http://www.ruby-forum.com/\.

--
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw

a = "abc"
p a[1] = "d" # I think the OP wants to see "adc" here
p a # not just here

Harry

···

On 6/3/07, Robert Dober <robert.dober@gmail.com> wrote:

On 6/3/07, Sun Park <geniusleonid@gmail.com> wrote:
> Sorry, what I meant was :
>
> a = "abc"
> => "abc"
> a (some method with arguments of 1 and "d")
> => "adc"
>
> I wanted to find out are there any methods behaving like this.
Yes #=, what is wrong with it? Would you prefer a method with a
different name, I am not sure there is one.
Robert
>

--

A Look into Japanese Ruby List in English