Tuesday, October 26, 2010

Started learning Ruby

Hi,

Today I decided to start learning Ruby on Rails 3.0.

Why? Well, there are a dozen of reasons but the most important are:

  • I’ve seen and listened enough about this language and all I’ve heard are positive things, well I assume there are also some negative things like debugging for example (debugging is an issue for a lot of dynamic type languages),
  • also I’d like to be able to make things a little bit more easy. I’d like to do it in a faster manner. I’d also like to have an alternative to .NET, because of its initial costs.
  • Well, also it is because of one of the podcasts I listen, This developer’s life. In podcast #5 Homerun Rob Conery talks with Rails creator David Heinemeier Hansson about Rails creation and about Rails in general, he seems very excited and is very confident about Rails future. Also one listening was watching the panel discussion of .NET Rocks! from NDC (Norwegian Developer Conference 2010). There were a lot of .NET folks and also Ruby folks as well, the panel topic was “ASP.NET MVC vs Ruby on Rails”, in the end silently everybody agreed that Ruby is simpler for startups, and it costs less (I mean you don’t need Visual Studio licenses and SQL Servers).
  • They all talk about this great Ruby community which is willing to help you if you will have any problems at all. I want to check that out.
  • I hope understanding more ASP.NET MVC 3 after making something practical in Ruby on Rails.
  • We do have now DLR  and IronRuby 1.0 in .NET so I could use my new set of skills in my usual environment.

Now where should I start from?

Assuming that I want to learn fast I found these two videos which apparently enough are very famous in Ruby world, and gave me a taste of what kind of power Ruby is:

Rails creator David Heinemeier Hansson shows how to make a blog in 15 minutes using Ruby on Rails

 

And the same kind of video but using already Rails 2.0 (it has 2 parts)

Creating blog in 15 minutes by Ryan Bates Part 1

 

Creating blog in 15 minutes by Ryan Bates Part 2

Now I must admit that I’ve seen it before using ASP.NET MVC but still I think it is very power. By the way the new ASP.NET MVC 3 is in beta now so you can check it out.

I also found a nice site on starting Ruby on Rails 3.0 using all modern tools which are used in the community like Git, GitHub and Heroku. And because it is not too long I’ll start to read these tutorial. Learn Rails by Example.

Also to be motivated I’ll need to do something real. Because I’m playing in a strategic web game fr.europe1400.com, and I’m the founder of the guild there I will try to create a web site for this guild.

What should this website include:

  1. Authentication
  2. User roles: admin, moderator, others
  3. User details
  4. Informative section: includes videos, pictures
  5. Discussion panel: which is pretty much the same as a forum or blog: someone creates a post and then someone else comment this post.
  6. Some admin only functionalities.

Well I hope it won’t take too long till this site will be up and running on Heroku. See ya, hope next time I’ll bring some code.

Wednesday, October 20, 2010

Tools I can’t imagine my dev life without #2: XSLT Debugger in Visual Studio

Hi,

While a lot of us .NET developers at some point in our life used XSLT, it was never easy. Of course our logic and XSLT syntax should help us to understand different things but that is not always the case. Probably because it’s almost always a black box abstraction.

In my last project we use a lot of XSLT for generating all kind of documents with all kind of restrictions and filtering, usually you have an XML document, you got an XSLT and a XslCompiledTransform object which you use to actually get your transformed document. But because of the thing that it is like a black box all you can do is sit back and hope you did well the XSLT thing and you’ll get needed document out of the box. At least this is what I thought until this week.

This week I needed to change one of my XSLT document templates, because I was getting a very strange error at document generation. I thought at this moment that I couldn’t do it without debugging and I was hoping that somewhere in the world existed a nice tool which could manage XSLT debugging and which would have at least a trial period. I googled “debug XSLT”, and guess what I found? This link which gave me the power I didn’t even imagined to have without having to install some third-party tools.

It is incorporated directly in Visual Studio !!! I tried it in VS 2008 and VS 2010, Visual Studio 2010 even got a profiler for XSLT, so you could measure your transformation sheets and make reports on that. Isn’t that great? Plus you got a lot of way to do debugging. And now some screenshots.

DebugXslt

1. While stylesheet open click on XML => Debug Xslt

ChooseXml

2. Choose input XML file

Debugging

3. Debug and actually see the magic ! In parallel you can see the output.

Same things could be done in VS 2010

Visual Studio 2010

And you automatically get the html output. Plus you can always see the source of html.

So get ready to a new era of XSLT debugging with all the tools you are used to: Locals, Watches, Immediate Window and others as well.

Link for Download

Tuesday, April 27, 2010

Code4Food #6: How to join multiple CSV files with the same structure ?

Hi,

Reading another question on Bytes.com I found this question. So assuming these CSV files have the same structure, I can easily join them.

Now to test it I split this CSV file into 5 files: CsvFile1.csv … CsvFile5.csv

"REVIEW_DATE","AUTHOR","ISBN","DISCOUNTED_PRICE"
"1985/01/21","Douglas Adams",0345391802,5.95
"1990/01/12","Douglas Hofstadter",0465026567,9.95
"1998/07/15","Timothy ""The Parser"" Campbell",0968411304,18.99
"1999/12/03","Richard Friedman",0060630353,5.95
"2001/09/19","Karen Armstrong",0345384563,9.95
"2002/06/23","David Jones",0198504691,9.95
"2002/06/23","Julian Jaynes",0618057072,12.50
"2003/09/30","Scott Adams",0740721909,4.95
"2004/10/04","Benjamin Radcliff",0804818088,4.95
"2004/10/04","Randel Helms",0879755725,4.50

