h = {a: {b: {c: 23}}}
def hash_brek(h)
h.each_with_object({}) do |i,temp|
temp[i.first] = i.last
p temp
if h.eql? ({c: 23}) then
p “hi”
temp[i.first] = i.last
return temp
end
hash_brek(i.last)
end
end
p hash_brek(h)
output:
D:\Rubyscript\My ruby learning days\Scripts>practice.rb
“2.0.0”
{:a=>{:b=>{:c=>23}}}
{:b=>{:c=>23}}
{:c=>23}
{:a=>{:b=>{:c=>23}}}
But I wanted the output as:
{:a=>{:b=>{:c=>23}}}
{:a=>{:b=>{:c=>23}},:b=>{:c=>23}}
{:a=>{:b=>{:c=>23}},:b=>{:c=>23},:c=>23}
{:a=>{:b=>{:c=>23}},:b=>{:c=>23},:c=>23}
Can you point where did I the wrong, thus not getting the output as
above?
I just corrected it,but different approach:
p RUBY_VERSION
h = {a: {b: {c: 23}}}
def hash_brek(h)
h.each_with_object([]) do |i,temp|
if h.eql? ({c: 23}) then
return temp.push(i.first,i.last)
else
return temp.push(i.first,i.last) + hash_brek(i.last)
end
end
end
p hash_brek(h)
output:
D:\Rubyscript\My ruby learning days\Scripts>practice.rb
“2.0.0”
[:a, {:b=>{:c=>23}}, :b, {:c=>23}, :c, 23]
D:\Rubyscript\My ruby learning days\Scripts>
pretty simple:
your line:
if h.eql? ({c: 23}) then
should not have a space after eql?:
tamouse mailing lists wrote in post #1103989:
pretty simple:
your line:
if h.eql? ({c: 23}) then
should not have a space after eql?:
I didn’t make the recursion properly. Now I made it proper,but thanks
for your interest. 
On Mon, Apr 1, 2013 at 12:56 PM, Love U Ruby [email protected]
wrote:
I didn’t make the recursion properly. Now I made it proper,but thanks
for your interest. 
the output of that new version looks different from the output you
said you wanted…
On Mon, Apr 1, 2013 at 1:41 PM, Love U Ruby [email protected]
wrote:
tamouse mailing lists wrote in post #1103993:
On Mon, Apr 1, 2013 at 12:56 PM, Love U Ruby [email protected]
wrote:
The above is fine. But I am looking for a direct function which will do
such update on the specified index of any array and will return the
whole modified array. Is it possible? any inbuilt function there?
What do you mean by ‘return the whole modified array’ ?
tamouse mailing lists wrote in post #1103993:
On Mon, Apr 1, 2013 at 12:56 PM, Love U Ruby [email protected]
wrote:
I didn’t make the recursion properly. Now I made it proper,but thanks
for your interest. 
the output of that new version looks different from the output you
said you wanted…
Yeah! finally changed it.
Now need one help.
[1,2][-1] = 44
=> 44
The above is fine. But I am looking for a direct function which will do
such update on the specified index of any array and will return the
whole modified array. Is it possible? any inbuilt function there?
tamouse mailing lists wrote in post #1104059:
On Mon, Apr 1, 2013 at 1:41 PM, Love U Ruby [email protected]
wrote:
tamouse mailing lists wrote in post #1103993:
What do you mean by ‘return the whole modified array’ ?
I want :
[1,2,3][-1] = 12 returns 12 on same update it would return - [1,2,12]
instead of 12, i.e. the whole updated array.
Thanks
Love U Ruby wrote in post #1104060:
tamouse mailing lists wrote in post #1104059:
On Mon, Apr 1, 2013 at 1:41 PM, Love U Ruby [email protected]
wrote:
tamouse mailing lists wrote in post #1103993:
What do you mean by ‘return the whole modified array’ ?
I want :
[1,2,3][-1] = 12 returns 12 on same update it would return - [1,2,12]
instead of 12, i.e. the whole updated array.
Thanks
How about:
(a = [1, 2, 3])[-1] = 12; a
?
In case Scott’s answer isn’t what you were looking for:
If you want to change the return value of a basic operation like that,
then you probably should create your own class to do it.
Messing with the return value in one of Ruby’s default classes is likely
to cause major repercussions elsewhere.
Scott M. wrote in post #1104062:
Love U Ruby wrote in post #1104060:
How about:
(a = [1, 2, 3])[-1] = 12; a
?
Nice one!
but couldn’t get the logic:
C:>irb
irb(main):001:0> (a = [1, 2, 3])[-1] = 12; a
=> [1, 2, 12]
what’s the magic here ;a?
Joel P. wrote in post #1104066:
In case Scott’s answer isn’t what you were looking for:
To be honest, I hope it’s not. I was just having a little fun.
Love U Ruby wrote in post #1104063:
what’s the magic here ;a?
The semicolon will end the previous statement. So my line could be
written as:
a = [1, 2, 3]
a[-1] = 12
a
If you let us know why you need to return the whole array, we might have
more to add.
If it’s something you do commonly then you could always write a method
which does it for you.
However, for the sake of readability in case you (or someone else) has
to work out what the code is doing years later, it’s probably worth
writing it out in an expressive form; tap looks fine.
Scott M. wrote in post #1104067:
Joel P. wrote in post #1104066:
In case Scott’s answer isn’t what you were looking for:
If you let us know why you need to return the whole array, we might have
more to add.
Actually I need the last element to be replaced,and after the update I
will add each_with_object on that updated array. If it possible then I
could do write one liner,otherwise I need to do the job in Break. Thus I
am looking for a direct function or a one liner to do the same.
Here is something I have tried till now:
[1,2,3].tap{ |arr| arr[-1] = 12 }
=> [1, 2, 12]
any small code from you experts?