# Call methods from different class

**URL:** <https://www.ruby-forum.com/t/call-methods-from-different-class/139471>\
**Category:** Rails\
**Created:** [June 4, 2008, 7:01am UTC](https://www.ruby-forum.com/t/call-methods-from-different-class/139471 "2008-06-04T07:01:57Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![hloooi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f17d59/32.png) [@hloooi](https://www.ruby-forum.com/u/hloooi)\
**Post date:** [June 4, 2008, 7:01am UTC](https://www.ruby-forum.com/t/call-methods-from-different-class/139471/1 "2008-06-04T07:01:57Z")

</div>

hi again, can i ask something bout the method and class thing?

i have a class called map.rb, and another class called Asset.rb

in my Asset.rb, i have a method called

def base\_part\_of(file\_name)  
name = File.basename(file\_name)  
name.gsub(/[^W.\_-]/, ‘’)  
sanitize\_filename(name)  
end

so, wat can i do in order to get the “name” from base\_part\_of in my map  
class?

isit i use require ‘Asset’, or

isit i create a string called ‘try’  
then try = name or something?

thx for helping again…

cheers~~

---

<div class="post-metadata">

**Author:** ![hloooi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f17d59/32.png) [@hloooi](https://www.ruby-forum.com/u/hloooi)\
**Post date:** [June 4, 2008, 8:33am UTC](https://www.ruby-forum.com/t/call-methods-from-different-class/139471/2 "2008-06-04T08:33:56Z")

</div>

On 4 Jun 2008, at 06:01, loki ja wrote:

> sanitize\_filename(name)  
> end
> 
> so, wat can i do in order to get the “name” from base\_part\_of in my  
> map  
> class?

What you’ve got there is an instance method of the Asset class. You  
can’t (without black magic) pull an instance method out of a class and  
use it on it’s own. So either you’ve got an instance of asser and you  
can call some\_asset.base\_part\_of(…) or you make that method class  
method (which seems reasonable enough in this case) by defining it as  
self.base\_part\_of and then calling it as Asset.base\_part\_of(…). You  
would also have to make sanitize\_filename a class method as well.

Fred

---

<div class="post-metadata">

**Author:** ![hloooi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f17d59/32.png) [@hloooi](https://www.ruby-forum.com/u/hloooi)\
**Post date:** [June 4, 2008, 8:46am UTC](https://www.ruby-forum.com/t/call-methods-from-different-class/139471/3 "2008-06-04T08:46:21Z")

</div>

okok, thx for the tips, i’ll try out and i’ll ask again~~ hehe

---

<div class="post-metadata">

**Author:** ![hloooi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f17d59/32.png) [@hloooi](https://www.ruby-forum.com/u/hloooi)\
**Post date:** [June 4, 2008, 9:09am UTC](https://www.ruby-forum.com/t/call-methods-from-different-class/139471/4 "2008-06-04T09:09:36Z")

</div>

hey fred, would u mind if u help me check the error, in my …  
program,

cause im now at my last part , which is i need to load a random file  
which ends with .lmx, isit ok if u help me check? cause after the tips u  
gave me i think my whole thing is like… screwed … \>.\<

---

<div class="post-metadata">

**Author:** ![hloooi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f17d59/32.png) [@hloooi](https://www.ruby-forum.com/u/hloooi)\
**Post date:** [June 4, 2008, 10:47am UTC](https://www.ruby-forum.com/t/call-methods-from-different-class/139471/5 "2008-06-04T10:47:44Z")

</div>

On Jun 4, 8:09 am, loki ja [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> hey fred, would u mind if u help me check the error, in my …  
> program,
> 
> cause im now at my last part , which is i need to load a random file  
> which ends with .lmx, isit ok if u help me check? cause after the tips u  
> gave me i think my whole thing is like… screwed … \>.\<

If you post some more of your code I’m sure someone would be able to  
help.

Fred

---

<div class="post-metadata">

**Author:** ![hloooi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f17d59/32.png) [@hloooi](https://www.ruby-forum.com/u/hloooi)\
**Post date:** [June 4, 2008, 11:04am UTC](https://www.ruby-forum.com/t/call-methods-from-different-class/139471/6 "2008-06-04T11:04:48Z")

</div>

On 4 Jun 2008, at 09:56, loki ja wrote:

> ow… but its a whole chunks of things…
> 
> or how am i read a file name? jus the file name with the  
> extension .lmx
> 
> a = File.open(“C:/rubydev/prac1/mail6/log/new\_file\_name.lmx” )

File.basename does that

> puts a
> 
> doc = Document.new File.new(“C:/rubydev/prac1/mail6/log/River\_Va.lmx”)
> 
> this is what i have, ive tried to use file.open, but it shows me weird  
> numbers…
> 
> #\<File:0x3799854\>

That’s a file object. Use read/readline/readlines to read from it.

Fred

---

<div class="post-metadata">

**Author:** ![hloooi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f17d59/32.png) [@hloooi](https://www.ruby-forum.com/u/hloooi)\
**Post date:** [June 4, 2008, 10:56am UTC](https://www.ruby-forum.com/t/call-methods-from-different-class/139471/7 "2008-06-04T10:56:36Z")

</div>

ow… but its a whole chunks of things…

or how am i read a file name? jus the file name with the extension .lmx

a = File.open(“C:/rubydev/prac1/mail6/log/new\_file\_name.lmx” )

puts a

doc = Document.new File.new(“C:/rubydev/prac1/mail6/log/River\_Va.lmx”)

this is what i have, ive tried to use file.open, but it shows me weird  
numbers…

#\<File:0x3799854\>

but actually i wanna read those file with extension .lmx, is there a  
way?

cause after reading it, i wanna put inside my doc

…

---

<div class="post-metadata">

**Author:** ![hloooi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f17d59/32.png) [@hloooi](https://www.ruby-forum.com/u/hloooi)\
**Post date:** [June 5, 2008, 11:06am UTC](https://www.ruby-forum.com/t/call-methods-from-different-class/139471/8 "2008-06-05T11:06:26Z")

</div>

On Jun 5, 2:27 am, loki ja [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Now, the problem i faced is, how am i going to read the file was  
> generated from action mailer, by using file.new, file.basename, or file  
> open?

Well somehow you’ve got to figure out where the file is. So either you  
store that in the database at the point at which you process the  
message, or perhaps it’s enough for you to just process all files in  
that folger, in which case Dir.glob should do the trick. On top of  
that your sql query won’t work (single quotes aren’t interpolated, and  
it wouldn’t be valid syntax anyway. It’s also quite hard to read your  
code because there are so many lines there which do nothing.

Fred

---

<div class="post-metadata">

**Author:** ![hloooi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f17d59/32.png) [@hloooi](https://www.ruby-forum.com/u/hloooi)\
**Post date:** [June 5, 2008, 3:27am UTC](https://www.ruby-forum.com/t/call-methods-from-different-class/139471/9 "2008-06-05T03:27:55Z")

</div>

alright, im still stuck, erm, this is my code…

First, this is my action mailer, which read and create file from the  
email, it will auto generates the file after reading the email and save  
it somewhere

require ‘ActionMailer’

class AssetSubmitHandler \< ActionMailer::Base

# content type should be validated to image/gif, image/jpg, or

image/jpeg  
def receive(email)

```
  if email.has_attachments?
    email.attachments.each do |attachment|

    # save original file
  File.open("C:/rubydev/prac1/mail6/log/" + 

```

base\_part\_of(attachment.original\_filename),File::CREAT|File::TRUNC|File::WRONLY,0666){  
|f|  
f.write(attachment.read)

```
      f.close()
    }

  end
end

```

end

def base\_part\_of(file\_name)  
name = File.basename(file\_name)  
name.gsub(/[^W.\_-]/, ‘’)  
sanitize\_filename(name)  
end

# Fixes a ‘feature’ of IE where it passes the entire path instead of

just the filename  
def sanitize\_filename(value)  
#get only the filename (not the whole path)  
just\_filename = value.gsub(/^.\*(\|/)/, ‘’)  
just\_filename.gsub(/[^\w.-]/,’\_’)  
end

end

then, at my map class, i need to read the file and process the lmx file  
and save to database.

require ‘ActiveRecord’

class Map

require ‘rexml/document’  
require “mysql”  
require “dbi”  
include REXML  
require ‘AssetSubmitHandler’

#~ scanfile = File.new(“C:/rubydev/prac1/mail6/log/River\_Va.lmx”)

#~ dbname=“email\_development”  
#~ doc = REXML::Document.new scanfile

a = File.open(“C:/rubydev/prac1/mail6/log/new\_file\_name.lmx” )

puts a

doc = Document.new File.new(“C:/rubydev/prac1/mail6/log/River\_Va.lmx”)  
#~ doc.elements.each(“lm:landmarkCollection”) { |element| puts  
element.attributes[“lm:latitude”]  
#~ latitude = element.attributes[“lm:latitude”]  
#~ }

#~ root = doc.root  
#~ puts  
root.elements[“lm:lmx/lm:landmarkCollection/lm:landmark/lm:coordinates”].attributes[“lm:latitude”]

names = []  
invisibility = XPath.first( doc, “//lm:landmark” )  
XPath.each( doc, “//lm:name”) { |element| puts names \<\< element.text}  
XPath.match( doc, “//lm:name” )  
puts names

latitude = []  
invisibility = XPath.first( doc, “/\*/lm:landmark” )  
XPath.each( doc, “//lm:latitude”) { |element| puts latitude \<\<  
element.text}  
XPath.match( doc, “//lm:latitude” )  
puts latitude

longitude = []  
invisibility = XPath.first( doc, “/\*/lm:landmark” )  
XPath.each( doc, “//lm:longitude”) { |element| puts longitude \<\<  
element.text}  
XPath.match( doc, “//lm:longitude” )  
puts longitude

# db insert

```
 m = Mysql.new("localhost", "root", "", "email_development")

```

sth=m.query(“insert into maps (name,latitude,longtitude) values  
(’#{names}’,’#{latitude}’,’#{longitude}’)”)

end

Now, the problem i faced is, how am i going to read the file was  
generated from action mailer, by using file.new, file.basename, or file  
open?

or is there a better way to get the file generated from action mailer?  
thx for helping me again~~

cheers~~