Now I made the program which makes the deal. Nothing to special here, just want to mention you do not need joining multiple times the column headers.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.IO;
using System.Text;

namespace CsvJoiner
{
    static class Program
    {
        public static void Main()
        {
            string[] csvFileNames = Directory.GetFiles(".", "CsvFile*.csv");

            JoinCsvFiles(csvFileNames, "CsvOutput.csv");
        }

        private static void JoinCsvFiles(string[] csvFileNames, string outputDestinationPath)
        {
            StringBuilder sb = new StringBuilder();

            bool columnHeadersRead = false;

            foreach (string csvFileName in csvFileNames)
            {
                TextReader tr = new StreamReader(csvFileName);

                string columnHeaders = tr.ReadLine();

                // Skip appending column headers if already appended
                if (!columnHeadersRead)
                {
                    sb.AppendLine(columnHeaders);
                    columnHeadersRead = true;
                }

                sb.AppendLine(tr.ReadToEnd());  
            }

            File.WriteAllText(outputDestinationPath, sb.ToString());
        }
    }
}
Now I'll show the output. To check out this project
download the sources here
"REVIEW_DATE","AUTHOR","ISBN","DISCOUNTED_PRICE"
"1985/01/21","Douglas Adams",0345391802,5.95
"1990/01/12","Douglas Hofstadter",0465026567,9.95
"1998/07/15","Timothy ""The Parser"" Campbell",0968411304,18.99
"1999/12/03","Richard Friedman",0060630353,5.95
"2001/09/19","Karen Armstrong",0345384563,9.95
"2002/06/23","David Jones",0198504691,9.95
"2002/06/23","Julian Jaynes",0618057072,12.50
"2003/09/30","Scott Adams",0740721909,4.95
"2004/10/04","Benjamin Radcliff",0804818088,4.95
"2004/10/04","Randel Helms",0879755725,4.50

Monday, April 26, 2010

Code4Food #5: How to get specific XML nodes using Linq to XML ?

Hi, another question has been received from Bytes.com.

So the problem was that we needed to get all the tags “Error” from a specific XML without actually caring to much about where exactly in the XML tree is Error tag. And we need to do this using Linq to Xml.

So he got an xml like these:

<Users>
      <User>
           <FirstName>Tom</FirstName>
           <LastName>Won</LastName>
           <Error>Test 2</Error>
       </User>
      <User>
           <FirstName>Jim</FirstName>
           <LastName>Kim</LastName>
           <Error>Test 2</Error>
       </User>
       <Error>Test 3</Error>
</Users>

Now this problem isn't actually a problem at all. All you need is to use Linq to XML objects like XElement, XDocument, XNode, and so on (opposite thing to XmlElement, XmlDocument, and XmlNode). Actually you can do it without directly using Linq, by callings Descendants(XName name) method.

So this is the code that I’ve written to make this done.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Xml.Linq;

namespace GetErrorTags.Linq2Xml
{
    static class Program
    {
        static void Main()
        {
            // Load the document into root XElement.
            // Remember in Linq you work with XDocument, XElement, XNode and not with XmlDocument, etc.
            XElement rootElement = XElement.Load("ErrorTags.xml");

            // without directly using Linq
            var errorTags = rootElement.Descendants("Error");

            Console.WriteLine("==============Without using Linq=========");

            foreach (XElement tagError in errorTags)
            {
                Console.WriteLine(tagError);
            }

            Console.ReadKey();

            // using Ling directly, we'll select only "Test 2" errors
            var linqErrorTags = rootElement.Descendants("Error").Where(element => element.Value.Equals("Test 2"));

            Console.WriteLine("==============Using Linq=========");

            foreach (XElement tagError in linqErrorTags)
            {
                Console.WriteLine(tagError);
            }

            Console.ReadKey();

        }
    }
}

And here is the output:

Download source

Thursday, April 22, 2010

Code4Food #4: How to change WinForms Treeview control image size?

Hi,

Today I visited Bytes.com and saw a question about Treeview.

Indeed how would one change ImageSize of a node ? So I had 15 free minutes and this is what I found.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TreeView.ImageSize
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            FillTreeView();
        }

        private void FillTreeView()
        {
            // Load the images in an ImageList.
            ImageList myImageList = new ImageList();
            myImageList.Images.Add(Image.FromFile("63vts4.jpg"));
            myImageList.Images.Add(Image.FromFile("ToDeleteIfNotSold.png"));
            
            // Change the size of the images.
            myImageList.ImageSize = new Size(50, 50);

            // Assign the ImageList to the TreeView.
            trvImages.ImageList = myImageList;
            trvImages.ItemHeight = 50;

            // Set the TreeView control's default image and selected image indexes.
            trvImages.ImageIndex = 0;
            trvImages.SelectedImageIndex = 1;

            // Create the root tree node.
            TreeNode rootNode = new TreeNode("CustomerList");

            TreeNode childNode1 = new TreeNode("Customer1");
            TreeNode childNode2 = new TreeNode("Customer2");

            // Add a main root tree node.
            trvImages.Nodes.Add(rootNode);
            trvImages.Nodes[0].Nodes.Add(childNode1);
            trvImages.Nodes[0].Nodes.Add(childNode2);
        }
    }
}

So the magic line was myImageList.ImageSize = new Size(50, 50); Here is the output and the code to download.

Download source code link