Using something like ?a will give you a’s ascii code. So, if you’re under Ruby 1.8.6, you can do something like this:
a = "whatever/"
puts a[-1] == ?/ # this will print true, because the code of the last character is the same as the code of ?
Using something like ?a will give you a’s ascii code. So, if you’re under Ruby 1.8.6, you can do something like this:
a = "whatever/"
puts a[-1] == ?/ # this will print true, because the code of the last character is the same as the code of ?
class Option
def get
raise "no value here"
end
def have_value?
raise "no value"
end
def getOrElse(value)
if have_value?
return get()
else
return value
end
end
end
class None < Option
def have_value?
false
end
end
class Some < Option
def get
return @value
end
def initialize(value)
@value = value
end
def have_value?
true
end
end
def find_user(name)
found = ["User1","User2","User3"].find {|z| z == name}
if found
return Some.new(found)
else
return None.new
end
end
user = find_user("User4").getOrElse("user not found")
puts user # will print user not found
user2 = find_user("User1").getOrElse("user not found")
puts user2 # will print User1
In the previous post about creating an executable jar from your Scala code, I showed how to create the jar using your favourite compression software.
Today, you’ll see how to do it using jarjar. jarjar can be used to package all of your jars into a single one, thus eliminating external dependencies.
The great part is that it has an ant task, so I’ll show you how to use that:
<project name="jarjar tester">
<target name="jrjr">
<taskdef name="jarjar" classname="com.tonicsystems.jarjar.JarJarTask" classpath="jarjar-1.0.jar" />
<jarjar destfile="jrjarilo.jar" manifest="Manifest.mf">
<zipfileset src="scala-library.jar" />
<fileset dir=".">
<include name="*.class" />
</fileset>
</jarjar>
</target>
</project>
You need to supply it with a manifest, so, you could use the one from here. You need to adjust the paths to your scala-library.jar and to jarjar.jar, and to specify the name you’d like your jar to receive.
If everything goes well ( and it should ), after running this target you should have your own executable jar, containing the Scala runtime.
I just tried the Groovy Eclipse plugin again ( after a pause of about 4-5 months ). I must say I’m very impressed with what it can do.
Nice job, Groovy plugin team!
For a while now I kept thinking how I would create an executable jar that would contain the Scala runtime. Tonight, I decided to give it a shot and try to make it work.
If you don’t already know, a jar is nothing but a zip file that includes a manifest ( which holds some information ).
To test this, I used the following code:
import java.io._
object Whatever
{
def main(args:Array[String]) = {
new File(args(0)).listFiles.foreach(println)
}
}
So … first we compile it to bytecode. We use scalac for that:
scalac x.scala
If you’re using the code I posted above, you should get 3 classes out of the compilation process. Next, we need to create a manifest:
Main-Class: Whatever
Please notice there is an extra line after the line with Main-Class.
Now, we need to actually create the jar. We use the jar binary that ships with your Java installation:
jar -cvfm h.jar Manifest.mf Whatever.class Whatever$$anonfun$main$1.class Whatever.class Whatever$.class
Ok, so if we now run the existing h.jar jar, you should get this exception:
Exception in thread "main" java.lang.NoClassDefFoundError: scala/ScalaObject
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632)
at java.lang.ClassLoader.defineClass(ClassLoader.java:616)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:14
1)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
at Whatever.main(x.scala)
Caused by: java.lang.ClassNotFoundException: scala.ScalaObject
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
... 13 more
It’s because it can’t find the Scala runtime. So … here comes the “not so elegant way” of adding the Scala runtime
. In your Scala’s installation folder, you have a folder named lib which contains a jar named scala-library.jar. I’m assuming you have some software that can extract zip archives ( I think most operating systems have this by default ). Extract that archive to some directory, and notice the scala folder. Now, open the jar we created above using your favourite compression software. You should have the following structure:

Remember the scala package I told you about a couple of lines above? Simply drag it into your archive, at the same level as the META-INF folder, like in the following picture:

After this is over, you can try to run your jar again:
java -jar h.jar .
And it should/will work. In a day or two, I’ll show you how to accomplish the same thing using jarjar.
If you want the changes made to a folder’s view to apply to all other folders as well, go to Tools->Folder Options->View and click on Apply to all folders.
Dir["./**/*.rb"].inject(0) {|v,e| v + File.readlines(e).size }
this will count the number of lines of all the .rb files in the current folder and subfolders.