2 Replies - 744 Views - Last Post: 15 November 2010 - 08:18 AM Rate Topic: -----

#1 pareidolia  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 38
  • Joined: 17-September 09

Method Not Working Like I Want It To

Posted 13 November 2010 - 11:45 AM

I added a method to the Array class, but it is not working like I had hoped. It's not a huge deal for this script, but figuring out what I am doing wrong here might help me in the future too so hopefully someone can point out what I'm doing wrong.

It's not really worth explaining what the script is doing, but I made the method to take out the . and .. from an array that is populated by Dir.entries().

class Array
	#this is a method to remove current directory and parent directory from an array
	def crop
		self.delete('.')
		self.delete('..')
	end
end

log_path = 'C:/Users/arromeo/AppData/Roaming/.purple/logs/aim/myscreenname'
Dir.chdir(log_path)

chat_partners = Dir.entries('.').crop
puts chat_partners


When I run the code like this, the output is simply:
..

Not sure why that happens. If I switch the code to read:
 chat_partners = Dir.entries('.')
chat_partners.crop
puts chat_partners


...it outputs correctly. Why does this happen?

This post has been edited by pareidolia: 13 November 2010 - 11:45 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Method Not Working Like I Want It To

#2 pareidolia  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 38
  • Joined: 17-September 09

Re: Method Not Working Like I Want It To

Posted 15 November 2010 - 01:18 AM

View Postpareidolia, on 13 November 2010 - 10:45 AM, said:

I added a method to the Array class, but it is not working like I had hoped. It's not a huge deal for this script, but figuring out what I am doing wrong here might help me in the future too so hopefully someone can point out what I'm doing wrong.

It's not really worth explaining what the script is doing, but I made the method to take out the . and .. from an array that is populated by Dir.entries().

class Array
	#this is a method to remove current directory and parent directory from an array
	def crop
		self.delete('.')
		self.delete('..')
	end
end

log_path = 'C:/Users/arromeo/AppData/Roaming/.purple/logs/aim/myscreenname'
Dir.chdir(log_path)

chat_partners = Dir.entries('.').crop
puts chat_partners


When I run the code like this, the output is simply:
..

Not sure why that happens. If I switch the code to read:
 chat_partners = Dir.entries('.')
chat_partners.crop
puts chat_partners


...it outputs correctly. Why does this happen?


Never mind, someone in #ruby-lang helped me. I'm not completely sure why, but when I ran the method in the assignment operation it took the last statement of my method and used it as the return value. So I just changed it to:
Array
	#this is a method to remove current directory and parent directory from an array
	def crop
		self.delete('.')
		self.delete('..')
                self
	end
end


Works now.
Was This Post Helpful? 0
  • +
  • -

#3 MitkOK  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 32
  • View blog
  • Posts: 403
  • Joined: 09-August 07

Re: Method Not Working Like I Want It To

Posted 15 November 2010 - 08:18 AM

That's because delete return the result array and you don't assing it to your variable.
Other workaround in 1.9.2 is to use Object#tap:
class Array
  def crop
    self.tap do |obj|
      obj.delete(".")
      obj.delete("..")
    end
  end
end


For the record, many rubyists thinks monkey patching is bad practice.

Cheers
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1